Class: GraphQL::Relay::BaseConnection
- Inherits:
- 
      Object
      
        - Object
- GraphQL::Relay::BaseConnection
 
- 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
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 {"Array" => ArrayConnection}
- {} 
Instance Attribute Summary collapse
- 
  
    
      #arguments  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute arguments. 
- 
  
    
      #context  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute context. 
- 
  
    
      #field  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute field. 
- 
  
    
      #max_page_size  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute max_page_size. 
- 
  
    
      #nodes  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute nodes. 
- 
  
    
      #parent  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute parent. 
Class Method Summary collapse
- 
  
    
      .connection_for_nodes(nodes)  ⇒ subclass of BaseConnection 
    
    
  
  
  
  
  
  
  
  
  
    Find a connection implementation suitable for exposing nodes.
- 
  
    
      .register_connection_implementation(nodes_class, connection_class)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Add connection_classas the connection wrapper fornodes_classeg,RelationConnectionis the implementation forAR::Relation.
Instance Method Summary collapse
- 
  
    
      #after  ⇒ String? 
    
    
  
  
  
  
  
  
  
  
  
    The value passed as after:, if there was one.
- 
  
    
      #before  ⇒ String? 
    
    
  
  
  
  
  
  
  
  
  
    The value passed as before:, if there was one.
- 
  
    
      #cursor_from_node(object)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    An opaque operation which returns a connection-specific cursor. 
- 
  
    
      #decode(data)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #edge_nodes  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    These are the nodes to render for this connection, probably wrapped by Edge. 
- 
  
    
      #encode(data)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #end_cursor  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Used by pageInfo.
- 
  
    
      #first  ⇒ Integer? 
    
    
  
  
  
  
  
  
  
  
  
    The value passed as first:, if there was one.
- 
  
    
      #has_next_page  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Used by pageInfo.
- 
  
    
      #has_previous_page  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Used by pageInfo.
- 
  
    
      #initialize(nodes, arguments, field: nil, max_page_size: nil, parent: nil, context: nil)  ⇒ BaseConnection 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Make a connection, wrapping nodes.
- 
  
    
      #inspect  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #last  ⇒ Integer? 
    
    
  
  
  
  
  
  
  
  
  
    The value passed as last:, if there was one.
- 
  
    
      #page_info  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Support the pageInfofield.
- 
  
    
      #start_cursor  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Used by pageInfo.
Constructor Details
#initialize(nodes, arguments, field: nil, max_page_size: nil, parent: nil, context: nil) ⇒ BaseConnection
Make a connection, wrapping nodes
| 61 62 63 64 65 66 67 68 69 | # File 'lib/graphql/relay/base_connection.rb', line 61 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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
| 52 53 54 | # File 'lib/graphql/relay/base_connection.rb', line 52 def arguments @arguments end | 
#context ⇒ Object (readonly)
Returns the value of attribute context.
| 52 53 54 | # File 'lib/graphql/relay/base_connection.rb', line 52 def context @context end | 
#field ⇒ Object (readonly)
Returns the value of attribute field.
| 52 53 54 | # File 'lib/graphql/relay/base_connection.rb', line 52 def field @field end | 
#max_page_size ⇒ Object (readonly)
Returns the value of attribute max_page_size.
| 52 53 54 | # File 'lib/graphql/relay/base_connection.rb', line 52 def max_page_size @max_page_size end | 
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
| 52 53 54 | # File 'lib/graphql/relay/base_connection.rb', line 52 def nodes @nodes end | 
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
| 52 53 54 | # File 'lib/graphql/relay/base_connection.rb', line 52 def parent @parent end | 
Class Method Details
.connection_for_nodes(nodes) ⇒ subclass of BaseConnection
Find a connection implementation suitable for exposing nodes
| 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | # File 'lib/graphql/relay/base_connection.rb', line 27 def connection_for_nodes(nodes) # If it's a new-style connection object, it's already ready to go if nodes.is_a?(GraphQL::Pagination::Connection) return nodes end # 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
| 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
#after ⇒ String?
The value passed as after:, if there was one
| 93 94 95 | # File 'lib/graphql/relay/base_connection.rb', line 93 def after arguments[:after] end | 
#before ⇒ String?
The value passed as before:, if there was one
| 105 106 107 | # File 'lib/graphql/relay/base_connection.rb', line 105 def before arguments[:before] end | 
#cursor_from_node(object) ⇒ Object
An opaque operation which returns a connection-specific cursor.
| 149 150 151 | # File 'lib/graphql/relay/base_connection.rb', line 149 def cursor_from_node(object) raise GraphQL::RequiredImplementationMissingError, "must return a cursor for this object/connection pair" end | 
#decode(data) ⇒ Object
| 75 76 77 | # File 'lib/graphql/relay/base_connection.rb', line 75 def decode(data) @encoder.decode(data, nonce: true) end | 
#edge_nodes ⇒ Object
These are the nodes to render for this connection, probably wrapped by Edge
| 111 112 113 | # File 'lib/graphql/relay/base_connection.rb', line 111 def edge_nodes @edge_nodes ||= paged_nodes end | 
#encode(data) ⇒ Object
| 71 72 73 | # File 'lib/graphql/relay/base_connection.rb', line 71 def encode(data) @encoder.encode(data, nonce: true) end | 
#end_cursor ⇒ Object
Used by pageInfo
| 140 141 142 143 144 145 146 | # File 'lib/graphql/relay/base_connection.rb', line 140 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 | 
#first ⇒ Integer?
The value passed as first:, if there was one. Negative numbers become 0.
| 81 82 83 84 85 86 87 88 89 | # File 'lib/graphql/relay/base_connection.rb', line 81 def first @first ||= begin capped = limit_pagination_argument(arguments[:first], max_page_size) if capped.nil? && last.nil? capped = max_page_size end capped end end | 
#has_next_page ⇒ Object
Used by pageInfo
| 121 122 123 | # File 'lib/graphql/relay/base_connection.rb', line 121 def has_next_page !!(first && sliced_nodes.count > first) end | 
#has_previous_page ⇒ Object
Used by pageInfo
| 126 127 128 | # File 'lib/graphql/relay/base_connection.rb', line 126 def has_previous_page !!(last && sliced_nodes.count > last) end | 
#inspect ⇒ Object
| 153 154 155 | # File 'lib/graphql/relay/base_connection.rb', line 153 def inspect "#<GraphQL::Relay::Connection @parent=#{@parent.inspect} @arguments=#{@arguments.to_h.inspect}>" end | 
#last ⇒ Integer?
The value passed as last:, if there was one. Negative numbers become 0.
| 99 100 101 | # File 'lib/graphql/relay/base_connection.rb', line 99 def last @last ||= limit_pagination_argument(arguments[:last], max_page_size) end | 
#page_info ⇒ Object
Support the pageInfo field
| 116 117 118 | # File 'lib/graphql/relay/base_connection.rb', line 116 def page_info self end | 
#start_cursor ⇒ Object
Used by pageInfo
| 131 132 133 134 135 136 137 | # File 'lib/graphql/relay/base_connection.rb', line 131 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 |