Packages
absinthe_relay
0.8.1
1.6.0
1.5.2
1.5.1
1.5.0
1.5.0-rc.0
1.5.0-beta.0
1.5.0-alpha.0
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.1
1.4.0-rc.0
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.3.0-rc.0
1.3.0-beta.1
1.3.0-beta.0
1.2.0
1.2.0-alpha0
1.2.0-alpha.3
1.2.0-alpha.2
1.2.0-alpha.1
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.1
0.8.0
Relay framework support for Absinthe
Current section
Files
Jump to
Current section
Files
lib/absinthe/relay/node/helpers.ex
defmodule Absinthe.Relay.Node.Helpers do
alias Absinthe.Relay.Node
@doc """
Wrap a resolver to parse node (global) ID arguments before it is executed.
## Examples
Parse a node (global) ID argument `:item_id` (which should be an ID for the
`:item` type)
```
resolve parsing_node_ids(&my_field_resolver/2, item_id: :item)
```
"""
def parsing_node_ids(resolver, id_keys) do
fn args, info ->
args = Enum.reduce(id_keys, args, fn {key, type}, args ->
with {:ok, global_id} <- Map.fetch(args, key),
{:ok, %{id: id, type: ^type}} <- Node.from_global_id(global_id, info.schema) do
{:success, Map.put(args, key, id)}
end
|> case do
{:ok, %{type: bad_type}} ->
# The user provided an ID for a different type of field,
# notify them in a normal GraphQL error response
{:error, "Invalid node type for argument `#{key}`; should be #{type}, was #{bad_type}"}
{:error, msg} ->
# A more serious error, eg, a missing type, notify
# the schema designer with an exception
raise ArgumentError, msg
{:success, args} ->
args
_ ->
args
end
end)
resolver.(args, info)
end
end
end