Packages

Code First GraphQL library for Gleam

Current section

Files

Jump to
mochi src mochi@args.erl
Raw

src/mochi@args.erl

-module(mochi@args).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/mochi/args.gleam").
-export([error_message/1, from_dict/1, to_dict/1, decode_input/3, get_string/2, get_id/2, get_int/2, get_float/2, get_bool/2, get_string_list/2, get_int_list/2, get_optional_string/2, get_optional_int/2, get_optional_float/2, get_optional_bool/2]).
-export_type([args/0, arg_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Typed accessor for GraphQL field arguments.\n"
"\n"
" Resolvers receive arguments as `Args`, an opaque wrapper around a\n"
" `Dict(String, Dynamic)`. Use the `get_*` family to read fields with\n"
" type checking; `to_dict` is a back-compat escape hatch.\n"
).
-opaque args() :: {args, gleam@dict:dict(binary(), gleam@dynamic:dynamic_())}.
-type arg_error() :: {missing, binary()} |
{wrong_type, binary(), binary()} |
{invalid_input, binary()}.
-file("src/mochi/args.gleam", 30).
?DOC(
" Render an `ArgError` as the same kind of message the legacy\n"
" `query.get_*` family produced, so downstream error reporting doesn't\n"
" change.\n"
).
-spec error_message(arg_error()) -> binary().
error_message(Err) ->
case Err of
{missing, Key} ->
<<"Missing required argument: "/utf8, Key/binary>>;
{wrong_type, Key@1, Expected} ->
<<<<<<"Invalid type for argument '"/utf8, Key@1/binary>>/binary,
"': expected "/utf8>>/binary,
Expected/binary>>;
{invalid_input, Key@2} ->
<<<<"Invalid input for '"/utf8, Key@2/binary>>/binary, "'"/utf8>>
end.
-file("src/mochi/args.gleam", 41).
?DOC(
" Wrap a raw dict as `Args`. The executor calls this at the resolver\n"
" boundary; user code rarely needs it directly.\n"
).
-spec from_dict(gleam@dict:dict(binary(), gleam@dynamic:dynamic_())) -> args().
from_dict(D) ->
{args, D}.
-file("src/mochi/args.gleam", 47).
?DOC(
" Unwrap to the underlying dict — for code bridging to APIs that still\n"
" take `Dict(String, Dynamic)`. Avoid in new code.\n"
).
-spec to_dict(args()) -> gleam@dict:dict(binary(), gleam@dynamic:dynamic_()).
to_dict(A) ->
erlang:element(2, A).
-file("src/mochi/args.gleam", 95).
-spec decode_input(args(), binary(), gleam@dynamic@decode:decoder(DQR)) -> {ok,
DQR} |
{error, arg_error()}.
decode_input(A, Key, Decoder) ->
case gleam_stdlib:map_get(erlang:element(2, A), Key) of
{error, _} ->
{error, {missing, Key}};
{ok, Value} ->
_pipe = gleam@dynamic@decode:run(Value, Decoder),
gleam@result:map_error(_pipe, fun(_) -> {invalid_input, Key} end)
end.
-file("src/mochi/args.gleam", 108).
-spec required(args(), binary(), gleam@dynamic@decode:decoder(DQV), binary()) -> {ok,
DQV} |
{error, arg_error()}.
required(A, Key, Decoder, Type_label) ->
case gleam_stdlib:map_get(erlang:element(2, A), Key) of
{error, _} ->
{error, {missing, Key}};
{ok, Value} ->
_pipe = gleam@dynamic@decode:run(Value, Decoder),
gleam@result:map_error(
_pipe,
fun(_) -> {wrong_type, Key, Type_label} end
)
end.
-file("src/mochi/args.gleam", 51).
-spec get_string(args(), binary()) -> {ok, binary()} | {error, arg_error()}.
get_string(A, Key) ->
required(
A,
Key,
{decoder, fun gleam@dynamic@decode:decode_string/1},
<<"String"/utf8>>
).
-file("src/mochi/args.gleam", 55).
-spec get_id(args(), binary()) -> {ok, binary()} | {error, arg_error()}.
get_id(A, Key) ->
required(
A,
Key,
{decoder, fun gleam@dynamic@decode:decode_string/1},
<<"ID"/utf8>>
).
-file("src/mochi/args.gleam", 59).
-spec get_int(args(), binary()) -> {ok, integer()} | {error, arg_error()}.
get_int(A, Key) ->
required(
A,
Key,
{decoder, fun gleam@dynamic@decode:decode_int/1},
<<"Int"/utf8>>
).
-file("src/mochi/args.gleam", 63).
-spec get_float(args(), binary()) -> {ok, float()} | {error, arg_error()}.
get_float(A, Key) ->
required(
A,
Key,
{decoder, fun gleam@dynamic@decode:decode_float/1},
<<"Float"/utf8>>
).
-file("src/mochi/args.gleam", 67).
-spec get_bool(args(), binary()) -> {ok, boolean()} | {error, arg_error()}.
get_bool(A, Key) ->
required(
A,
Key,
{decoder, fun gleam@dynamic@decode:decode_bool/1},
<<"Bool"/utf8>>
).
-file("src/mochi/args.gleam", 87).
-spec get_string_list(args(), binary()) -> {ok, list(binary())} |
{error, arg_error()}.
get_string_list(A, Key) ->
required(
A,
Key,
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
<<"[String]"/utf8>>
).
-file("src/mochi/args.gleam", 91).
-spec get_int_list(args(), binary()) -> {ok, list(integer())} |
{error, arg_error()}.
get_int_list(A, Key) ->
required(
A,
Key,
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_int/1}
),
<<"[Int]"/utf8>>
).
-file("src/mochi/args.gleam", 122).
-spec optional(args(), binary(), gleam@dynamic@decode:decoder(DQZ)) -> gleam@option:option(DQZ).
optional(A, Key, Decoder) ->
case gleam_stdlib:map_get(erlang:element(2, A), Key) of
{error, _} ->
none;
{ok, Value} ->
case gleam@dynamic@decode:run(Value, Decoder) of
{ok, V} ->
{some, V};
{error, _} ->
none
end
end.
-file("src/mochi/args.gleam", 71).
-spec get_optional_string(args(), binary()) -> gleam@option:option(binary()).
get_optional_string(A, Key) ->
optional(A, Key, {decoder, fun gleam@dynamic@decode:decode_string/1}).
-file("src/mochi/args.gleam", 75).
-spec get_optional_int(args(), binary()) -> gleam@option:option(integer()).
get_optional_int(A, Key) ->
optional(A, Key, {decoder, fun gleam@dynamic@decode:decode_int/1}).
-file("src/mochi/args.gleam", 79).
-spec get_optional_float(args(), binary()) -> gleam@option:option(float()).
get_optional_float(A, Key) ->
optional(A, Key, {decoder, fun gleam@dynamic@decode:decode_float/1}).
-file("src/mochi/args.gleam", 83).
-spec get_optional_bool(args(), binary()) -> gleam@option:option(boolean()).
get_optional_bool(A, Key) ->
optional(A, Key, {decoder, fun gleam@dynamic@decode:decode_bool/1}).