1 |
|
-module(mongoose_graphql_helper). |
2 |
|
|
3 |
|
-export([null_to_default/2, null_to_undefined/1, undefined_to_null/1]). |
4 |
|
|
5 |
|
-export([format_result/2, make_error/2, make_error/3]). |
6 |
|
|
7 |
|
-include("mongoose_graphql_types.hrl"). |
8 |
|
|
9 |
|
-spec format_result(InResult, Context) -> OutResult when |
10 |
|
InResult :: {atom(), iodata() | integer()}, |
11 |
|
Context :: map(), |
12 |
|
OutResult :: {ok, binary() | integer()} | {error, resolver_error()}. |
13 |
|
format_result(Result, Context) -> |
14 |
320 |
case Result of |
15 |
33 |
{ok, Val} when is_integer(Val) orelse is_map(Val) -> {ok, Val}; |
16 |
170 |
{ok, Msg} -> {ok, iolist_to_binary(Msg)}; |
17 |
117 |
{ErrCode, Msg} -> make_error(ErrCode, Msg, Context) |
18 |
|
end. |
19 |
|
|
20 |
|
-spec make_error({atom(), iodata()}, map()) -> {error, resolver_error()}. |
21 |
|
make_error({Reason, Msg}, Context) -> |
22 |
202 |
make_error(Reason, Msg, Context). |
23 |
|
|
24 |
|
-spec make_error(atom(), iodata(), map()) -> {error, resolver_error()}. |
25 |
|
make_error(Reason, Msg, Context) -> |
26 |
450 |
{error, #resolver_error{reason = Reason, msg = iolist_to_binary(Msg), context = Context}}. |
27 |
|
|
28 |
|
null_to_default(null, Default) -> |
29 |
59 |
Default; |
30 |
|
null_to_default(Value, _Default) -> |
31 |
116 |
Value. |
32 |
|
|
33 |
60 |
null_to_undefined(null) -> undefined; |
34 |
28 |
null_to_undefined(V) -> V. |
35 |
|
|
36 |
6 |
undefined_to_null(undefined) -> null; |
37 |
62 |
undefined_to_null(V) -> V. |