Class: GraphQL::Relay::BaseConnection

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
Defined in:
lib/graphql/relay/base_connection.rb

Overview

Subclasses must implement:

- {#cursor_from_node}, which returns an opaque cursor for the given item
- {#sliced_nodes}, which slices by `before` & `after`
- {#paged_nodes}, which applies `first` & `last` limits

In a subclass, you have access to

- {#nodes}, the collection which the connection will wrap
- {#first}, {#after}, {#last}, {#before} (arguments passed to the field)
- {#max_page_size} (the specified maximum page size that can be returned from a connection)

Direct Known Subclasses

ArrayConnection, RelationConnection

Constant Summary collapse

CURSOR_SEPARATOR =

Just to encode data in the cursor, use something that won’t conflict

"---"
CONNECTION_IMPLEMENTATIONS =

Map of collection class names -> connection_classes eg => ArrayConnection

{}
METHODS_FROM_ARGUMENTS =

Provide easy access to provided arguments:

[:first, :after, :last, :before, :order]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes, arguments, field: nil, max_page_size: nil, parent: nil) ⇒ BaseConnection

Make a connection, wrapping ‘nodes`

Parameters:

  • The (Object)

    collection of nodes

  • Query

    arguments

  • field (Object) (defaults to: nil)

    The underlying field

  • max_page_size (Int) (defaults to: nil)

    The maximum number of results to return

  • parent (Object) (defaults to: nil)

    The object which this collection belongs to



64
65
66
67
68
69
70
# File 'lib/graphql/relay/base_connection.rb', line 64

def initialize(nodes, arguments, field: nil, max_page_size: nil, parent: nil)
  @nodes = nodes
  @arguments = arguments
  @max_page_size = max_page_size
  @field = field
  @parent = parent
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



56
57
58
# File 'lib/graphql/relay/base_connection.rb', line 56

def arguments
  @arguments
end

#fieldObject (readonly)

Returns the value of attribute field.



56
57
58
# File 'lib/graphql/relay/base_connection.rb', line 56

def field
  @field
end

#max_page_sizeObject (readonly)

Returns the value of attribute max_page_size.



56
57
58
# File 'lib/graphql/relay/base_connection.rb', line 56

def max_page_size
  @max_page_size
end

#nodesObject (readonly) Also known as: object

Returns the value of attribute nodes.



56
57
58
# File 'lib/graphql/relay/base_connection.rb', line 56

def nodes
  @nodes
end

#parentObject (readonly)

Returns the value of attribute parent.



56
57
58
# File 'lib/graphql/relay/base_connection.rb', line 56

def parent
  @parent
end

Class Method Details

.connection_for_nodes(nodes) ⇒ subclass of BaseConnection Also known as: connection_for_items

Find a connection implementation suitable for exposing ‘nodes`

Parameters:

  • A (Object)

    collection of nodes (eg, Array, AR::Relation)

Returns:

  • (subclass of BaseConnection)

    a connection Class for wrapping ‘nodes`



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/graphql/relay/base_connection.rb', line 30

def connection_for_nodes(nodes)
  # Check for class _names_ because classes can be redefined in Rails development
  ancestor_names = nodes.class.ancestors.map(&:name)
  implementation = CONNECTION_IMPLEMENTATIONS.find do |nodes_class_name, connection_class|
    ancestor_names.include? nodes_class_name
  end
  if implementation.nil?
    raise("No connection implementation to wrap #{nodes.class} (#{nodes})")
  else
    implementation[1]
  end
end

.register_connection_implementation(nodes_class, connection_class) ⇒ Object

Add ‘connection_class` as the connection wrapper for `nodes_class` eg, `RelationConnection` is the implementation for `AR::Relation`

Parameters:

  • A (Class)

    class representing a collection (eg, Array, AR::Relation)

  • A (Class)

    class implementing Connection methods



47
48
49
# File 'lib/graphql/relay/base_connection.rb', line 47

def register_connection_implementation(nodes_class, connection_class)
  CONNECTION_IMPLEMENTATIONS[nodes_class.name] = connection_class
end

Instance Method Details

#afterObject

The value passed as ‘after:`, if there was one



89
90
91
92
93
# File 'lib/graphql/relay/base_connection.rb', line 89

METHODS_FROM_ARGUMENTS.each do |arg_name|
  define_method(arg_name) do
    arguments[arg_name]
  end
end

#beforeObject

The value passed as ‘before:`, if there was one



89
90
91
92
93
# File 'lib/graphql/relay/base_connection.rb', line 89

METHODS_FROM_ARGUMENTS.each do |arg_name|
  define_method(arg_name) do
    arguments[arg_name]
  end
end

#cursor_from_node(object) ⇒ Object

An opaque operation which returns a connection-specific cursor.

Raises:

  • (NotImplementedError)


135
136
137
# File 'lib/graphql/relay/base_connection.rb', line 135

def cursor_from_node(object)
  raise NotImplementedError, "must return a cursor for this object/connection pair"
end

#edge_nodesObject

These are the nodes to render for this connection, probably wrapped by Edge



97
98
99
# File 'lib/graphql/relay/base_connection.rb', line 97

def edge_nodes
  @edge_nodes ||= paged_nodes
end

#end_cursorObject

Used by ‘pageInfo`



126
127
128
129
130
131
132
# File 'lib/graphql/relay/base_connection.rb', line 126

def end_cursor
  if end_node = (respond_to?(:paged_nodes_array, true) ? paged_nodes_array : paged_nodes).last
    return cursor_from_node(end_node)
  else
    return nil
  end
end

#firstObject

The value passed as ‘first:`, if there was one



89
90
91
92
93
# File 'lib/graphql/relay/base_connection.rb', line 89

METHODS_FROM_ARGUMENTS.each do |arg_name|
  define_method(arg_name) do
    arguments[arg_name]
  end
end

#has_next_pageObject

Used by ‘pageInfo`



107
108
109
# File 'lib/graphql/relay/base_connection.rb', line 107

def has_next_page
  !!(first && sliced_nodes.count > first)
end

#has_previous_pageObject

Used by ‘pageInfo`



112
113
114
# File 'lib/graphql/relay/base_connection.rb', line 112

def has_previous_page
  !!(last && sliced_nodes.count > last)
end

#lastObject

The value passed as ‘last:`, if there was one



89
90
91
92
93
# File 'lib/graphql/relay/base_connection.rb', line 89

METHODS_FROM_ARGUMENTS.each do |arg_name|
  define_method(arg_name) do
    arguments[arg_name]
  end
end

#orderObject

The value passed as ‘order:`, if there was one



89
90
91
92
93
# File 'lib/graphql/relay/base_connection.rb', line 89

METHODS_FROM_ARGUMENTS.each do |arg_name|
  define_method(arg_name) do
    arguments[arg_name]
  end
end

#page_infoObject

Support the ‘pageInfo` field



102
103
104
# File 'lib/graphql/relay/base_connection.rb', line 102

def page_info
  self
end

#start_cursorObject

Used by ‘pageInfo`



117
118
119
120
121
122
123
# File 'lib/graphql/relay/base_connection.rb', line 117

def start_cursor
  if start_node = (respond_to?(:paged_nodes_array, true) ? paged_nodes_array : paged_nodes).first
    return cursor_from_node(start_node)
  else
    return nil
  end
end