class GraphQL::Types::Relay::BaseConnection

Use this to implement Relay connections, or take it as inspiration for Relay classes in your own app.

You may wish to copy this code into your own base class, so you can extend your own `BaseObject` instead of `GraphQL::Schema::Object`.

@example Implementation a connection and edge

# Given some object in your app ...
class Types::Post < BaseObject
end

# Make a couple of base classes:
class Types::BaseEdge < GraphQL::Types::Relay::BaseEdge; end
class Types::BaseConnection < GraphQL::Types::Relay::BaseConnection; end

# Then extend them for the object in your app
class Types::PostEdge < Types::BaseEdge
  node_type(Types::Post)
end
class Types::PostConnection < Types::BaseConnection
  edge_type(Types::PostEdge)
end

@see Relay::BaseEdge for edge types

Attributes

node_type[R]

@return [Class]

Public Class Methods

accessible?(ctx) click to toggle source
# File lib/graphql/types/relay/base_connection.rb, line 80
def accessible?(ctx)
  node_type.accessible?(ctx)
end
authorized?(obj, ctx) click to toggle source
# File lib/graphql/types/relay/base_connection.rb, line 76
def authorized?(obj, ctx)
  true # Let nodes be filtered out
end
edge_type(edge_type_class, edge_class: GraphQL::Relay::Edge, node_type: edge_type_class.node_type, nodes_field: true) click to toggle source

Configure this connection to return `edges` and `nodes` based on `edge_type_class`.

This method will use the inputs to create:

  • `edges` field

  • `nodes` field

  • description

It's called when you subclass this base connection, trying to use the class name to set defaults. You can call it again in the class definition to override the default (or provide a value, if the default lookup failed).

# File lib/graphql/types/relay/base_connection.rb, line 48
def edge_type(edge_type_class, edge_class: GraphQL::Relay::Edge, node_type: edge_type_class.node_type, nodes_field: true)
  # Set this connection's graphql name
  node_type_name = node_type.graphql_name

  @node_type = node_type
  @edge_type = edge_type_class

  field :edges, [edge_type_class, null: true],
    null: true,
    description: "A list of edges.",
    method: :edge_nodes,
    edge_class: edge_class

  define_nodes_field if nodes_field

  description("The connection type for #{node_type_name}.")
end
nodes_field() click to toggle source

Add the shortcut `nodes` field to this connection and its subclasses

# File lib/graphql/types/relay/base_connection.rb, line 72
def nodes_field
  define_nodes_field
end
scope_items(items, context) click to toggle source

Filter this list according to the way its node type would scope them

# File lib/graphql/types/relay/base_connection.rb, line 67
def scope_items(items, context)
  node_type.scope_items(items, context)
end
visible?(ctx) click to toggle source
# File lib/graphql/types/relay/base_connection.rb, line 84
def visible?(ctx)
  node_type.visible?(ctx)
end

Private Class Methods

define_nodes_field() click to toggle source
# File lib/graphql/types/relay/base_connection.rb, line 90
def define_nodes_field
  field :nodes, [@node_type, null: true],
    null: true,
    description: "A list of nodes."
end

Public Instance Methods

nodes() click to toggle source

By default this calls through to the ConnectionWrapper's edge nodes method, but sometimes you need to override it to support the `nodes` field

# File lib/graphql/types/relay/base_connection.rb, line 101
def nodes
  @object.edge_nodes
end