GreenFairy.Relay.Node (GreenFairy v0.3.0)

View Source

Relay Node interface and query field support.

This module provides the standard Relay node query field that allows fetching any object by its global ID.

Schema Integration

Add the node query to your schema:

defmodule MyApp.GraphQL.Schema do
  use GreenFairy.Schema, discover: [MyApp.GraphQL]
  use GreenFairy.Relay.Node

  # This adds the node(id: ID!) query field
end

Type Registration

Types that implement the Node interface must register themselves:

defmodule MyApp.GraphQL.Types.User do
  use GreenFairy.Type

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

    global_id :id
    field :email, :string
  end
end

Default Node Resolution

Configure a default resolver for all node types:

use GreenFairy.Relay,
  repo: MyApp.Repo,
  node_resolver: fn type_module, id, ctx ->
    struct = type_module.__green_fairy_struct__()
    MyApp.Repo.get(struct, id)
  end

The resolver receives:

  • type_module - The GraphQL type module (e.g., MyApp.GraphQL.Types.User)
  • id - The local ID (already parsed to integer if numeric)
  • ctx - The Absinthe context

Per-Type Node Resolution

Override the default for specific types:

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

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

  global_id :id
  field :email, :string
end

Summary

Functions

Macro to add Relay node query support to a schema.

Resolves a node from its global ID.

Functions

__using__(opts \\ [])

(macro)

Macro to add Relay node query support to a schema.

This adds:

  • node(id: ID!) query field
  • nodes(ids: [ID!]!) query field for batch fetching

Options

  • :repo - The Ecto repo to use for fetching (if using Ecto adapter)
  • :node_resolver - Default resolver function fn type_module, id, ctx -> ... end

resolve_node(global_id, resolution, opts \\ [])

Resolves a node from its global ID.

This function:

  1. Decodes the global ID to get type name and local ID
  2. Finds the type module for the type name
  3. Calls the type's node resolver or uses the default adapter resolution