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
- {#object}, the object 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 classes -> connection_classes eg Array -> ArrayConnection
{}
- METHODS_FROM_ARGUMENTS =
Provide easy access to provided arguments:
[:first, :after, :last, :before, :order]
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#max_page_size ⇒ Object
readonly
Returns the value of attribute max_page_size.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
-
.connection_for_items(items) ⇒ subclass of BaseConnection
Find a connection implementation suitable for exposing ‘items`.
-
.create_type(wrapped_type, &block) ⇒ Object
Create a connection which exposes edges of this type.
-
.register_connection_implementation(items_class, connection_class) ⇒ Object
Add ‘connection_class` as the connection wrapper for `items_class` eg, `RelationConnection` is the implementation for `AR::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.
-
#edges ⇒ Object
Wrap nodes in Edges so they expose cursors.
-
#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(object, arguments, max_page_size: nil) ⇒ BaseConnection
constructor
Make a connection, wrapping ‘object`.
-
#last ⇒ Object
The value passed as ‘last:`, if there was one.
-
#order ⇒ Object
The value passed as ‘order:`, if there was one.
-
#page_info ⇒ Object
Support the ‘pageInfo` field.
Constructor Details
#initialize(object, arguments, max_page_size: nil) ⇒ BaseConnection
Make a connection, wrapping ‘object`
70 71 72 73 74 |
# File 'lib/graphql/relay/base_connection.rb', line 70 def initialize(object, arguments, max_page_size: nil) @object = object @arguments = arguments @max_page_size = max_page_size end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
64 65 66 |
# File 'lib/graphql/relay/base_connection.rb', line 64 def arguments @arguments end |
#max_page_size ⇒ Object (readonly)
Returns the value of attribute max_page_size.
64 65 66 |
# File 'lib/graphql/relay/base_connection.rb', line 64 def max_page_size @max_page_size end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
64 65 66 |
# File 'lib/graphql/relay/base_connection.rb', line 64 def object @object end |
Class Method Details
.connection_for_items(items) ⇒ subclass of BaseConnection
Find a connection implementation suitable for exposing ‘items`
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/graphql/relay/base_connection.rb', line 39 def self.connection_for_items(items) # We check class membership by comparing class names rather than # identity to prevent this from being broken by Rails autoloading. # Changes to the source file for ItemsClass in Rails apps cause it to be # reloaded as a new object, so if we were to use `is_a?` here, it would # no longer match any registered custom connection types. ancestor_names = items.class.ancestors.map(&:name) implementation = CONNECTION_IMPLEMENTATIONS.find do |items_class_name, connection_class| ancestor_names.include? items_class_name end if implementation.nil? raise("No connection implementation to wrap #{items.class} (#{items})") else implementation[1] end end |
.create_type(wrapped_type, &block) ⇒ Object
Create a connection which exposes edges of this type
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/graphql/relay/base_connection.rb', line 22 def self.create_type(wrapped_type, &block) edge_type = wrapped_type.edge_type connection_type = ObjectType.define do name("#{wrapped_type.name}Connection") field :edges, types[edge_type] field :pageInfo, PageInfo, property: :page_info block && instance_eval(&block) end connection_type end |
.register_connection_implementation(items_class, connection_class) ⇒ Object
Add ‘connection_class` as the connection wrapper for `items_class` eg, `RelationConnection` is the implementation for `AR::Relation`
60 61 62 |
# File 'lib/graphql/relay/base_connection.rb', line 60 def self.register_connection_implementation(items_class, connection_class) CONNECTION_IMPLEMENTATIONS[items_class.name] = connection_class end |
Instance Method Details
#after ⇒ Object
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 |
#before ⇒ Object
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.
116 117 118 |
# File 'lib/graphql/relay/base_connection.rb', line 116 def cursor_from_node(object) raise NotImplementedError, "must return a cursor for this object/connection pair" end |
#edges ⇒ Object
Wrap nodes in Edges so they expose cursors.
96 97 98 |
# File 'lib/graphql/relay/base_connection.rb', line 96 def edges @edges ||= paged_nodes.map { |item| Edge.new(item, self) } end |
#first ⇒ Object
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_page ⇒ Object
Used by ‘pageInfo`
106 107 108 |
# File 'lib/graphql/relay/base_connection.rb', line 106 def has_next_page !!(first && sliced_nodes.count > first) end |
#has_previous_page ⇒ Object
Used by ‘pageInfo`
111 112 113 |
# File 'lib/graphql/relay/base_connection.rb', line 111 def has_previous_page !!(last && sliced_nodes.count > last) end |
#last ⇒ Object
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 |
#order ⇒ Object
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_info ⇒ Object
Support the ‘pageInfo` field
101 102 103 |
# File 'lib/graphql/relay/base_connection.rb', line 101 def page_info self end |