1 |
|
-module(mongoose_graphql_check_categories). |
2 |
|
|
3 |
|
-export([process_ast/2]). |
4 |
|
|
5 |
|
-include_lib("graphql/src/graphql_schema.hrl"). |
6 |
|
-include_lib("graphql/src/graphql_internal.hrl"). |
7 |
|
-include_lib("graphql/include/graphql.hrl"). |
8 |
|
-include_lib("jid/include/jid.hrl"). |
9 |
|
|
10 |
|
-type document() :: #document{}. |
11 |
|
-type categories() :: [binary()]. |
12 |
|
|
13 |
|
-include("mongoose.hrl"). |
14 |
|
|
15 |
|
-spec process_ast(document(), categories()) -> document(). |
16 |
|
process_ast(#document{definitions = Definitions} = Document, Categories) -> |
17 |
10584 |
case Categories of |
18 |
|
[] -> |
19 |
10584 |
Document; |
20 |
|
_ -> |
21 |
:-( |
Definitions2 = lists:map(fun(#op{schema = Schema} = Op) -> |
22 |
:-( |
parse_schema(Schema, Op, Categories) |
23 |
|
end, Definitions), |
24 |
:-( |
#document{definitions = Definitions2} |
25 |
|
end. |
26 |
|
|
27 |
|
parse_schema(#object_type{fields = Fields} = Schema, Op, Categories) -> |
28 |
:-( |
Fields2 = maps:map(fun(Key, Value) -> |
29 |
:-( |
case lists:member(Key, Categories) of |
30 |
:-( |
true -> Value; |
31 |
|
false -> |
32 |
:-( |
case Value of |
33 |
|
#schema_field{resolve = undefined} -> |
34 |
:-( |
Fun = category_disabled_fun(Key), |
35 |
:-( |
Value#schema_field{resolve = Fun}; |
36 |
|
_ -> |
37 |
:-( |
Value |
38 |
|
end |
39 |
|
end |
40 |
|
end, Fields), |
41 |
:-( |
Schema2 = Schema#object_type{fields = Fields2}, |
42 |
:-( |
Op#op{schema = Schema2}; |
43 |
|
parse_schema(_, Op, _) -> |
44 |
:-( |
Op. |
45 |
|
|
46 |
|
-spec category_disabled_fun(binary()) -> resolver(). |
47 |
|
category_disabled_fun(Category) -> |
48 |
:-( |
Msg = <<"Category disabled">>, |
49 |
:-( |
Extra = #{category => Category}, |
50 |
:-( |
fun(_, _, _, _) -> mongoose_graphql_helper:make_error(category_disabled, Msg, Extra) end. |