GreenFairy.Relay.Mutation (GreenFairy v0.3.0)

View Source

Relay-compliant mutation helpers with clientMutationId support.

This module provides macros for defining Relay-style mutations that automatically handle the clientMutationId field.

Usage

Use relay_mutation instead of field for Relay-compliant mutations:

defmodule MyApp.GraphQL.Mutations.UserMutations do
  use GreenFairy.Mutation
  import GreenFairy.Relay.Mutation

  mutations do
    relay_mutation :create_user do
      @desc "Creates a new user"

      input do
        field :email, non_null(:string)
        field :name, :string
      end

      output do
        field :user, :user
        field :errors, list_of(:string)
      end

      resolve fn input, ctx ->
        case MyApp.Accounts.create_user(input) do
          {:ok, user} -> {:ok, %{user: user}}
          {:error, changeset} -> {:ok, %{errors: format_errors(changeset)}}
        end
      end
    end
  end
end

This generates:

  • CreateUserInput input type with clientMutationId field
  • CreateUserPayload output type with clientMutationId field
  • Automatic passthrough of clientMutationId from input to output

Summary

Functions

Converts a mutation name to its input type name.

Converts a mutation name to its payload type name.

Defines a Relay-compliant mutation with automatic clientMutationId handling.

Functions

mutation_input_name(name)

Converts a mutation name to its input type name.

Examples

iex> mutation_input_name(:create_user)
:create_user_input

mutation_payload_name(name)

Converts a mutation name to its payload type name.

Examples

iex> mutation_payload_name(:create_user)
:create_user_payload

relay_mutation(name, list)

(macro)

Defines a Relay-compliant mutation with automatic clientMutationId handling.

Options

  • :input - Block defining input fields (clientMutationId is added automatically)
  • :output - Block defining output fields (clientMutationId is added automatically)
  • :resolve - Resolver function