Skip to content

Commit

Permalink
Merge branch 'feature/graphql' into graphql/fix-error-messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik authored Dec 17, 2021
2 parents 6a73d1d + 22b483c commit c0671d1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
2 changes: 0 additions & 2 deletions priv/graphql/schemas/admin/admin_schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ schema{
mutation: AdminMutation
}

directive @protected on FIELD_DEFINITION | OBJECT

"""
Contains all admin available queries.
Only an authenticated admin can execute these queries.
Expand Down
11 changes: 11 additions & 0 deletions priv/graphql/schemas/global/jid.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
Jabber Identifiers (JIDs) uniquely identify individual entities in the xmpp network.
"""
type JID{
"The user identifier"
user: String
"The server identifier"
server: String!
"The resource identifier"
resource: String
}
2 changes: 2 additions & 0 deletions priv/graphql/schemas/global/protected_dir.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"Marks the resource to be accessed only by authorized requests"
directive @protected on FIELD_DEFINITION | OBJECT
2 changes: 0 additions & 2 deletions priv/graphql/schemas/user/user_schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ schema{
mutation: UserMutation
}

directive @protected on FIELD_DEFINITION | OBJECT

"""
Contains all user available queries.
Only authenticated user can execute this queries.
Expand Down
25 changes: 17 additions & 8 deletions src/mongoose_graphql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
%% @doc Create and initialize endpoints for user and admin.
-spec init() -> ok.
init() ->
create_endpoint(?USER_EP_NAME, user_mapping_rules(), schema_pattern("user")),
create_endpoint(?ADMIN_EP_NAME, admin_mapping_rules(), schema_pattern("admin")),
create_endpoint(?USER_EP_NAME, user_mapping_rules(), schema_global_patterns("user")),
create_endpoint(?ADMIN_EP_NAME, admin_mapping_rules(), schema_global_patterns("admin")),
ok.

%% @doc Get endpoint_context for passed endpoint name.
Expand All @@ -42,11 +42,11 @@ get_endpoint(Name) ->
graphql_schema:get_endpoint_ctx(Name).

%% @doc Create a new endpoint and load schema.
-spec create_endpoint(atom(), map(), file:filename_all()) -> gen:start_ret().
create_endpoint(Name, Mapping, Pattern) ->
-spec create_endpoint(atom(), map(), [file:filename_all()]) -> gen:start_ret().
create_endpoint(Name, Mapping, Patterns) ->
Res = graphql_schema:start_link(Name),
Ep = graphql_schema:get_endpoint_ctx(Name),
{ok, SchemaData} = load_multiple_file_schema(Pattern),
{ok, SchemaData} = load_multiple_file_schema(Patterns),
ok = graphql:load_schema(Ep, Mapping, SchemaData),
ok = graphql:validate_schema(Ep),
Res.
Expand Down Expand Up @@ -94,8 +94,17 @@ execute(Ep, OpName, Doc) ->

% Internal

-spec schema_global_patterns(file:name_all()) -> [file:filename_all()].
schema_global_patterns(SchemaDir) ->
[schema_pattern(SchemaDir), schema_pattern("global")].

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

-spec schema_pattern(file:name_all(), file:name_all()) -> file:filename_all().
schema_pattern(DirName, Pattern) ->
filename:join([code:priv_dir(mongooseim), "graphql/schemas", DirName, Pattern]).

graphql_parse(Doc) ->
case graphql:parse(Doc) of
Expand All @@ -122,8 +131,8 @@ user_mapping_rules() ->
}
}.

load_multiple_file_schema(Pattern) ->
Paths = filelib:wildcard(Pattern),
load_multiple_file_schema(Patterns) ->
Paths = lists:flatmap(fun(P) -> filelib:wildcard(P) end, Patterns),
try
SchemaData = [read_schema_file(P) || P <- Paths],
{ok, lists:flatten(SchemaData)}
Expand Down
21 changes: 17 additions & 4 deletions test/mongoose_graphql_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ all() ->
[can_create_endpoint,
can_load_split_schema,
unexpected_internal_error,
admin_and_user_load_global_types,
{group, unprotected_graphql},
{group, protected_graphql},
{group, errors_handling},
Expand Down Expand Up @@ -52,7 +53,7 @@ init_per_testcase(C, Config) when C =:= auth_can_execute_protected_query;
C =:= unauth_cannot_execute_protected_mutation;
C =:= unauth_can_access_introspection ->
{Mapping, Pattern} = example_schema_protected_data(Config),
{ok, _} = mongoose_graphql:create_endpoint(C, Mapping, Pattern),
{ok, _} = mongoose_graphql:create_endpoint(C, Mapping, [Pattern]),
Ep = mongoose_graphql:get_endpoint(C),
[{endpoint, Ep} | Config];
init_per_testcase(C, Config) when C =:= can_execute_query_with_vars;
Expand All @@ -64,7 +65,7 @@ init_per_testcase(C, Config) when C =:= can_execute_query_with_vars;
C =:= should_catch_type_check_errors;
C =:= should_catch_parsing_errors ->
{Mapping, Pattern} = example_schema_data(Config),
{ok, _} = mongoose_graphql:create_endpoint(C, Mapping, Pattern),
{ok, _} = mongoose_graphql:create_endpoint(C, Mapping, [Pattern]),
Ep = mongoose_graphql:get_endpoint(C),
[{endpoint, Ep} | Config];
init_per_testcase(C, Config) ->
Expand All @@ -76,7 +77,7 @@ end_per_testcase(_, _Config) ->
can_create_endpoint(Config) ->
Name = ?config(endpoint_name, Config),
{Mapping, Pattern} = example_schema_protected_data(Config),
{ok, Pid} = mongoose_graphql:create_endpoint(Name, Mapping, Pattern),
{ok, Pid} = mongoose_graphql:create_endpoint(Name, Mapping, [Pattern]),

Ep = mongoose_graphql:get_endpoint(Name),
?assertMatch({endpoint_context, Name, Pid, _, _}, Ep),
Expand All @@ -87,7 +88,7 @@ can_create_endpoint(Config) ->
can_load_split_schema(Config) ->
Name = ?config(endpoint_name, Config),
{Mapping, Pattern} = example_split_schema_data(Config),
{ok, Pid} = mongoose_graphql:create_endpoint(Name, Mapping, Pattern),
{ok, Pid} = mongoose_graphql:create_endpoint(Name, Mapping, [Pattern]),

Ep = mongoose_graphql:get_endpoint(Name),
?assertMatch({endpoint_context, Name, Pid, _, _}, Ep),
Expand All @@ -103,6 +104,18 @@ unexpected_internal_error(Config) ->
Res = mongoose_graphql:execute(Name, undefined, Doc),
?assertEqual({error, internal_crash}, Res).

admin_and_user_load_global_types(_Config) ->
mongoose_graphql:init(),
AdminEp = mongoose_graphql:get_endpoint(admin),
?assertMatch(#object_type{id = <<"JID">>}, graphql_schema:get(AdminEp, <<"JID">>)),
?assertMatch(#directive_type{id = <<"protected">>},
graphql_schema:get(AdminEp, <<"protected">>)),

UserEp = mongoose_graphql:get_endpoint(user),
?assertMatch(#object_type{id = <<"JID">>}, graphql_schema:get(UserEp, <<"JID">>)),
?assertMatch(#directive_type{id = <<"protected">>},
graphql_schema:get(UserEp, <<"protected">>)).

auth_can_execute_protected_query(Config) ->
Ep = ?config(endpoint, Config),
Doc = <<"{ field }">>,
Expand Down

0 comments on commit c0671d1

Please sign in to comment.