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

Schema::Member::HasFields::CONFLICT_FIELD_NAMES, Schema::Member::HasFields::GRAPHQL_RUBY_KEYWORDS, Schema::Member::HasFields::RUBY_KEYWORDS

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, interface_type_memberships, interfaces, kind, own_interface_type_memberships, to_graphql

Methods included from Schema::Member::HasFields

#add_field, #field, #field_class, #fields, #get_field, #global_id_field, #own_fields

Methods included from Schema::Member::HasAstNode

#ast_node

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

#inherited

Methods included from Schema::Member::BaseDSLMethods

#accessible?, #authorized?, #default_graphql_name, #description, #graphql_name, #introspection, #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, #type_class

Constructor Details

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

Class Attribute Details

.edge_classClass (readonly)

Returns:

  • (Class)

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

def edge_class
  @edge_class
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

.accessible?(ctx) ⇒ Boolean

Returns:


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

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

.authorized?(obj, ctx) ⇒ Boolean

Returns:


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

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


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/graphql/types/relay/base_connection.rb', line 51

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
  @edge_class = edge_class

  field :edges, [edge_type_class, null: true],
    null: true,
    description: "A list of edges.",
    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


75
76
77
# File 'lib/graphql/types/relay/base_connection.rb', line 75

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


70
71
72
# File 'lib/graphql/types/relay/base_connection.rb', line 70

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

.visible?(ctx) ⇒ Boolean

Returns:


87
88
89
# File 'lib/graphql/types/relay/base_connection.rb', line 87

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

Instance Method Details

#edgesObject


108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/graphql/types/relay/base_connection.rb', line 108

def edges
  if @object.is_a?(GraphQL::Pagination::Connection)
    @object.edges
  elsif context.interpreter?
    context.schema.after_lazy(object.edge_nodes) do |nodes|
      nodes.map { |n| self.class.edge_class.new(n, object) }
    end
  else
    # This is done by edges_instrumentation
    @object.edge_nodes
  end
end

#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


104
105
106
# File 'lib/graphql/types/relay/base_connection.rb', line 104

def nodes
  @object.edge_nodes
end