Module: GraphQL::Relay::ConnectionInstrumentation

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

Overview

Provided a GraphQL field which returns a collection of nodes, wrap that field to expose those nodes as a connection.

The original resolve proc is used to fetch nodes, then a connection implementation is fetched with BaseConnection.connection_for_nodes.

Class Method Summary collapse

Class Method Details

.default_argumentsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/graphql/relay/connection_instrumentation.rb', line 10

def self.default_arguments
  @default_arguments ||= begin
    argument_definitions = [
        ["first", GraphQL::INT_TYPE, "Returns the first _n_ elements from the list."],
        ["after", GraphQL::STRING_TYPE, "Returns the elements in the list that come after the specified cursor."],
        ["last", GraphQL::INT_TYPE, "Returns the last _n_ elements from the list."],
        ["before", GraphQL::STRING_TYPE, "Returns the elements in the list that come before the specified cursor."],
      ]

    argument_definitions.reduce({}) do |memo, arg_defn|
      argument = GraphQL::Argument.new
      name, type, description = arg_defn
      argument.name = name
      argument.type = type
      argument.description = description
      memo[argument.name.to_s] = argument
      memo
    end
  end
end

.instrument(type, field) ⇒ Object

Build a connection field from a Field by: - Merging in the default arguments - Transforming its resolve function to return a connection object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphql/relay/connection_instrumentation.rb', line 34

def self.instrument(type, field)
  if field.connection?
    connection_arguments = default_arguments.merge(field.arguments)
    original_resolve = field.resolve_proc
    original_lazy_resolve = field.lazy_resolve_proc
    connection_resolve = GraphQL::Relay::ConnectionResolve.new(field, original_resolve)
    connection_lazy_resolve = GraphQL::Relay::ConnectionResolve.new(field, original_lazy_resolve)
    field.redefine(
      resolve: connection_resolve,
      lazy_resolve: connection_lazy_resolve,
      arguments: connection_arguments,
    )
  else
    field
  end
end