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, #get_field, #global_id_field, #included, #inherited, #own_fields

Methods included from Schema::Member::HasPath

#path

Methods included from Schema::Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Methods included from Schema::Member::Scoped

#scope_items

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?, #default_graphql_name, #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

.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

.accessible?(ctx) ⇒ Boolean

Returns:



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

def accessible?(ctx)
  node_type.accessible?(ctx)
end

.authorized?(obj, ctx) ⇒ Boolean

Returns:



76
77
78
# 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) ⇒ Object

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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_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
  define_nodes_field
end

.scope_items(items, context) ⇒ Object

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



67
68
69
# File 'lib/graphql/types/relay/base_connection.rb', line 67

def scope_items(items, context)
  node_type.scope_items(items, context)
end

.visible?(ctx) ⇒ Boolean

Returns:



84
85
86
# File 'lib/graphql/types/relay/base_connection.rb', line 84

def visible?(ctx)
  node_type.visible?(ctx)
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



101
102
103
# File 'lib/graphql/types/relay/base_connection.rb', line 101

def nodes
  @object.edge_nodes
end