1 |
|
-module(mongoose_graphql_operations). |
2 |
|
|
3 |
|
-export([verify_operations/2]). |
4 |
|
|
5 |
|
-include_lib("graphql/src/graphql_schema.hrl"). |
6 |
|
-include_lib("graphql/src/graphql_internal.hrl"). |
7 |
|
|
8 |
|
verify_operations(Ctx, #document{definitions = Definitions}) -> |
9 |
37079 |
Method = maps:get(method, Ctx, undefined), |
10 |
37079 |
[verify_op_type(Method, op_type(Ty)) || #op{id = Id, ty = Ty} <- Definitions, |
11 |
37079 |
is_requested_op(Ctx, Id)], |
12 |
37077 |
ok. |
13 |
|
|
14 |
|
verify_op_type(Method, OpType) -> |
15 |
37079 |
case is_supported(Method, OpType) of |
16 |
|
true -> |
17 |
37077 |
ok; |
18 |
|
false -> |
19 |
2 |
Error = {unsupported_operation, Method, OpType}, |
20 |
2 |
graphql_err:abort([], verify, Error) |
21 |
|
end. |
22 |
|
|
23 |
4 |
is_supported(Method, subscription) -> Method =:= sse; |
24 |
37075 |
is_supported(Method, _) -> Method =/= sse. |
25 |
|
|
26 |
37079 |
is_requested_op(#{operation_name := undefined}, _) -> true; |
27 |
:-( |
is_requested_op(#{operation_name := OpName}, {name, _, Name}) -> OpName =:= Name. |
28 |
|
|
29 |
564 |
op_type(undefined) -> query; |
30 |
36515 |
op_type({OpType, _}) -> OpType. |