Class: GraphQL::Pagination::Connections

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/pagination/connections.rb

Overview

A schema-level connection wrapper manager.

Attach as a plugin.

Examples:

Adding a custom wrapper

class MySchema < GraphQL::Schema
  connections.add(MyApp::SearchResults, MyApp::SearchResultsConnection)
end

Removing default connection support for arrays (they can still be manually wrapped)

class MySchema < GraphQL::Schema
  connections.delete(Array)
end

See Also:

  • {Schema{Schema.connections}

Defined Under Namespace

Classes: ImplementationMissingError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:) ⇒ Connections

Returns a new instance of Connections.



31
32
33
34
35
# File 'lib/graphql/pagination/connections.rb', line 31

def initialize(schema:)
  @schema = schema
  @wrappers = {}
  add_default
end

Class Method Details

.use(schema_defn) ⇒ Object



24
25
26
27
28
29
# File 'lib/graphql/pagination/connections.rb', line 24

def self.use(schema_defn)
  if schema_defn.plugins.any? { |(plugin, args)| plugin == self }
    GraphQL::Deprecation.warn("#{self} is now the default, remove `use #{self}` from #{caller(2,1).first}")
  end
  schema_defn.connections = self.new(schema: schema_defn)
end

Instance Method Details

#add(nodes_class, implementation) ⇒ Object



37
38
39
# File 'lib/graphql/pagination/connections.rb', line 37

def add(nodes_class, implementation)
  @wrappers[nodes_class] = implementation
end

#all_wrappersObject



45
46
47
48
49
50
51
52
53
# File 'lib/graphql/pagination/connections.rb', line 45

def all_wrappers
  all_wrappers = {}
  @schema.ancestors.reverse_each do |schema_class|
    if schema_class.respond_to?(:connections) && (c = schema_class.connections)
      all_wrappers.merge!(c.wrappers)
    end
  end
  all_wrappers
end

#delete(nodes_class) ⇒ Object



41
42
43
# File 'lib/graphql/pagination/connections.rb', line 41

def delete(nodes_class)
  @wrappers.delete(nodes_class)
end

#edge_class_for_field(field) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

use an override if there is one



94
95
96
97
98
99
100
101
102
# File 'lib/graphql/pagination/connections.rb', line 94

def edge_class_for_field(field)
  conn_type = field.type.unwrap
  conn_type_edge_type = conn_type.respond_to?(:edge_class) && conn_type.edge_class
  if conn_type_edge_type && conn_type_edge_type != Relay::Edge
    conn_type_edge_type
  else
    nil
  end
end

#wrap(field, parent, items, arguments, context) ⇒ Object

Used by the runtime to wrap values in connection wrappers.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/graphql/pagination/connections.rb', line 68

def wrap(field, parent, items, arguments, context)
  return items if GraphQL::Execution::Interpreter::RawValue === items
  wrappers = context ? context.namespace(:connections)[:all_wrappers] : all_wrappers
  impl = wrapper_for(items, wrappers: wrappers)

  if impl.nil?
    raise ImplementationMissingError, "Couldn't find a connection wrapper for #{items.class} during #{field.path} (#{items.inspect})"
  end

  impl.new(
    items,
    context: context,
    parent: parent,
    field: field,
    max_page_size: field.max_page_size || context.schema.default_max_page_size,
    first: arguments[:first],
    after: arguments[:after],
    last: arguments[:last],
    before: arguments[:before],
    arguments: arguments,
    edge_class: edge_class_for_field(field),
  )
end

#wrapper_for(items, wrappers: all_wrappers) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/graphql/pagination/connections.rb', line 55

def wrapper_for(items, wrappers: all_wrappers)
  impl = nil

  items.class.ancestors.each { |cls|
    impl = wrappers[cls]
    break if impl
  }

  impl
end