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:

Using new default connections

class MySchema < GraphQL::Schema
  use GraphQL::Pagination::Connections
end

Adding a custom wrapper

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

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

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

See Also:

  • {Schema{Schema.connections}

Defined Under Namespace

Classes: ImplementationMissingError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnections

Returns a new instance of Connections.



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

def initialize
  @wrappers = {}
  add_default
end

Class Method Details

.use(schema_defn) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/graphql/pagination/connections.rb', line 31

def self.use(schema_defn)
  if schema_defn.is_a?(Class)
    schema_defn.connections = self.new
  else
    # Unwrap a `.define` object
    schema_defn = schema_defn.target
    schema_defn.connections = self.new
    schema_defn.class.connections = schema_defn.connections
  end
end

Instance Method Details

#add(nodes_class, implementation) ⇒ Object



47
48
49
# File 'lib/graphql/pagination/connections.rb', line 47

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

#delete(nodes_class) ⇒ Object



51
52
53
# File 'lib/graphql/pagination/connections.rb', line 51

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

#wrap(field, object, arguments, context) ⇒ Object

Used by the runtime to wrap values in connection wrappers.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/graphql/pagination/connections.rb', line 57

def wrap(field, object, arguments, context)
  impl = nil
  object.class.ancestors.each { |cls|
    impl = @wrappers[cls]
    break if impl
  }

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

  impl.new(
    object,
    context: context,
    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],
  )
end