I hope this helps someone. I lost a bit of time on this problem and I’m not sure if there’s a better way but to access the root arguments from a nested resolver you can do the following:
1- create a custom middleware that accesses the arguments and stores them into the context
2 - register the middleware
3 - access parent arguments via context
In detail
1 - Custom middleware that’ll store the parent/root arguments into the context
defmodule MyAppWeb.GraphQL.Middleware.RootArguments do
@behaviour Absinthe.Middleware
@impl Absinthe.Middleware
def call(resolution, _config) do
%{ resolution | context: Map.put(resolution.context, :root_arguments, resolution.arguments || %{})}
end
end
2 - Register it to the GraphQL Schema
defmodule MyAppWeb.GraphQL.Schema do
use Absinthe.Schema
import_types MyAppWeb.GraphQL.Types
# ...
alias MyAppWeb.GraphQL.Middleware.RootArguments
def middleware(middleware, _field, %{identifier: type}) when type in [:query, :mutation] do
SafeResolution.apply(middleware) ++ [RootArguments]
end
def middleware(middleware, _field, _object) do
middleware
end
end
3 - And then inside of resolvers you can access the arguments via context:
object :orders do
field :total_count, :integer do
resolve(fn _parent, _args, info ->
arguments = Map.get(info.context, :root_arguments, %{})
{:ok, count} = MyApp.Orders.count(arguments)
{:ok, count}
end)
end
end