Class: GraphQL::Schema::Field::ConnectionExtension

Inherits:
GraphQL::Schema::FieldExtension show all
Defined in:
lib/graphql/schema/field/connection_extension.rb

Instance Attribute Summary

Attributes inherited from GraphQL::Schema::FieldExtension

#field, #options

Instance Method Summary collapse

Methods inherited from GraphQL::Schema::FieldExtension

#initialize

Constructor Details

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

Instance Method Details

#after_resolve(value:, object:, arguments:, context:, memo:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/graphql/schema/field/connection_extension.rb', line 24

def after_resolve(value:, object:, arguments:, context:, memo:)
  if value.is_a? GraphQL::ExecutionError
    # This isn't even going to work because context doesn't have ast_node anymore
    context.add_error(value)
    nil
  elsif value.nil?
    nil
  elsif value.is_a?(GraphQL::Pagination::Connection)
    # update the connection with some things that may not have been provided
    value.context ||= context
    value.first_value ||= arguments[:first]
    value.after_value ||= arguments[:after]
    value.last_value ||= arguments[:last]
    value.before_value ||= arguments[:before]
    value.max_page_size ||= field.max_page_size
    value
  elsif context.schema.new_connections?
    wrappers = context.namespace(:connections)[:all_wrappers] ||= context.schema.connections.all_wrappers
    context.schema.connections.wrap(field, value, arguments, context, wrappers: wrappers)
  else
    if object.is_a?(GraphQL::Schema::Object)
      object = object.object
    end
    connection_class = GraphQL::Relay::BaseConnection.connection_for_nodes(value)
    connection_class.new(
      value,
      arguments,
      field: field,
      max_page_size: field.max_page_size,
      parent: object,
      context: context,
    )
  end
end

#applyObject



7
8
9
10
11
12
# File 'lib/graphql/schema/field/connection_extension.rb', line 7

def apply
  field.argument :after, "String", "Returns the elements in the list that come after the specified cursor.", required: false
  field.argument :before, "String", "Returns the elements in the list that come before the specified cursor.", required: false
  field.argument :first, "Int", "Returns the first _n_ elements from the list.", required: false
  field.argument :last, "Int", "Returns the last _n_ elements from the list.", required: false
end

#resolve(object:, arguments:, context:) {|object, next_args| ... } ⇒ Object

Remove pagination args before passing it to a user method

Yields:

  • (object, next_args)


15
16
17
18
19
20
21
22
# File 'lib/graphql/schema/field/connection_extension.rb', line 15

def resolve(object:, arguments:, context:)
  next_args = arguments.dup
  next_args.delete(:first)
  next_args.delete(:last)
  next_args.delete(:before)
  next_args.delete(:after)
  yield(object, next_args)
end