diff --git a/priv/graphql/api_schema.gql b/priv/graphql/admin_schema.gql similarity index 62% rename from priv/graphql/api_schema.gql rename to priv/graphql/admin_schema.gql index eb76ece987a..74770686c5b 100644 --- a/priv/graphql/api_schema.gql +++ b/priv/graphql/admin_schema.gql @@ -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. @@ -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. diff --git a/priv/graphql/root_schema.gql b/priv/graphql/root_schema.gql new file mode 100644 index 00000000000..93df85652ff --- /dev/null +++ b/priv/graphql/root_schema.gql @@ -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 +} diff --git a/priv/graphql/user_schema.gql b/priv/graphql/user_schema.gql new file mode 100644 index 00000000000..4e7c4a6facb --- /dev/null +++ b/priv/graphql/user_schema.gql @@ -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 +} diff --git a/src/mongoose_graphql.erl b/src/mongoose_graphql.erl index a74a71b8ab4..8802a076f3f 100644 --- a/src/mongoose_graphql.erl +++ b/src/mongoose_graphql.erl @@ -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(), @@ -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} -> @@ -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).