Class: GraphQL::Types::Relay::BaseConnection

Inherits:
BaseObject show all
Extended by:
Forwardable
Defined in:
lib/graphql/types/relay/base_connection.rb

Overview

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.

Examples:

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 Also:

Constant Summary

Constants included from Schema::Member::GraphQLTypeNames

Schema::Member::GraphQLTypeNames::Boolean, Schema::Member::GraphQLTypeNames::ID, Schema::Member::GraphQLTypeNames::Int

Class Attribute Summary collapse

Attributes inherited from Schema::Object

#context, #object

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseObject

default_relay, default_relay?, to_graphql

Methods inherited from Schema::Object

authorized_new, fields, implements, #initialize, interfaces, kind, own_interfaces, to_graphql

Methods included from Schema::Member::HasFields

add_default_resolve_module, #add_field, extended, #field, #field_class, #fields, #global_id_field, #included, #inherited, #own_fields

Methods included from Schema::Member::TypeSystemHelpers

#kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature

Methods included from Schema::Member::BaseDSLMethods

#accessible?, #authorized?, #description, #graphql_name, #introspection, #mutation, #name, #overridden_graphql_name, #to_graphql, #visible?

Methods included from Relay::TypeExtensions

#connection_type, #define_connection, #define_edge, #edge_type

Methods included from Schema::Member::CachedGraphQLDefinition

#graphql_definition, #initialize_copy

Constructor Details

This class inherits a constructor from GraphQL::Schema::Object

Class Attribute Details

.edge_type(edge_type_class, edge_class: GraphQL::Relay::Edge, node_type: edge_type_class.node_type) ⇒ Object (readonly)

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).



39
40
41
# File 'lib/graphql/types/relay/base_connection.rb', line 39

def edge_type
  @edge_type
end

.node_typeClass (readonly)

Returns:

  • (Class)


36
37
38
# File 'lib/graphql/types/relay/base_connection.rb', line 36

def node_type
  @node_type
end

Class Method Details

.nodes_fieldObject

Add the shortcut nodes field to this connection and its subclasses



72
73
74
# File 'lib/graphql/types/relay/base_connection.rb', line 72

def nodes_field
  field :nodes, [@node_type, null: true], null: true
end

Instance Method Details

#nodesObject

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



81
82
83
# File 'lib/graphql/types/relay/base_connection.rb', line 81

def nodes
  @object.edge_nodes
end