View Source AbsintheUtils.Middleware.ArgLoader (absinthe_utils v0.0.2)
Absinthe middleware for loading entities in field
arguments.
This middleware should be defined before resolve
. It will manipulate the arguments
before they are passed to the resolver function.
As configuration it accepts a map of original argument names to a keyword list, containing:
load_function
: the function used to load the argument into an entity. As an input accepts one single argument: the input received in the resolution. The function should return the entity, ornil
when not found.new_name
: the new name to push the loaded entity into. (optional, defaults to the original argument name).
example
Example
query do
field :user, :user do
arg(:id, :id)
# Add the before before your resolver
middleware(
ArgLoader,
%{
id: [
new_name: :user,
load_function: &get_user_by_id/1
]
}
)
resolve(fn _, arguments, _ ->
{:ok, Map.get(arguments, :user)}
end)
end
This will define a user
query that accepts an id
input. Before calling the resolver,
it will load the user via get_user_by_id(id)
into the user
argument, removing id
.