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 |
29633 |
case Categories of |
18 |
|
[] -> |
19 |
29630 |
Document; |
20 |
|
_ -> |
21 |
3 |
Definitions2 = lists:map(fun(#op{schema = Schema} = Op) -> |
22 |
3 |
parse_schema(Schema, Op, Categories) |
23 |
|
end, Definitions), |
24 |
3 |
#document{definitions = Definitions2} |
25 |
|
end. |
26 |
|
|
27 |
|
parse_schema(#object_type{fields = Fields} = Schema, Op, Categories) -> |
28 |
3 |
Fields2 = maps:map(fun(Key, Value) -> |
29 |
57 |
case lists:member(Key, Categories) of |
30 |
6 |
true -> Value; |
31 |
|
false -> |
32 |
51 |
case Value of |
33 |
|
#schema_field{resolve = undefined} -> |
34 |
45 |
Fun = category_disabled_fun(Key), |
35 |
45 |
Value#schema_field{resolve = Fun}; |
36 |
|
_ -> |
37 |
6 |
Value |
38 |
|
end |
39 |
|
end |
40 |
|
end, Fields), |
41 |
3 |
Schema2 = Schema#object_type{fields = Fields2}, |
42 |
3 |
Op#op{schema = Schema2}; |
43 |
|
parse_schema(_, Op, _) -> |
44 |
:-( |
Op. |
45 |
|
|
46 |
|
-spec category_disabled_fun(binary()) -> resolver(). |
47 |
|
category_disabled_fun(Category) -> |
48 |
45 |
Msg = <<"Category disabled">>, |
49 |
45 |
Extra = #{category => Category}, |
50 |
45 |
fun(_, _, _, _) -> mongoose_graphql_helper:make_error(category_disabled, Msg, Extra) end. |