Module: GraphQL::Relay::ConnectionType

Defined in:
lib/graphql/relay/connection_type.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_nodes_fieldObject

Returns the value of attribute default_nodes_field.



6
7
8
# File 'lib/graphql/relay/connection_type.rb', line 6

def default_nodes_field
  @default_nodes_field
end

Class Method Details

.create_type(wrapped_type, edge_type: nil, edge_class: nil, nodes_field: ConnectionType.default_nodes_field, &block) ⇒ Object

Create a connection which exposes edges of this type



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/graphql/relay/connection_type.rb', line 12

def self.create_type(wrapped_type, edge_type: nil, edge_class: nil, nodes_field: ConnectionType.default_nodes_field, &block)
  edge_type ||= wrapped_type.edge_type
  edge_class ||= GraphQL::Relay::Edge
  connection_type_name = "#{wrapped_type.name}Connection"
  connection_type_description = "The connection type for #{wrapped_type.name}."

  connection_type = ObjectType.define do
    name(connection_type_name)
    description(connection_type_description)
    field :edges, types[edge_type] do
      description "A list of edges."
      resolve ->(obj, args, ctx) {
        obj.edge_nodes.map { |item| edge_class.new(item, obj) }
      }
    end
    if nodes_field
      field :nodes, types[wrapped_type] do
        description "A list of nodes."
        resolve ->(obj, args, ctx) {
          obj.edge_nodes
        }
      end
    end
    field :pageInfo, !PageInfo, "Information to aid in pagination.", property: :page_info
    block && instance_eval(&block)
  end

  connection_type
end