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
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 => ArrayConnection
{}
- METHODS_FROM_ARGUMENTS =
Provide easy access to provided arguments:
[:first, :after, :last, :before]
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#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_class
as the connection wrapper fornodes_class
eg,RelationConnection
is the implementation forAR::Relation
.
Instance Method Summary collapse
-
#after ⇒ Object
The value passed as
after:
, if there was one. -
#before ⇒ Object
The value passed as
before:
, if there was one. -
#cursor_from_node(object) ⇒ Object
An opaque operation which returns a connection-specific cursor.
-
#edge_nodes ⇒ Object
These are the nodes to render for this connection, probably wrapped by Edge.
-
#end_cursor ⇒ Object
Used by
pageInfo
. -
#first ⇒ Object
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) ⇒ BaseConnection
constructor
Make a connection, wrapping
nodes
. -
#last ⇒ Object
The value passed as
last:
, if there was one. -
#page_info ⇒ Object
Support the
pageInfo
field. -
#start_cursor ⇒ Object
Used by
pageInfo
.
Constructor Details
#initialize(nodes, arguments, field: nil, max_page_size: nil, parent: nil) ⇒ BaseConnection
Make a connection, wrapping nodes
57 58 59 60 61 62 63 |
# File 'lib/graphql/relay/base_connection.rb', line 57 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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
49 50 51 |
# File 'lib/graphql/relay/base_connection.rb', line 49 def arguments @arguments end |
#field ⇒ Object (readonly)
Returns the value of attribute field.
49 50 51 |
# File 'lib/graphql/relay/base_connection.rb', line 49 def field @field end |
#max_page_size ⇒ Object (readonly)
Returns the value of attribute max_page_size.
49 50 51 |
# File 'lib/graphql/relay/base_connection.rb', line 49 def max_page_size @max_page_size end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
49 50 51 |
# File 'lib/graphql/relay/base_connection.rb', line 49 def nodes @nodes end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
49 50 51 |
# File 'lib/graphql/relay/base_connection.rb', line 49 def parent @parent end |
Class Method Details
.connection_for_nodes(nodes) ⇒ subclass of BaseConnection
Find a connection implementation suitable for exposing nodes
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/graphql/relay/base_connection.rb', line 26 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_class_name = ancestor_names.find do |ancestor_class_name| CONNECTION_IMPLEMENTATIONS.include? ancestor_class_name end if implementation_class_name.nil? raise("No connection implementation to wrap #{nodes.class} (#{nodes})") else CONNECTION_IMPLEMENTATIONS[implementation_class_name] 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
44 45 46 |
# File 'lib/graphql/relay/base_connection.rb', line 44 def register_connection_implementation(nodes_class, connection_class) CONNECTION_IMPLEMENTATIONS[nodes_class.name] = connection_class end |
Instance Method Details
#after ⇒ Object
The value passed as after:
, if there was one
76 77 78 79 80 |
# File 'lib/graphql/relay/base_connection.rb', line 76 METHODS_FROM_ARGUMENTS.each do |arg_name| define_method(arg_name) do arguments[arg_name] end end |
#before ⇒ Object
The value passed as before:
, if there was one
76 77 78 79 80 |
# File 'lib/graphql/relay/base_connection.rb', line 76 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.
122 123 124 |
# File 'lib/graphql/relay/base_connection.rb', line 122 def cursor_from_node(object) raise NotImplementedError, "must return a cursor for this object/connection pair" end |
#edge_nodes ⇒ Object
These are the nodes to render for this connection, probably wrapped by Edge
84 85 86 |
# File 'lib/graphql/relay/base_connection.rb', line 84 def edge_nodes @edge_nodes ||= paged_nodes end |
#end_cursor ⇒ Object
Used by pageInfo
113 114 115 116 117 118 119 |
# File 'lib/graphql/relay/base_connection.rb', line 113 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 ⇒ Object
The value passed as first:
, if there was one
76 77 78 79 80 |
# File 'lib/graphql/relay/base_connection.rb', line 76 METHODS_FROM_ARGUMENTS.each do |arg_name| define_method(arg_name) do arguments[arg_name] end end |
#has_next_page ⇒ Object
Used by pageInfo
94 95 96 |
# File 'lib/graphql/relay/base_connection.rb', line 94 def has_next_page !!(first && sliced_nodes.count > first) end |
#has_previous_page ⇒ Object
Used by pageInfo
99 100 101 |
# File 'lib/graphql/relay/base_connection.rb', line 99 def has_previous_page !!(last && sliced_nodes.count > last) end |
#last ⇒ Object
The value passed as last:
, if there was one
76 77 78 79 80 |
# File 'lib/graphql/relay/base_connection.rb', line 76 METHODS_FROM_ARGUMENTS.each do |arg_name| define_method(arg_name) do arguments[arg_name] end end |
#page_info ⇒ Object
Support the pageInfo
field
89 90 91 |
# File 'lib/graphql/relay/base_connection.rb', line 89 def page_info self end |
#start_cursor ⇒ Object
Used by pageInfo
104 105 106 107 108 109 110 |
# File 'lib/graphql/relay/base_connection.rb', line 104 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 |