1 |
|
%% @doc Management and execution of administration commands with GraphQL API |
2 |
|
|
3 |
|
-module(mongoose_graphql_commands). |
4 |
|
|
5 |
|
%% API |
6 |
|
-export([start/0, stop/0, process/1]). |
7 |
|
|
8 |
|
%% Internal API |
9 |
|
-export([wrap_type/2]). |
10 |
|
|
11 |
|
%% Only for tests |
12 |
|
-export([build_specs/1, get_specs/0]). |
13 |
|
|
14 |
|
-ignore_xref([build_specs/1, get_specs/0]). |
15 |
|
|
16 |
|
% Needed to get the 'agent' vCard Fields inside a vCard |
17 |
|
-define(MAX_TYPE_RECURSION_DEPTH, 2). |
18 |
|
|
19 |
|
% Needed to handle e.g. [String!]!, which has 3 wrapper types: NON_NULL, LIST, NON_NULL |
20 |
|
-define(MAX_INTROSPECTION_DEPTH, 3). |
21 |
|
|
22 |
|
-type context() :: #{args := [string()], |
23 |
|
category => category(), |
24 |
|
commands => command_map(), |
25 |
|
command => command(), |
26 |
|
args_spec => [arg_spec()], |
27 |
|
doc => doc(), |
28 |
|
vars => json_map(), |
29 |
|
reason => atom() | tuple(), |
30 |
|
result => result(), |
31 |
|
status => executed | error | usage}. |
32 |
|
-type result() :: {ok, #{atom() => graphql:json()}} | {error, any()}. |
33 |
|
-type specs() :: #{category() => category_spec()}. |
34 |
|
-type category() :: binary(). |
35 |
|
-type category_spec() :: #{desc := binary(), commands := command_map()}. |
36 |
|
-type command_map() :: #{command() => command_spec()}. |
37 |
|
-type command() :: binary(). |
38 |
|
-type command_spec() :: #{desc := binary(), |
39 |
|
op_type := op_type(), |
40 |
|
args := [arg_spec()], |
41 |
|
fields := [field_spec()], |
42 |
|
doc := doc()}. |
43 |
|
-type arg_spec() :: #{name := binary(), type := binary(), kind := binary(), wrap := [list | required]}. |
44 |
|
-type field_spec() :: #{name | on := binary(), fields => [field_spec()]}. |
45 |
|
-type op_type() :: binary(). |
46 |
|
-type doc() :: binary(). |
47 |
|
-type ep() :: graphql:endpoint_context(). |
48 |
|
-type json_map() :: #{binary() => graphql:json()}. |
49 |
|
|
50 |
|
-export_type([category/0, command/0, command_map/0, arg_spec/0, context/0]). |
51 |
|
|
52 |
|
%% API |
53 |
|
|
54 |
|
-spec start() -> ok. |
55 |
|
start() -> |
56 |
53 |
Specs = build_specs(admin), |
57 |
53 |
persistent_term:put(?MODULE, Specs). |
58 |
|
|
59 |
|
-spec stop() -> ok. |
60 |
|
stop() -> |
61 |
53 |
persistent_term:erase(?MODULE), |
62 |
53 |
ok. |
63 |
|
|
64 |
|
%% The returned context has 'status' with the following values: |
65 |
|
%% - 'executed' means that a GraphQL command was called, and 'result' contains the returned value |
66 |
|
%% - 'error' means that the arguments were incorrect, and 'reason' contains more information |
67 |
|
%% - 'usage' means that help needs to be displayed |
68 |
|
-spec process([string()]) -> context(). |
69 |
|
process(Args) -> |
70 |
642 |
lists:foldl(fun(_, #{status := _} = Ctx) -> Ctx; |
71 |
3176 |
(StepF, Ctx) -> StepF(Ctx) |
72 |
|
end, #{args => Args}, steps()). |
73 |
|
|
74 |
|
%% Internal API |
75 |
|
|
76 |
|
-spec build_specs(atom()) -> specs(). |
77 |
|
build_specs(EpName) -> |
78 |
135 |
Ep = mongoose_graphql:get_endpoint(EpName), |
79 |
135 |
CatSpecs = get_category_specs(Ep), |
80 |
135 |
lists:foldl(fun({Category, CategorySpec}, Acc) -> |
81 |
4287 |
insert_category(Category, CategorySpec, Acc) |
82 |
|
end, #{}, CatSpecs). |
83 |
|
|
84 |
|
-spec get_specs() -> specs(). |
85 |
|
get_specs() -> |
86 |
644 |
persistent_term:get(?MODULE). |
87 |
|
|
88 |
|
%% Internals |
89 |
|
|
90 |
|
steps() -> |
91 |
642 |
[fun find_category/1, fun find_command/1, fun parse_args/1, fun check_args/1, fun execute/1]. |
92 |
|
|
93 |
|
-spec find_category(context()) -> context(). |
94 |
|
find_category(CtxIn = #{args := [CategoryStr | Args]}) -> |
95 |
641 |
Category = list_to_binary(CategoryStr), |
96 |
641 |
Ctx = CtxIn#{category => Category, args => Args}, |
97 |
641 |
case get_specs() of |
98 |
|
#{Category := #{commands := Commands}} -> |
99 |
639 |
Ctx#{commands => Commands}; |
100 |
|
#{} -> |
101 |
2 |
Ctx#{status => error, reason => unknown_category} |
102 |
|
end; |
103 |
|
find_category(Ctx = #{args := []}) -> |
104 |
1 |
Ctx#{status => error, reason => no_args}. |
105 |
|
|
106 |
|
-spec find_command(context()) -> context(). |
107 |
|
find_command(CtxIn = #{args := [CommandStr | Args]}) -> |
108 |
638 |
Command = list_to_binary(CommandStr), |
109 |
638 |
Ctx = #{commands := Commands} = CtxIn#{command => Command, args => Args}, |
110 |
638 |
case Commands of |
111 |
|
#{Command := CommandSpec} -> |
112 |
636 |
#{doc := Doc, args := ArgSpec} = CommandSpec, |
113 |
636 |
Ctx#{doc => Doc, args_spec => ArgSpec}; |
114 |
|
#{} -> |
115 |
2 |
Ctx#{status => error, reason => unknown_command} |
116 |
|
end; |
117 |
|
find_command(Ctx) -> |
118 |
1 |
Ctx#{status => usage}. |
119 |
|
|
120 |
|
-spec parse_args(context()) -> context(). |
121 |
|
parse_args(Ctx = #{args := ["--help"]}) -> |
122 |
1 |
Ctx#{status => usage}; |
123 |
|
parse_args(Ctx) -> |
124 |
635 |
parse_args_loop(Ctx#{vars => #{}}). |
125 |
|
|
126 |
|
parse_args_loop(Ctx = #{vars := Vars, |
127 |
|
args_spec := ArgsSpec, |
128 |
|
args := ["--" ++ ArgNameStr, ArgValueStr | Rest]}) -> |
129 |
1299 |
ArgName = list_to_binary(ArgNameStr), |
130 |
1299 |
case lists:filter(fun(#{name := Name}) -> Name =:= ArgName end, ArgsSpec) of |
131 |
|
[] -> |
132 |
1 |
Ctx#{status => error, reason => {unknown_arg, ArgName}}; |
133 |
|
[ArgSpec] -> |
134 |
1298 |
ArgValue = list_to_binary(ArgValueStr), |
135 |
1298 |
try parse_arg(ArgValue, ArgSpec) of |
136 |
|
ParsedValue -> |
137 |
1297 |
NewVars = Vars#{ArgName => ParsedValue}, |
138 |
1297 |
parse_args_loop(Ctx#{vars := NewVars, args := Rest}) |
139 |
|
catch _:_ -> |
140 |
1 |
Ctx#{status => error, reason => {invalid_arg_value, ArgName, ArgValue}} |
141 |
|
end |
142 |
|
end; |
143 |
|
parse_args_loop(Ctx = #{args := []}) -> |
144 |
631 |
Ctx; |
145 |
|
parse_args_loop(Ctx) -> |
146 |
2 |
Ctx#{status => error, reason => invalid_args}. |
147 |
|
|
148 |
|
-spec parse_arg(binary(), arg_spec()) -> jiffy:json_value(). |
149 |
|
parse_arg(Value, ArgSpec = #{type := Type}) -> |
150 |
1298 |
case is_json_arg(ArgSpec) of |
151 |
|
true -> |
152 |
84 |
jiffy:decode(Value, [return_maps]); |
153 |
|
false -> |
154 |
1214 |
convert_input_type(Type, Value) |
155 |
|
end. |
156 |
|
|
157 |
|
%% Used input types that are not parsed from binaries should be handled here |
158 |
|
convert_input_type(Type, Value) when Type =:= <<"Int">>; |
159 |
|
Type =:= <<"PosInt">>; |
160 |
51 |
Type =:= <<"NonNegInt">> -> binary_to_integer(Value); |
161 |
1163 |
convert_input_type(_, Value) -> Value. |
162 |
|
|
163 |
|
%% Complex argument values should be provided in JSON |
164 |
|
-spec is_json_arg(arg_spec()) -> boolean(). |
165 |
31 |
is_json_arg(#{kind := <<"INPUT_OBJECT">>}) -> true; |
166 |
|
is_json_arg(#{kind := Kind, wrap := Wrap}) when Kind =:= <<"SCALAR">>; |
167 |
|
Kind =:= <<"ENUM">> -> |
168 |
1267 |
lists:member(list, Wrap). |
169 |
|
|
170 |
|
-spec check_args(context()) -> context(). |
171 |
|
check_args(Ctx = #{args_spec := ArgsSpec, vars := Vars}) -> |
172 |
631 |
MissingArgs = [Name || #{name := Name, wrap := [required|_]} <- ArgsSpec, |
173 |
1091 |
not maps:is_key(Name, Vars)], |
174 |
631 |
case MissingArgs of |
175 |
628 |
[] -> Ctx; |
176 |
3 |
_ -> Ctx#{status => error, reason => {missing_args, MissingArgs}} |
177 |
|
end. |
178 |
|
|
179 |
|
-spec execute(context()) -> context(). |
180 |
|
execute(#{doc := Doc, vars := Vars} = Ctx) -> |
181 |
628 |
Ctx#{status => executed, result => execute(mongoose_graphql:get_endpoint(admin), Doc, Vars)}. |
182 |
|
|
183 |
|
-spec get_category_specs(ep()) -> [{category(), category_spec()}]. |
184 |
|
get_category_specs(Ep) -> |
185 |
135 |
lists:flatmap(fun(OpType) -> get_category_specs(Ep, OpType) end, op_types()). |
186 |
|
|
187 |
|
get_category_specs(Ep, OpType) -> |
188 |
405 |
OpTypeName = <<OpType/binary, "Type">>, |
189 |
405 |
Doc = iolist_to_binary(["{ __schema { ", OpTypeName, " ", category_spec_query(), " } }"]), |
190 |
405 |
{ok, #{data := #{<<"__schema">> := Schema}}} = mongoose_graphql:execute(Ep, undefined, Doc), |
191 |
405 |
#{OpTypeName := #{<<"fields">> := Categories}} = Schema, |
192 |
405 |
get_category_specs(Ep, OpType, Categories). |
193 |
|
|
194 |
|
op_types() -> |
195 |
135 |
[<<"query">>, <<"mutation">>, <<"subscription">>]. |
196 |
|
|
197 |
|
-spec get_category_specs(ep(), op_type(), [json_map()]) -> [{category(), category_spec()}]. |
198 |
|
get_category_specs(Ep, OpType, Categories) -> |
199 |
405 |
[get_category_spec(Ep, OpType, Category) || Category <- Categories, is_category(Category)]. |
200 |
|
|
201 |
|
is_category(#{<<"name">> := <<"checkAuth">>}) -> |
202 |
135 |
false; |
203 |
|
is_category(#{}) -> |
204 |
4287 |
true. |
205 |
|
|
206 |
|
-spec get_category_spec(ep(), op_type(), json_map()) -> {category(), category_spec()}. |
207 |
|
get_category_spec(Ep, OpType, #{<<"name">> := Category, <<"description">> := Desc, |
208 |
|
<<"type">> := #{<<"name">> := CategoryType}}) -> |
209 |
4287 |
Doc = iolist_to_binary( |
210 |
|
["query ($type: String!) { __type(name: $type) " |
211 |
|
"{name fields {name description args {name type ", arg_type_query(), "} type ", |
212 |
|
field_type_query(), "}}}"]), |
213 |
4287 |
Vars = #{<<"type">> => CategoryType}, |
214 |
4287 |
{ok, #{data := #{<<"__type">> := #{<<"fields">> := Commands}}}} = execute(Ep, Doc, Vars), |
215 |
4287 |
CommandSpecs = [get_command_spec(Ep, Category, OpType, Command) || Command <- Commands], |
216 |
4287 |
{Category, #{desc => Desc, commands => maps:from_list(CommandSpecs)}}. |
217 |
|
|
218 |
|
-spec get_command_spec(ep(), category(), op_type(), json_map()) -> {command(), command_spec()}. |
219 |
|
get_command_spec(Ep, Category, OpType, |
220 |
|
#{<<"name">> := Name, <<"args">> := Args, <<"type">> := TypeMap} = Map) -> |
221 |
14820 |
Spec = #{op_type => OpType, args => get_args(Args), fields => get_fields(Ep, TypeMap, [])}, |
222 |
14820 |
Doc = prepare_doc(Category, Name, Spec), |
223 |
14820 |
{Name, add_description(Spec#{doc => Doc}, Map)}. |
224 |
|
|
225 |
|
add_description(Spec, #{<<"description">> := Desc}) -> |
226 |
14820 |
Spec#{desc => Desc}; |
227 |
|
add_description(Spec, #{}) -> |
228 |
:-( |
Spec. |
229 |
|
|
230 |
|
-spec get_args([json_map()]) -> [arg_spec()]. |
231 |
|
get_args(Args) -> |
232 |
14820 |
lists:map(fun get_arg_info/1, Args). |
233 |
|
|
234 |
|
-spec get_arg_info(json_map()) -> arg_spec(). |
235 |
|
get_arg_info(#{<<"name">> := ArgName, <<"type">> := Arg}) -> |
236 |
27871 |
(get_arg_type(Arg, []))#{name => ArgName}. |
237 |
|
|
238 |
|
get_arg_type(#{<<"kind">> := <<"NON_NULL">>, <<"ofType">> := TypeMap}, Wrap) -> |
239 |
22369 |
get_arg_type(TypeMap, [required | Wrap]); |
240 |
|
get_arg_type(#{<<"kind">> := <<"LIST">>, <<"ofType">> := TypeMap}, Wrap) -> |
241 |
1899 |
get_arg_type(TypeMap, [list | Wrap]); |
242 |
|
get_arg_type(#{<<"name">> := Type, <<"kind">> := Kind}, Wrap) when Kind =:= <<"SCALAR">>; |
243 |
|
Kind =:= <<"ENUM">>; |
244 |
|
Kind =:= <<"INPUT_OBJECT">> -> |
245 |
27871 |
#{type => Type, kind => Kind, wrap => lists:reverse(Wrap)}. |
246 |
|
|
247 |
|
-spec get_fields(ep(), json_map(), [binary()]) -> [field_spec()]. |
248 |
|
get_fields(_Ep, #{<<"kind">> := Kind}, _Path) |
249 |
|
when Kind =:= <<"SCALAR">>; |
250 |
76007 |
Kind =:= <<"ENUM">> -> []; |
251 |
|
get_fields(Ep, #{<<"kind">> := <<"UNION">>, <<"possibleTypes">> := TypeMaps}, Path) -> |
252 |
2402 |
[get_union_type(Ep, TypeMap, Path) || TypeMap <- TypeMaps]; |
253 |
|
get_fields(Ep, #{<<"kind">> := Kind, <<"ofType">> := Type}, Path) |
254 |
|
when Kind =:= <<"NON_NULL">>; |
255 |
|
Kind =:= <<"LIST">> -> |
256 |
46963 |
get_fields(Ep, Type, Path); |
257 |
|
get_fields(Ep, #{<<"kind">> := <<"OBJECT">>, <<"name">> := Type}, Path) -> |
258 |
21254 |
case length([T || T <- Path, T =:= Type]) >= ?MAX_TYPE_RECURSION_DEPTH of |
259 |
|
true -> |
260 |
270 |
[#{name => <<"__typename">>}]; % inform about the type of the trimmed subtree |
261 |
|
false -> |
262 |
20984 |
Fields = get_object_fields(Ep, Type), |
263 |
20984 |
[get_field(Ep, Field, [Type | Path]) || Field <- Fields] |
264 |
|
end. |
265 |
|
|
266 |
|
-spec get_union_type(ep(), json_map(), [binary()]) -> field_spec(). |
267 |
|
get_union_type(Ep, #{<<"kind">> := <<"OBJECT">>, <<"name">> := Type} = M, Path) -> |
268 |
6433 |
#{on => Type, fields => get_fields(Ep, M, Path)}. |
269 |
|
|
270 |
|
-spec get_field(ep(), json_map(), [binary()]) -> field_spec(). |
271 |
|
get_field(Ep, #{<<"type">> := Type, <<"name">> := Name}, Path) -> |
272 |
78410 |
case get_fields(Ep, Type, Path) of |
273 |
68462 |
[] -> #{name => Name}; |
274 |
9948 |
Fields -> #{name => Name, fields => Fields} |
275 |
|
end. |
276 |
|
|
277 |
|
-spec get_object_fields(ep(), binary()) -> [json_map()]. |
278 |
|
get_object_fields(Ep, ObjectType) -> |
279 |
20984 |
Doc = iolist_to_binary(["query ($type: String!) { __type(name: $type) " |
280 |
|
"{name fields {name type ", field_type_query(), "}}}"]), |
281 |
20984 |
Vars = #{<<"type">> => ObjectType}, |
282 |
20984 |
{ok, #{data := #{<<"__type">> := #{<<"fields">> := Fields}}}} = execute(Ep, Doc, Vars), |
283 |
20984 |
Fields. |
284 |
|
|
285 |
|
-spec insert_category(category(), category_spec(), specs()) -> specs(). |
286 |
|
insert_category(Category, NewCatSpec = #{commands := NewCommands}, Specs) -> |
287 |
4287 |
case Specs of |
288 |
|
#{Category := #{desc := OldDesc, commands := OldCommands}} -> |
289 |
1699 |
case maps:with(maps:keys(OldCommands), NewCommands) of |
290 |
|
Common when Common =:= #{} -> |
291 |
1699 |
Specs#{Category := #{desc => OldDesc, |
292 |
|
commands => maps:merge(OldCommands, NewCommands)}}; |
293 |
|
Common -> |
294 |
:-( |
error(#{what => overlapping_graphql_commands, |
295 |
|
text => <<"GraphQL query and mutation names are not unique">>, |
296 |
|
category => Category, |
297 |
|
commands => maps:keys(Common)}) |
298 |
|
end; |
299 |
|
_ -> |
300 |
2588 |
Specs#{Category => NewCatSpec} |
301 |
|
end. |
302 |
|
|
303 |
|
-spec prepare_doc(category(), command(), map()) -> doc(). |
304 |
|
prepare_doc(Category, Command, #{op_type := OpType, args := Args, fields := Fields}) -> |
305 |
14820 |
iolist_to_binary([OpType, " ", declare_variables(Args), "{ ", Category, " { ", Command, |
306 |
|
use_variables(Args), return_fields(Fields), " } }"]). |
307 |
|
|
308 |
|
-spec declare_variables([arg_spec()]) -> iolist(). |
309 |
1243 |
declare_variables([]) -> ""; |
310 |
|
declare_variables(Args) -> |
311 |
13577 |
["(", lists:join(", ", lists:map(fun declare_variable/1, Args)), ") "]. |
312 |
|
|
313 |
|
-spec declare_variable(arg_spec()) -> iolist(). |
314 |
|
declare_variable(#{name := Name, type := Type, wrap := Wrap}) -> |
315 |
27871 |
["$", Name, ": ", wrap_type(Wrap, Type)]. |
316 |
|
|
317 |
|
-spec wrap_type([required | list], binary()) -> iolist(). |
318 |
|
wrap_type([required | Wrap], Type) -> |
319 |
22381 |
[wrap_type(Wrap, Type), $!]; |
320 |
|
wrap_type([list | Wrap], Type) -> |
321 |
1901 |
[$[, wrap_type(Wrap, Type), $]]; |
322 |
|
wrap_type([], Type) -> |
323 |
27881 |
[Type]. |
324 |
|
|
325 |
|
-spec use_variables([arg_spec()]) -> iolist(). |
326 |
1243 |
use_variables([]) -> ""; |
327 |
|
use_variables(Args) -> |
328 |
13577 |
["(", lists:join(", ", lists:map(fun use_variable/1, Args)), ")"]. |
329 |
|
|
330 |
|
-spec use_variable(arg_spec()) -> iolist(). |
331 |
|
use_variable(#{name := Name}) -> |
332 |
27871 |
[Name, ": $", Name]. |
333 |
|
|
334 |
|
-spec return_fields([field_spec()]) -> iolist(). |
335 |
7545 |
return_fields([]) -> ""; |
336 |
|
return_fields(Fields) -> |
337 |
23656 |
[" { ", lists:join(" ", [return_field(F) || F <- Fields]), " }"]. |
338 |
|
|
339 |
|
-spec return_field(field_spec()) -> iodata(). |
340 |
|
return_field(#{name := Name, fields := Fields}) -> |
341 |
9948 |
[Name, return_fields(Fields)]; |
342 |
|
return_field(#{name := Name}) -> |
343 |
68732 |
Name; |
344 |
|
return_field(#{on := Type, fields := Fields}) -> |
345 |
6433 |
["... on ", Type, return_fields(Fields)]. |
346 |
|
|
347 |
|
-spec execute(ep(), doc(), json_map()) -> result(). |
348 |
|
execute(Ep, Doc, Vars) -> |
349 |
25899 |
mongoose_graphql:execute(Ep, #{document => Doc, |
350 |
|
operation_name => undefined, |
351 |
|
vars => Vars, |
352 |
|
authorized => true, |
353 |
|
ctx => #{method => cli}}). |
354 |
|
|
355 |
|
field_type_query() -> |
356 |
25271 |
nested_type_query("name kind possibleTypes {name kind}"). |
357 |
|
|
358 |
|
arg_type_query() -> |
359 |
4287 |
nested_type_query("name kind"). |
360 |
|
|
361 |
|
nested_type_query(BasicQuery) -> |
362 |
29558 |
lists:foldl(fun(_, QueryAcc) -> ["{ ", BasicQuery, " ofType ", QueryAcc, " }"] end, |
363 |
|
["{ ", BasicQuery, " }"], lists:seq(1, ?MAX_INTROSPECTION_DEPTH)). |
364 |
|
|
365 |
|
category_spec_query() -> |
366 |
405 |
"{name fields {name description type {name fields {name}}}}". |