GreenFairy.Relay.Field (GreenFairy v0.3.0)

View Source

Field helpers for Relay-compliant types.

This module provides macros for defining Relay-specific fields like global IDs and node resolvers.

Usage

Import this module in your type definitions:

defmodule MyApp.GraphQL.Types.User do
  use GreenFairy.Type
  import GreenFairy.Relay.Field

  type "User", struct: MyApp.User do
    implements GreenFairy.BuiltIns.Node

    # Automatically generates globally unique ID
    global_id :id

    field :email, :string
  end
end

Summary

Functions

Gets the type name for global ID encoding.

Defines a globally unique ID field for Relay.

Defines a custom node resolver for this type.

Functions

get_type_name(module, resolution)

Gets the type name for global ID encoding.

This is called at runtime to determine the type name to use when encoding global IDs.

global_id(field_name, opts \\ [])

(macro)

Defines a globally unique ID field for Relay.

This generates an :id field that returns a Base64-encoded global ID containing the type name and local ID.

Options

  • :source - The source field to use for the local ID (default: :id)
  • :type_name - Override the type name used in encoding (default: uses the GraphQL type name)

Examples

# Uses the struct's :id field
global_id :id

# Uses a different source field
global_id :id, source: :uuid

# Override the type name
global_id :id, type_name: "User"

node_resolver(resolver_fn)

(macro)

Defines a custom node resolver for this type.

When the node(id: ID!) query is used, this resolver will be called to fetch the object by its local ID.

Examples

node_resolver fn id, ctx ->
  MyApp.Accounts.get_user(id)
end