GreenFairy.Input (GreenFairy v0.3.0)

View Source

Defines a GraphQL input object type with a clean DSL.

Usage

defmodule MyApp.GraphQL.Inputs.CreateUserInput do
  use GreenFairy.Input

  input "CreateUserInput" do
    field :email, :string, null: false
    field :first_name, :string, null: false
    field :organization_id, :id
  end
end

Authorization

Control which input fields a user can provide:

defmodule MyApp.GraphQL.Inputs.CreateUserInput do
  use GreenFairy.Input

  input "CreateUserInput" do
    authorize fn input, ctx ->
      if ctx[:current_user]?.admin do
        :all
      else
        [:email, :first_name, :last_name]  # Non-admins can't set role
      end
    end

    field :email, non_null(:string)
    field :first_name, non_null(:string)
    field :last_name, :string
    field :role, :string  # Only admins can set this
  end
end

When a user tries to provide unauthorized fields, they will be stripped from the input or rejected (depending on configuration).

Options

  • :description - Description of the input type (can also use @desc)

Summary

Functions

Sets up field-level authorization for this input type.

Defines a GraphQL input object type.

Controls whether this input type or its fields appear in introspection results. See GreenFairy.Type.visible/1 for details.

Functions

authorize(func)

(macro)

Sets up field-level authorization for this input type.

The authorize function receives the input map and context, and returns which fields are allowed.

Examples

input "CreateUserInput" do
  authorize fn input, ctx ->
    if ctx[:current_user]?.admin, do: :all, else: [:email, :name]
  end

  field :email, non_null(:string)
  field :name, :string
  field :role, :string  # Admin only
end

Return Values

  • :all - All fields allowed
  • :none - No fields allowed (reject input)
  • [:field1, :field2] - Only these fields allowed

input(name, opts \\ [], list)

(macro)

Defines a GraphQL input object type.

Examples

input "CreateUserInput" do
  field :email, :string, null: false
  field :name, :string
end

visible(func)

(macro)

Controls whether this input type or its fields appear in introspection results. See GreenFairy.Type.visible/1 for details.