Is there a way to make types non-nullable by default with Absinthe?

The Absinthe docs state:

By default, all types in GraphQL are nullable. To declare a type that disallows null, wrap it in a Absinthe.Type.NonNull struct.

I’m working on a large codebase, where I have to put non_null(:string) on a large number of fields.
I would prefer to have fields be non_null by default and those that can be null, have something like a is_nullable(:integer).

This issue has already been discussed with the Absinthe maintainers. The main takeaway was that this could be done with some macros - has anyone attempted this yet? I haven’t written macros before, so just wondering if anyone else has.

defmodule NonNullNotation do

  defmacro field!(identifier, type) when is_atom(type) do
    quote do
      field(unquote(identifier), non_null(unquote(type)))
    end
  end
end

import this module in your schema and you can do field!(:name, :string) and have it be non-nullable.

This is the macro in its simplest form.

4 Likes