Skip to content

Commit

Permalink
Apply review, Fix formatting
Browse files Browse the repository at this point in the history
- Use jiffy instead of jsx
- Fix descriptions
- Other small fixes
  • Loading branch information
Premwoik committed Dec 9, 2021
1 parent fba3a94 commit a719db0
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 85 deletions.
2 changes: 2 additions & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
remove_personal_data_pubsub],
"at the moment mod_pubsub doesn't support dynamic domains"}.

{suites, "tests", graphql_SUITE}.

{suites, "tests", inbox_SUITE}.

{suites, "tests", inbox_extensions_SUITE}.
Expand Down
16 changes: 4 additions & 12 deletions big_tests/tests/graphql_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,15 @@ cowboy_handler() ->
can_connect_to_user].

user_handler() ->
[wrong_creds_cannot_access_protected_types,
unauth_cannot_access_protected_types,
unauth_can_access_unprotected_types,
can_execute_query_with_variables,
auth_user_can_access_protected_types,
invalid_json_body_error,
no_query_supplied_error,
variables_invalid_json_error,
can_load_graphiql].


[auth_user_can_access_protected_types | common_tests()].
admin_handler() ->
[auth_admin_can_access_protected_types | common_tests()].

common_tests() ->
[wrong_creds_cannot_access_protected_types,
unauth_cannot_access_protected_types,
unauth_can_access_unprotected_types,
can_execute_query_with_variables,
auth_admin_can_access_protected_types,
invalid_json_body_error,
no_query_supplied_error,
variables_invalid_json_error,
Expand Down
4 changes: 2 additions & 2 deletions big_tests/tests/mongooseimctl_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1138,12 +1138,12 @@ can_handle_execution_error(Config) ->
graphql_wrong_arguments_number(Config) ->
ExpectedFragment = "This command requires",
ResNoArgs = mongooseimctl("graphql", [], Config),
?assertMatch({_, 0}, ResNoArgs),
?assertMatch({_, 1}, ResNoArgs),
Data1 = element(1, ResNoArgs),
?assertNotEqual(nomatch, string:find(Data1, ExpectedFragment)),

ResTooManyArgs = mongooseimctl("graphql", ["{}", "{}"], Config),
?assertMatch({_, 0}, ResTooManyArgs),
?assertMatch({_, 1}, ResTooManyArgs),
Data2 = element(1, ResTooManyArgs),
?assertNotEqual(nomatch, string:find(Data2, ExpectedFragment)).

Expand Down
6 changes: 3 additions & 3 deletions priv/graphql/wsite/index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!--
* Copyright (c) Facebook, Inc.
* Copyright (c) 2021 GraphQL Contributors
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
* This source code is licensed under the license
* https://github.com/graphql/graphiql/blob/main/LICENSE.
-->
<!DOCTYPE html>
<html>
Expand Down
6 changes: 3 additions & 3 deletions src/ejabberd_ctl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ process(["graphql", Arg]) when is_list(Arg) ->
Ep = mongoose_graphql:get_endpoint(admin),
case mongoose_graphql:execute(Ep, undefined, Doc) of
{ok, Result} ->
PrettyResult = jsx:prettify(jsx:encode(Result)),
PrettyResult = jiffy:encode(Result, [pretty]),
?PRINT("~s\n", [PrettyResult]);
{error, _} = Err ->
?PRINT("~p\n", [Err])
end,
?STATUS_SUCCESS;
process(["graphql" | _]) ->
?PRINT("This command requires one string type argument!\n", []),
?STATUS_SUCCESS;
?STATUS_ERROR;

%% @doc The arguments --long and --dual are not documented because they are
%% automatically selected depending in the number of columns of the shell
Expand Down Expand Up @@ -571,7 +571,7 @@ print_usage(HelpMode, MaxC, ShCode) ->
{"restart", [], "Restart MongooseIM"},
{"help", ["[--tags [tag] | com?*]"], "Show help (try: mongooseimctl help help)"},
{"mnesia", ["[info]"], "show information of Mnesia system"},
{"graphql", ["query"], "Executes graphql query or mutation"}] ++
{"graphql", ["query"], "Execute graphql query or mutation"}] ++
get_list_commands() ++
get_list_ctls(),

Expand Down
4 changes: 1 addition & 3 deletions src/mongoose_graphql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ execute(Ep, OpName, Doc) ->
% Internal

schema_pattern(DirName) ->
filename:join([code:priv_dir(mongooseim),
"graphql/schemas",
DirName, "*.gql"]).
filename:join([code:priv_dir(mongooseim), "graphql/schemas", DirName, "*.gql"]).

graphql_parse(Doc) ->
case graphql:parse(Doc) of
Expand Down
27 changes: 13 additions & 14 deletions src/mongoose_graphql/mongoose_graphql_cowboy_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ is_authorized(Req, State) ->
State2 = check_auth(Auth, State),
{true, Req, State2}.

resource_exists(#{ method := <<"GET">> } = Req, State) ->
resource_exists(#{method := <<"GET">>} = Req, State) ->
{true, Req, State};
resource_exists(#{ method := <<"POST">> } = Req, State) ->
resource_exists(#{method := <<"POST">>} = Req, State) ->
{false, Req, State}.

to_html(Req, #{ index_location :=
{priv_file, App, FileLocation}} = State) ->
to_html(Req, #{index_location := {priv_file, App, FileLocation}} = State) ->
Filename = filename:join(code:priv_dir(App), FileLocation),
{ok, Data} = file:read_file(Filename),
{Data, Req, State}.
Expand Down Expand Up @@ -107,7 +106,7 @@ auth_admin(_, State) ->
% auth credentials not provided in config
State#{authorized => true}.

run_request(#{ document := undefined }, Req, State) ->
run_request(#{document := undefined}, Req, State) ->
reply_error(400, no_query_supplied, Req, State);
run_request(#{} = ReqCtx, Req, #{schema_endpoint := EpName,
authorized := AuthStatus} = State) ->
Expand All @@ -127,11 +126,11 @@ run_request(#{} = ReqCtx, Req, #{schema_endpoint := EpName,
gather(Req) ->
{ok, Body, Req2} = cowboy_req:read_body(Req),
Bindings = cowboy_req:bindings(Req2),
try jsx:decode(Body, [return_maps]) of
try jiffy:decode(Body, [return_maps]) of
JSON ->
gather(Req2, JSON, Bindings)
catch
error:badarg ->
_:_ ->
{error, invalid_json_body}
end.

Expand All @@ -140,26 +139,26 @@ gather(Req, Body, Params) ->
case variables([Params, Body]) of
{ok, Vars} ->
Operation = operation_name([Params, Body]),
{ok, Req, #{ document => QueryDocument,
{ok, Req, #{document => QueryDocument,
vars => Vars,
operation_name => Operation}};
{error, Reason} ->
{error, Reason}
end.

document([#{ <<"query">> := Q }|_]) -> Q;
document([#{<<"query">> := Q}|_]) -> Q;
document([_|Next]) -> document(Next);
document([]) -> undefined.

variables([#{ <<"variables">> := Vars} | _]) ->
variables([#{<<"variables">> := Vars} | _]) ->
if
is_binary(Vars) ->
try jsx:decode(Vars, [return_maps]) of
try jiffy:decode(Vars, [return_maps]) of
null -> {ok, #{}};
JSON when is_map(JSON) -> {ok, JSON};
_ -> {error, variables_invalid_json}
catch
error:badarg ->
_:_ ->
{error, variables_invalid_json}
end;
is_map(Vars) ->
Expand All @@ -172,7 +171,7 @@ variables([_ | Next]) ->
variables([]) ->
{ok, #{}}.

operation_name([#{ <<"operationName">> := OpName } | _]) ->
operation_name([#{<<"operationName">> := OpName} | _]) ->
OpName;
operation_name([_ | Next]) ->
operation_name(Next);
Expand All @@ -189,7 +188,7 @@ reply_error(Code, Msg, Req, State) ->
[#{type => error, message => Formatted}]
end,

Body = jsx:encode(#{ errors => Errors}),
Body = jiffy:encode(#{errors => Errors}),
Req2 = cowboy_req:set_resp_body(Body, Req),
Reply = cowboy_req:reply(Code, Req2),
{stop, Reply, State}.
2 changes: 1 addition & 1 deletion src/mongoose_graphql/mongoose_graphql_cowboy_response.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-export([term_to_json/1]).

term_to_json(Term) ->
jsx:encode(fixup(Term)).
jiffy:encode(fixup(Term)).

%% Ground types
fixup(Term) when is_number(Term) -> Term;
Expand Down
2 changes: 1 addition & 1 deletion src/mongoose_graphql/mongoose_graphql_default.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
-ignore_xref([execute/4]).

%% Assume we are given a map(). Look up the field in the map. If not
%% %% present, return the value null.
%% present, return the value null.
execute(_Ctx, Obj, Field, _Args) ->
{ok, maps:get(Field, Obj, null)}.
4 changes: 2 additions & 2 deletions src/mongoose_graphql/mongoose_graphql_errors.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

-ignore_xref([err/2, crash/2]).

% callback invoked when resorer returns error tuple
% callback invoked when resolver returns error tuple
err(_Ctx, domain_not_found) ->
#{message => <<"Given domain does not exist">>, extensions => #{code => resolver_error}};
err(_Ctx, ErrorTerm) ->
#{message => iolist_to_binary(io_lib:format("~p", [ErrorTerm])),
extensions => #{code => resolver_error}}.

% callback invoked when resoler crashes
% callback invoked when resolver crashes
crash(_Ctx, #{type := Type}) ->
#{message => <<"Unexpected ", Type/binary, " resolver crash">>,
extensions => #{code => resolver_crash}}.
11 changes: 5 additions & 6 deletions src/mongoose_graphql/mongoose_graphql_permissions.erl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
%% @doc Module that checks if a requested query can be executed with provided
%% permissions.
%% @doc Checks if a requested query can be executed with provided permissions.
%%
%% GraphQL has directives that allow attaching additional information to schema,
%% to objects, to fields, and more. The custom directive `@protected' is created
%% to mark which objects or fields could be accessed only by authorized request.
%% to mark which objects or fields could be accessed only by an authorized request.
%% This module analyzes the AST and tries to find if there is at least one protected
%% resource.
%%
%% If unauthorized request want to execute a query that contains protected resources,
%% If an unauthorized request wants to execute a query that contains protected resources,
%% an error is thrown.
%%
%% Directives can have arguments, so if needed this functionality can be easily
Expand All @@ -25,7 +24,7 @@
-type auth_status() :: boolean().

%% @doc Checks if query can be executed by unauthorized request. If not, throws
%% an error. When request is authorized just skip.
%% an error. When request is authorized, just skip.
%% @end
-spec check_permissions(binary(), auth_status(), #document{}) -> ok.
check_permissions(OpName, false, #document{definitions = Definitions}) ->
Expand All @@ -41,7 +40,7 @@ check_permissions(OpName, false, #document{definitions = Definitions}) ->
% When an object is protected we need to ensure that the request
% query contains only introspection fields to execute it without
% authorization. Otherwise, a user couldn't access documentation
% without login in.
% without logging in.
case is_introspection_op(Op1) of
true ->
ok;
Expand Down
Loading

0 comments on commit a719db0

Please sign in to comment.