GreenFairy.Relay.Node (GreenFairy v0.3.0)
View SourceRelay 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
endType 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
endDefault 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)
endThe 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.
This adds:
node(id: ID!)query fieldnodes(ids: [ID!]!)query field for batch fetching
Options
:repo- The Ecto repo to use for fetching (if using Ecto adapter):node_resolver- Default resolver functionfn type_module, id, ctx -> ... end
Resolves a node from its global ID.
This function:
- Decodes the global ID to get type name and local ID
- Finds the type module for the type name
- Calls the type's node resolver or uses the default adapter resolution