Skip to content

Middleware: Remove `null` arguments from resolvers' input

Mickael Lucchini edited this page May 14, 2018 · 1 revision

Absinthe v1.4 supports GraphQL null literals.

null values, when provided as arguments, are passed on to Absinthe resolvers as nil (provided they don’t run afoul of a non_null/1 argument constraint).

The concrete effect of this is that your resolvers may need to be updated to take into account that nil is a possible value; in the past arguments would never be passed as nil.

Reverting to the v1.3 behavior is possible using a piece of middleware to strip out nil argument values:

defmodule MyApp.Schema.Middleware.RemoveNil do
  def call(res, _) do
    arguments = fix_args(res.arguments)
    %{res | arguments: arguments}
  end

  def fix_args(%_{} = struct), do: struct

  def fix_args(%{} = args) do
    for {key, val} <- args,
        val != nil,
        into: %{},
        do: {key, fix_args(val)}
  end

  def fix_args(list) when is_list(list) do
    for x <- list, do: fix_args(x)
  end

  def fix_args(val), do: val
end
Clone this wiki locally