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 |
543 |
case Result of |
15 |
133 |
{ok, Val} when is_integer(Val) orelse is_map(Val) -> {ok, Val}; |
16 |
225 |
{ok, Msg} -> {ok, iolist_to_binary(Msg)}; |
17 |
185 |
{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 |
231 |
make_error(Reason, Msg, Context). |
23 |
|
|
24 |
|
-spec make_error(atom(), iodata(), map()) -> {error, resolver_error()}. |
25 |
|
make_error(Reason, Msg, Context) -> |
26 |
588 |
{error, #resolver_error{reason = Reason, msg = iolist_to_binary(Msg), context = Context}}. |
27 |
|
|
28 |
|
null_to_default(null, Default) -> |
29 |
106 |
Default; |
30 |
|
null_to_default(Value, _Default) -> |
31 |
183 |
Value. |
32 |
|
|
33 |
151 |
null_to_undefined(null) -> undefined; |
34 |
80 |
null_to_undefined(V) -> V. |
35 |
|
|
36 |
9 |
undefined_to_null(undefined) -> null; |
37 |
87 |
undefined_to_null(V) -> V. |