Class: GraphQL::Relay::ConnectionField

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/relay/connection_field.rb

Overview

Provided a GraphQL field which returns a collection of items, `ConnectionField.create` modifies that field to expose those items as a collection.

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

Constant Summary collapse

ARGUMENT_DEFINITIONS =
[
  [:first, GraphQL::INT_TYPE],
  [:after, GraphQL::STRING_TYPE],
  [:last, GraphQL::INT_TYPE],
  [:before, GraphQL::STRING_TYPE],
  [:order, GraphQL::STRING_TYPE],
]
DEFAULT_ARGUMENTS =
ARGUMENT_DEFINITIONS.reduce({}) do |memo, arg_defn|
  argument = GraphQL::Argument.new
  argument.name = arg_defn[0]
  argument.type = arg_defn[1]
  memo[argument.name.to_s] = argument
  memo
end

Class Method Summary collapse

Class Method Details

.create(underlying_field) ⇒ GraphQL::Field

Turn A GraphQL::Field into a connection by:

  • Merging in the default arguments

  • Transforming its resolve function to return a connection object

Parameters:

  • A (GraphQL::Field)

    field which returns items to be wrapped as a connection

Returns:

  • (GraphQL::Field)

    A field which serves a connections


31
32
33
34
35
36
37
# File 'lib/graphql/relay/connection_field.rb', line 31

def self.create(underlying_field)
  underlying_field.arguments = DEFAULT_ARGUMENTS.merge(underlying_field.arguments)  # TODO: make a public API on GraphQL::Field to expose this proc

  original_resolve = underlying_field.instance_variable_get(:@resolve_proc)
  underlying_field.resolve = get_connection_resolve(underlying_field.name, original_resolve)
  underlying_field
end