Module: GraphQL::Types::Relay

Defined in:
lib/graphql/types/relay.rb,
lib/graphql/types/relay/node.rb,
lib/graphql/types/relay/base_edge.rb,
lib/graphql/types/relay/page_info.rb,
lib/graphql/types/relay/base_field.rb,
lib/graphql/types/relay/node_field.rb,
lib/graphql/types/relay/base_object.rb,
lib/graphql/types/relay/nodes_field.rb,
lib/graphql/types/relay/base_interface.rb,
lib/graphql/types/relay/base_connection.rb

Overview

This module contains some types and fields that could support Relay conventions in GraphQL.

You can use these classes out of the box if you want, but if you want to use your own GraphQL extensions along with the features in this code, you could also open up the source files and copy the relevant methods and configuration into your own classes.

For example, the provided object types extend BaseObject, but you might want to:

  1. Migrate the extensions from BaseObject into your app’s base object
  2. Copy BaseConnection, BaseEdge, etc into your app, and change them to extend your base object.

Similarly, BaseField’s extensions could be migrated to your app and Node could be implemented to mix in your base interface module.

Defined Under Namespace

Modules: BaseInterface, Node Classes: BaseConnection, BaseEdge, BaseField, BaseObject, PageInfo

Constant Summary collapse

NodeField =

This can be used for implementing Query.node(id: ...), or use it for inspiration for your own field definition.

Examples:

Adding this field directly

add_field(GraphQL::Types::Relay::NodeField)

Implementing a similar field in your own Query root


field :node, GraphQL::Types::Relay::Node, null: true,
  description: "Fetches an object given its ID" do
    argument :id, ID, required: true
  end

def node(id:)
  context.schema.object_from_id(id, context)
end
GraphQL::Schema::Field.new(
  name: "node",
  owner: nil,
  type: GraphQL::Types::Relay::Node,
  null: true,
  description: "Fetches an object given its ID.",
  relay_node_field: true,
) do
  argument :id, "ID!", required: true,
    description: "ID of the object."

  def resolve(obj, args, ctx)
    ctx.schema.object_from_id(args[:id], ctx)
  end

  def resolve_field(obj, args, ctx)
    resolve(obj, args, ctx)
  end
end
NodesField =

This can be used for implementing Query.nodes(ids: ...), or use it for inspiration for your own field definition.

Examples:

Adding this field directly

add_field(GraphQL::Types::Relay::NodesField)

Implementing a similar field in your own Query root


field :nodes, [GraphQL::Types::Relay::Node, null: true], null: false,
  description: Fetches a list of objects given a list of IDs." do
    argument :ids, [ID], required: true
  end

def nodes(ids:)
  ids.map do |id|
    context.schema.object_from_id(context, id)
  end
end
GraphQL::Schema::Field.new(
  name: "nodes",
  owner: nil,
  type: [GraphQL::Types::Relay::Node, null: true],
  null: false,
  description: "Fetches a list of objects given a list of IDs.",
  relay_nodes_field: true,
) do
  argument :ids, "[ID!]!", required: true,
    description: "IDs of the objects."

  def resolve(obj, args, ctx)
    args[:ids].map { |id| ctx.schema.object_from_id(id, ctx) }
  end

  def resolve_field(obj, args, ctx)
    resolve(obj, args, ctx)
  end
end