Skip to content

Commit

Permalink
Add support for multi file schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Nov 23, 2021
1 parent 5015e8c commit 10baf3b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 46 deletions.
32 changes: 0 additions & 32 deletions priv/graphql/api_schema.gql → priv/graphql/admin_schema.gql
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
type Query {
"Contains all admin available queries"
admin: AdminQuery
"Contains all user available queries"
user: UserQuery
}

type Mutation {
"Contains all user available mutations"
admin: AdminMutation
"Contains all user available mutations"
user: UserMutation
}

"""
Contains all admin available queries.
Only authenticated admin can execute this queries.
Expand All @@ -38,24 +24,6 @@ type AdminMutation{
disableDomain(domain: String!): Domain
}

"""
Contains all user available queries.
Only authenticated user can execute this queries.
"""
type UserQuery{
"Something to not leave type without fields"
field: String
}

"""
Contains all user available mutations.
Only authenticated user can execute this mutations.
"""
type UserMutation{
"Something to not leave type without fields"
field: String
}

"""
A dynamic domain representation.
Some operation could return not complete object i.e. some fields can be null.
Expand Down
13 changes: 13 additions & 0 deletions priv/graphql/root_schema.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Query {
"Contains all admin available queries"
admin: AdminQuery
"Contains all user available queries"
user: UserQuery
}

type Mutation {
"Contains all user available mutations"
admin: AdminMutation
"Contains all user available mutations"
user: UserMutation
}
17 changes: 17 additions & 0 deletions priv/graphql/user_schema.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
Contains all user available queries.
Only authenticated user can execute this queries.
"""
type UserQuery{
"Something to not leave type without fields"
field: String
}

"""
Contains all user available mutations.
Only authenticated user can execute this mutations.
"""
type UserMutation{
"Something to not leave type without fields"
field: String
}
43 changes: 29 additions & 14 deletions src/mongoose_graphql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

-ignore_xref([execute/1, execute/3]).

-define(SCHEMA_PATH, "graphql/api_schema.gql").
-define(SCHEMA_PATH_PATTERN, "graphql/*.gql").

-spec init() -> ok.
init() ->
graphql_schema:reset(),
PrivDir = code:priv_dir(mongooseim),
{ok, SchemaData} = file:read_file( filename:join(PrivDir, ?SCHEMA_PATH)),
Pattern =
filename:join(
code:priv_dir(mongooseim), ?SCHEMA_PATH_PATTERN),
{ok, SchemaData} = load_multiple_file_schema(Pattern),
Mapping = mapping_rules(),
ok = graphql:load_schema(Mapping, SchemaData),
ok = setup_root(),
Expand All @@ -21,15 +23,17 @@ init() ->
execute(Doc) ->
execute(<<>>, admin, Doc).

-spec execute(binary(), mongoose_graphql_permission:role(), binary()) -> {ok, map()} | {error, term()}.
-spec execute(binary(), mongoose_graphql_permission:role(), binary()) ->
{ok, map()} | {error, term()}.
execute(OpName, Role, Doc) ->
case graphql:parse(Doc) of
{ok, Ast} ->
try
{ok, #{ast := Ast2 }} = graphql:type_check(Ast),
{ok, #{ast := Ast2}} = graphql:type_check(Ast),
ok = graphql:validate(Ast2),
Ctx = #{params => #{}, operation_name => OpName,
role => Role},
Ctx = #{params => #{},
operation_name => OpName,
role => Role},
{ok, graphql:execute(Ctx, Ast2)}
catch
throw:{error, Err} ->
Expand All @@ -50,14 +54,25 @@ mapping_rules() ->
'UserQuery' => mongoose_graphql_user_query,
'UserMutation' => mongoose_graphql_user_mutation,
'Domain' => mongoose_graphql_domain,
'default' => mongoose_graphql_default
}
}.
default => mongoose_graphql_default
}
}.

setup_root() ->
Root = {root,
#{ query => 'Query',
mutation => 'Mutation'
}},
Root = {root, #{query => 'Query', mutation => 'Mutation'}},
ok = graphql:insert_root(Root),
ok.

load_multiple_file_schema(Pattern) ->
Paths = filelib:wildcard(Pattern),
try
SchemaData = [read_schema_file(P) || P <- Paths],
{ok, lists:flatten(SchemaData)}
catch
_:_ ->
{error, cannot_load}
end.

read_schema_file(Path) ->
{ok, Data} = file:read_file(Path),
binary_to_list(Data).

0 comments on commit 10baf3b

Please sign in to comment.