Class: GraphQL::Relay::BaseConnection

Inherits:
Object
  • Object
show all
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

Direct Known Subclasses

ArrayConnection, RelationConnection

Constant Summary

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 {"Array" => ArrayConnection}

{}

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, context: nil) ⇒ BaseConnection

Make a connection, wrapping nodes

Parameters:

  • nodes (Object)

    The collection of nodes

  • arguments (GraphQL::Query::Arguments)

    Query arguments

  • field (GraphQL::Field)

    The underlying field

  • max_page_size (Int)

    The maximum number of results to return

  • parent (Object)

    The object which this collection belongs to

  • context (GraphQL::Query::Context)

    The context from the field being resolved



57
58
59
60
61
62
63
64
65
# File 'lib/graphql/relay/base_connection.rb', line 57

def initialize(nodes, arguments, field: nil, max_page_size: nil, parent: nil, context: nil)
  @context = context
  @nodes = nodes
  @arguments = arguments
  @field = field
  @parent = parent
  @encoder = context ? @context.schema.cursor_encoder : GraphQL::Schema::Base64Encoder
  @max_page_size = max_page_size.nil? && context ? @context.schema.default_max_page_size : max_page_size
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments



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

def arguments
  @arguments
end

#contextObject (readonly)

Returns the value of attribute context



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

def context
  @context
end

#fieldObject (readonly)

Returns the value of attribute field



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

def field
  @field
end

#max_page_sizeObject (readonly)

Returns the value of attribute max_page_size



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

def max_page_size
  @max_page_size
end

#nodesObject (readonly)

Returns the value of attribute nodes



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

def nodes
  @nodes
end

#parentObject (readonly)

Returns the value of attribute parent



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

def parent
  @parent
end

Class Method Details

.connection_for_nodes(nodes) ⇒ subclass of BaseConnection

Find a connection implementation suitable for exposing nodes

Parameters:

  • nodes (Object)

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

Returns:

  • (subclass of BaseConnection)

    a connection Class for wrapping nodes



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/graphql/relay/base_connection.rb', line 27

def connection_for_nodes(nodes)
  # Check for class _names_ because classes can be redefined in Rails development
  nodes.class.ancestors.each do |ancestor|
    conn_impl = CONNECTION_IMPLEMENTATIONS[ancestor.name]
    if conn_impl
      return conn_impl
    end
  end
  # Should have found a connection during the loop:
  raise("No connection implementation to wrap #{nodes.class} (#{nodes})")
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:

  • nodes_class (Class)

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

  • connection_class (Class)

    A class implementing Connection methods



43
44
45
# File 'lib/graphql/relay/base_connection.rb', line 43

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

Instance Method Details

#afterString?

The value passed as after:, if there was one

Returns:

  • (String, nil)


83
84
85
# File 'lib/graphql/relay/base_connection.rb', line 83

def after
  arguments[:after]
end

#beforeString?

The value passed as before:, if there was one

Returns:

  • (String, nil)


95
96
97
# File 'lib/graphql/relay/base_connection.rb', line 95

def before
  arguments[:before]
end

#cursor_from_node(object) ⇒ Object

An opaque operation which returns a connection-specific cursor.

Raises:

  • (NotImplementedError)


139
140
141
# File 'lib/graphql/relay/base_connection.rb', line 139

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

#decode(data) ⇒ Object



71
72
73
# File 'lib/graphql/relay/base_connection.rb', line 71

def decode(data)
  @encoder.decode(data, nonce: true)
end

#edge_nodesObject

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



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

def edge_nodes
  @edge_nodes ||= paged_nodes
end

#encode(data) ⇒ Object



67
68
69
# File 'lib/graphql/relay/base_connection.rb', line 67

def encode(data)
  @encoder.encode(data, nonce: true)
end

#end_cursorObject

Used by pageInfo



130
131
132
133
134
135
136
# File 'lib/graphql/relay/base_connection.rb', line 130

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

#firstInteger?

The value passed as first:, if there was one. Negative numbers become 0.

Returns:

  • (Integer, nil)


77
78
79
# File 'lib/graphql/relay/base_connection.rb', line 77

def first
  @first ||= get_limited_arg(:first)
end

#has_next_pageObject

Used by pageInfo



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

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

#has_previous_pageObject

Used by pageInfo



116
117
118
# File 'lib/graphql/relay/base_connection.rb', line 116

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

#lastInteger?

The value passed as last:, if there was one. Negative numbers become 0.

Returns:

  • (Integer, nil)


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

def last
  @last ||= get_limited_arg(:last)
end

#page_infoObject

Support the pageInfo field



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

def page_info
  self
end

#start_cursorObject

Used by pageInfo



121
122
123
124
125
126
127
# File 'lib/graphql/relay/base_connection.rb', line 121

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