Packages
absinthe
1.4.2
1.11.0
1.10.2
1.10.1
1.10.0
1.9.1
1.9.0
1.8.0
1.7.11
1.7.10
1.7.9
1.7.8
1.7.7
1.7.6
1.7.5
1.7.4
1.7.3
1.7.2
1.7.1
1.7.0
1.6.8
1.6.7
retired
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.6.0-rc.1
1.6.0-rc.0
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.5.0-rc.5
1.5.0-rc.4
1.5.0-rc.3
1.5.0-rc.2
1.5.0-rc.1
1.5.0-rc.0
1.5.0-beta.2
1.5.0-beta.1
1.5.0-beta.0
1.5.0-alpha.4
1.5.0-alpha.3
1.5.0-alpha.2
1.5.0-alpha.1
1.5.0-alpha.0
1.4.16
1.4.15
1.4.14
1.4.13
1.4.12
1.4.11
1.4.10
1.4.9
1.4.8
retired
1.4.7
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.3
1.4.0-rc.2
1.4.0-rc.1
1.4.0-rc.0
1.4.0-beta.5
1.4.0-beta.4
1.4.0-beta.3
1.4.0-beta.2
1.4.0-beta.1
1.3.2
1.3.1
1.3.0
1.3.0-rc.0
1.3.0-beta.2
1.3.0-beta.1
1.3.0-beta.0
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.2.0-rc.0
1.2.0-beta.0
1.2.0-alpha0
1.2.0-alpha.2
1.2.0-alpha.1
1.1.11
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
0.5.2
0.5.1
0.5.0
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.2.3
0.2.2
0.2.1
0.1.0
GraphQL for Elixir
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/absinthe/resolution.ex
defmodule Absinthe.Resolution do
@moduledoc """
The primary struct of resolution.
In many ways like the `%Conn{}` from `Plug`, the `%Absinthe.Resolution{}` is the
piece of information that passed along from middleware to middleware as part of
resolution.
"""
@typedoc """
Information about the current resolution. It is created by adding field specific
information to the more general `%Absinthe.Blueprint.Execution{}` struct.
## Contents
- `:adapter` - The adapter used for any name conversions.
- `:definition` - The Blueprint definition for this field.
- `:context` - The context passed to `Absinthe.run`.
- `:root_value` - The root value passed to `Absinthe.run`, if any.
- `:parent_type` - The parent type for the field.
- `:schema` - The current schema.
- `:source` - The resolved parent object; source of this field.
To access the schema type for this field, see the `definition.schema_node`.
"""
@type arguments :: %{optional(atom) => any}
@type source :: any
@type t :: %__MODULE__{
value: term,
errors: [term],
adapter: Absinthe.Adapter.t,
context: map,
root_value: any,
schema: Absinthe.Schema.t,
definition: Absinthe.Blueprint.node_t,
parent_type: Absinthe.Type.t,
source: source,
state: field_state,
acc: %{any => any},
extensions: %{any => any},
arguments: arguments,
fragments: [Absinthe.Blueprint.Document.Fragment.Named.t],
}
defstruct [
:value,
:adapter,
:context,
:parent_type,
:root_value,
:definition,
:schema,
:source,
errors: [],
middleware: [],
acc: %{},
arguments: %{},
extensions: %{},
private: %{},
path: [],
state: :unresolved,
fragments: [],
fields_cache: %{},
]
def resolver_spec(fun) do
{{__MODULE__, :call}, fun}
end
@type field_state :: :unresolved | :resolved | :suspended
@doc """
Get the child fields under the current field.
See `project/2` for details.
"""
def project(info) do
case info.definition.schema_node.type do
%Absinthe.Type.Interface{} ->
raise need_concrete_type_error()
%Absinthe.Type.Union{} ->
raise need_concrete_type_error()
schema_node ->
project(info, schema_node)
end
end
@doc """
Get the current path.
Each `Absinthe.Resolution` struct holds the current result path as a list of
blueprint nodes and indices. Usually however you don't need the full AST list
and instead just want the path that will eventually end up in the result.
For that, use this function.
## Examples
Given some query:
```
{users { email }}
```
If you called this function inside a resolver on the users email field it
returns a value like:
```elixir
resolve fn _, _, resolution ->
Absinthe.Resolution.path(resolution) #=> ["users", 5, "email"]
end
```
In this case `5` is the 0 based index in the list of users the field is currently
at.
"""
def path(%{path: path}) do
path
|> Enum.reverse
|> Enum.drop(1)
|> Enum.map(&field_name/1)
end
defp field_name(%{alias: nil, name: name}), do: name
defp field_name(%{alias: name}), do: name
defp field_name(%{name: name}), do: name
defp field_name(index), do: index
@doc """
Get the child fields under the current field.
## Example
Given a document like:
```
{ user { id name }}
```
```
field :user, :user do
resolve fn _, info ->
child_fields = Absinthe.Resolution.project(info) |> Enum.map(&(&1.name))
# ...
end
end
```
`child_fields` will be `["id", "name"]`.
It correctly handles fragments, so for example if you had the document:
```
{
user {
... on User {
id
}
... on Named {
name
}
}
}
```
you would still get a nice and simple `child_fields` that was `["id", "name"]`.
"""
def project(%{
definition: %{selections: selections},
path: path,
fields_cache: cache,
} = info, type) do
type = Absinthe.Schema.lookup_type(info.schema, type)
{fields, _} = Absinthe.Resolution.Projector.project(selections, type, path, cache, info)
fields
end
defp need_concrete_type_error() do
"""
You tried to project from a field that is an abstract type without concrete type information!
Use `project/2` instead of `project/1`, and supply the type yourself please!
"""
end
def call(%{state: :unresolved} = res, resolution_function) do
result = case resolution_function do
fun when is_function(fun, 2) ->
fun.(res.arguments, res)
fun when is_function(fun, 3) ->
fun.(res.source, res.arguments, res)
{mod, fun} ->
apply(mod, fun, [res.source, res.arguments, res])
_ ->
raise Absinthe.ExecutionError, """
Field resolve property must be a 2 arity anonymous function, 3 arity
anonymous function, or a `{Module, :function}` tuple.
Instead got: #{inspect resolution_function}
Info: #{inspect res}
"""
end
put_result(res, result)
end
def call(res, _), do: res
def path_string(%__MODULE__{path: path}) do
Enum.map(path, fn
%{name: name, alias: alias} ->
alias || name
%{schema_node: schema_node} ->
schema_node.name
end)
end
@doc """
Handy function for applying user function result tuples to a resolution struct
User facing functions generally return one of several tuples like `{:ok, val}`
or `{:error, reason}`. This function handles applying those various tuples
to the resolution struct.
The resolution state is updated depending on the tuple returned. `:ok` and
`:error` tuples set the state to `:resolved`, whereas middleware tuples set it
to `:unresolved`.
This is useful for middleware that wants to handle user facing functions, but
does not want to duplicate this logic.
"""
def put_result(res, {:ok, value}) do
%{res | state: :resolved, value: value}
end
def put_result(res, {:error, [{_, _} | _] = error_keyword}) do
%{res | state: :resolved, errors: [error_keyword]}
end
def put_result(res, {:error, errors}) do
%{res | state: :resolved, errors: List.wrap(errors)}
end
def put_result(res, {:plugin, module, opts}) do
put_result(res, {:middleware, module, opts})
end
def put_result(res, {:middleware, module, opts}) do
%{res | state: :unresolved, middleware: [{module, opts} | res.middleware]}
end
def put_result(res, result) do
raise result_error(result, res.definition, res.source)
end
@doc false
def result_error({:error, _} = value, field, source) do
result_error(
value, field, source,
"You're returning an :error tuple, but did you forget to include a `:message`\nkey in every custom error (map or keyword list)?"
)
end
def result_error(value, field, source) do
result_error(
value, field, source,
"Did you forget to return a valid `{:ok, any}` | `{:error, error_value}` tuple?"
)
end
@doc """
TODO: Deprecate
"""
def call(resolution_function, parent, args, field_info) do
case resolution_function do
fun when is_function(fun, 2) ->
fun.(args, field_info)
fun when is_function(fun, 3) ->
fun.(parent, args, field_info)
{mod, fun} ->
apply(mod, fun, [parent, args, field_info])
_ ->
raise Absinthe.ExecutionError, """
Field resolve property must be a 2 arity anonymous function, 3 arity
anonymous function, or a `{Module, :function}` tuple.
Instead got: #{inspect resolution_function}
Info: #{inspect field_info}
"""
end
end
def call(function, args, info) do
call(function, info.source, args, info)
end
@error_detail """
## For a data result
`{:ok, any}` result will do.
### Examples:
A simple integer result:
{:ok, 1}
Something more complex:
{:ok, %Model.Thing{some: %{complex: :data}}}
## For an error result
One or more errors for a field can be returned in a single `{:error, error_value}` tuple.
`error_value` can be:
- A simple error message string.
- A map containing `:message` key, plus any additional serializable metadata.
- A keyword list containing a `:message` key, plus any additional serializable metadata.
- A list containing multiple of any/all of these.
- Any other value compatible with `to_string/1`.
### Examples
A simple error message:
{:error, "Something bad happened"}
Multiple error messages:
{:error, ["Something bad", "Even worse"]
Single custom errors (note the required `:message` keys):
{:error, message: "Unknown user", code: 21}
{:error, %{message: "A database error occurred", details: format_db_error(some_value)}}
Three errors of mixed types:
{:error, ["Simple message", [message: "A keyword list error", code: 1], %{message: "A map error"}]}
Generic handler for interoperability with errors from other libraries:
{:error, :foo}
{:error, 1.0}
{:error, 2}
## To activate a plugin
`{:plugin, NameOfPluginModule, term}` to activate a plugin.
See `Absinthe.Resolution.Plugin` for more information.
"""
def result_error(value, field, source, guess) do
Absinthe.ExecutionError.exception("""
Invalid value returned from resolver.
Resolving field:
#{field.name}
Defined at:
#{field.schema_node.__reference__.location.file}:#{field.schema_node.__reference__.location.line}
Resolving on:
#{inspect source}
Got value:
#{inspect value}
...
#{guess}
...
The result must be one of the following...
#{@error_detail}
""")
end
end
defimpl Inspect, for: Absinthe.Resolution do
import Inspect.Algebra
def inspect(res, opts) do
# TODO: better inspect representation
inner =
res
|> Map.from_struct
|> Map.update!(:fields_cache, fn _ ->
"#fieldscache<...>"
end)
|> Map.to_list
|> Inspect.List.inspect(opts)
concat ["#Absinthe.Resolution<", inner, ">"]
end
end