Class: GraphQL::Pagination::Connection
- Inherits:
-
Object
- Object
- GraphQL::Pagination::Connection
- Defined in:
- lib/graphql/pagination/connection.rb
Overview
A Connection wraps a list of items and provides cursor-based pagination over it.
Connections were introduced by Facebook's Relay front-end framework, but
proved to be generally useful for GraphQL APIs. When in doubt, use connections
to serve lists (like Arrays, ActiveRecord::Relations) via GraphQL.
Unlike the previous connection implementation, these default to bidirectional pagination.
Pagination arguments and context may be provided at initialization or assigned later (see Schema::Field::ConnectionExtension).
Defined Under Namespace
Classes: Edge, PaginationImplementationMissingError
Instance Attribute Summary collapse
-
#after_value ⇒ Object
Raw access to client-provided values.
-
#arguments ⇒ Object
Returns.
-
#before_value ⇒ Object
Raw access to client-provided values.
-
#context ⇒ Object
Returns.
-
#edge_class ⇒ Object
Returns.
-
#field ⇒ Object
Returns.
-
#first ⇒ Object
Returns.
-
#first_value ⇒ Object
Raw access to client-provided values.
-
#items ⇒ Object
readonly
Returns.
-
#last ⇒ Object
Returns.
-
#last_value ⇒ Object
Raw access to client-provided values.
-
#lookahead ⇒ Object
Returns.
-
#parent ⇒ Object
Returns.
-
#was_authorized_by_scope_items ⇒ Object
writeonly
Sets the attribute was_authorized_by_scope_items.
Class Method Summary collapse
-
.custom_cursor_for?(default_owner) ⇒ Boolean
Subclasses may override
cursor_forto build their own cursors (for example, value-based cursors).
Instance Method Summary collapse
-
#after ⇒ Object
Returns.
-
#before ⇒ Object
Returns.
-
#cursor_for(item) ⇒ Object
Return a cursor for this item.
-
#cursor_for_position(item, _position) ⇒ Object
:nodoc:.
- #default_page_size ⇒ Object
- #default_page_size=(new_value) ⇒ Object
-
#edge_nodes ⇒ Object
A dynamic alias for compatibility with GraphQL::Types::Relay::BaseConnection.
-
#edges ⇒ Object
Returns.
-
#end_cursor ⇒ Object
Returns.
- #has_default_page_size_override? ⇒ Boolean
- #has_max_page_size_override? ⇒ Boolean
-
#has_next_page ⇒ Object
Returns.
-
#has_previous_page ⇒ Object
Returns.
-
#initialize(items, parent: nil, field: nil, context: nil, first: nil, after: nil, max_page_size: NOT_CONFIGURED, default_page_size: NOT_CONFIGURED, last: nil, before: nil, edge_class: nil, arguments: nil) ⇒ Connection
constructor
Parameters.
- #max_page_size ⇒ Object
- #max_page_size=(new_value) ⇒ Object
-
#nodes ⇒ Object
Returns.
-
#page_info ⇒ Object
The connection object itself implements
PageInfofields. -
#range_add_edge(item) ⇒ Object
This is called by
Relay::RangeAdd-- it can be overridden whenitemneeds some modifications based on this connection's state. -
#start_cursor ⇒ Object
Returns.
- #was_authorized_by_scope_items? ⇒ Boolean
Constructor Details
#initialize(items, parent: nil, field: nil, context: nil, first: nil, after: nil, max_page_size: NOT_CONFIGURED, default_page_size: NOT_CONFIGURED, last: nil, before: nil, edge_class: nil, arguments: nil) ⇒ Connection
Parameters
items(Object) — some unpaginated collection item, like anArrayorActiveRecord::Relationcontext(Query::Context)parent(Object) — The object this collection belongs tofirst(Integer, nil) — The limit parameter from the client, if it provided oneafter(String, nil) — A cursor for pagination, if the client provided onelast(Integer, nil) — Limit parameter from the client, if providedbefore(String, nil) — A cursor for pagination, if the client provided one.arguments(Hash) — The arguments to the field that returned the collection wrapped by this connectionmax_page_size(Integer, nil) — A configured value to cap the result size. Applied asfirstif neither first or last are given and nodefault_page_sizeis set.default_page_size(Integer, nil) — A configured value to determine the result size when neither first or last are given.
:call-seq:
initialize(Object items, Object parent:, field:, Query::Context context:, Integer | nil first:, String | nil after:, Integer | nil max_page_size:, Integer | nil default_page_size:, Integer | nil last:, String | nil before:, edge_class:, Hash arguments:)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/graphql/pagination/connection.rb', line 108 def initialize(items, parent: nil, field: nil, context: nil, first: nil, after: nil, max_page_size: NOT_CONFIGURED, default_page_size: NOT_CONFIGURED, last: nil, before: nil, edge_class: nil, arguments: nil) @items = items @parent = parent @context = context @field = field @first_value = first @after_value = after @last_value = last @before_value = before @arguments = arguments @edge_class = edge_class || self.class.const_get(:Edge) # This is only true if the object was _initialized_ with an override # or if one is assigned later. @has_max_page_size_override = max_page_size != NOT_CONFIGURED @max_page_size = if max_page_size == NOT_CONFIGURED nil else max_page_size end @has_default_page_size_override = default_page_size != NOT_CONFIGURED @default_page_size = if default_page_size == NOT_CONFIGURED nil else default_page_size end @was_authorized_by_scope_items = end |
Instance Attribute Details
#after_value ⇒ Object
Raw access to client-provided values. (max_page_size not applied to first or last.)
58 59 60 |
# File 'lib/graphql/pagination/connection.rb', line 58 def after_value @after_value end |
#arguments ⇒ Object
Returns
Hash<Symbol => Object>— The field arguments from the field that returned this connection
91 92 93 |
# File 'lib/graphql/pagination/connection.rb', line 91 def arguments @arguments end |
#before_value ⇒ Object
Raw access to client-provided values. (max_page_size not applied to first or last.)
58 59 60 |
# File 'lib/graphql/pagination/connection.rb', line 58 def before_value @before_value end |
#context ⇒ Object
Returns
GraphQL::Query::Context
42 43 44 |
# File 'lib/graphql/pagination/connection.rb', line 42 def context @context end |
#edge_class ⇒ Object
Returns
Class— A wrapper class for edges of this connection
238 239 240 |
# File 'lib/graphql/pagination/connection.rb', line 238 def edge_class @edge_class end |
#field ⇒ Object
Returns
GraphQL::Schema::Field— The field this connection was returned by
243 244 245 |
# File 'lib/graphql/pagination/connection.rb', line 243 def field @field end |
#first ⇒ Object
Returns
Integer, nil— A clampedfirstvalue. (The underlying instance variable doesn't have limits on it.) If neitherfirstnorlastis given, butdefault_page_sizeis present, default_page_size is used for first. Ifdefault_page_sizeis greater thanmax_page_size``, it'll be clamped down tomax_page_size. Ifdefault_page_sizeis nil, usemax_page_size`.
:call-seq:
first() -> Integer | nil
183 184 185 186 187 188 189 190 191 |
# File 'lib/graphql/pagination/connection.rb', line 183 def first @first ||= begin capped = limit_pagination_argument(@first_value, max_page_size) if capped.nil? && last.nil? capped = limit_pagination_argument(default_page_size, max_page_size) || max_page_size end capped end end |
#first_value ⇒ Object
Raw access to client-provided values. (max_page_size not applied to first or last.)
58 59 60 |
# File 'lib/graphql/pagination/connection.rb', line 58 def first_value @first_value end |
#items ⇒ Object (readonly)
Returns
Object— A list object, from the application. This is the unpaginated value passed into the connection.
37 38 39 |
# File 'lib/graphql/pagination/connection.rb', line 37 def items @items end |
#last ⇒ Object
Returns
Integer, nil— A clampedlastvalue. (The underlying instance variable doesn't have limits on it)
:call-seq:
last() -> Integer | nil
217 218 219 |
# File 'lib/graphql/pagination/connection.rb', line 217 def last @last ||= limit_pagination_argument(@last_value, max_page_size) end |
#last_value ⇒ Object
Raw access to client-provided values. (max_page_size not applied to first or last.)
58 59 60 |
# File 'lib/graphql/pagination/connection.rb', line 58 def last_value @last_value end |
#lookahead ⇒ Object
Returns
GraphQL::Execution::LookaheadSelections on this connection, set by connection fields
248 249 250 |
# File 'lib/graphql/pagination/connection.rb', line 248 def lookahead @lookahead end |
#parent ⇒ Object
Returns
Object— the object this collection belongs to
55 56 57 |
# File 'lib/graphql/pagination/connection.rb', line 55 def parent @parent end |
#was_authorized_by_scope_items=(value) ⇒ Object (writeonly)
Sets the attribute was_authorized_by_scope_items
136 137 138 |
# File 'lib/graphql/pagination/connection.rb', line 136 def (value) @was_authorized_by_scope_items = value end |
Class Method Details
.custom_cursor_for?(default_owner) ⇒ Boolean
Subclasses may override cursor_for to build their own cursors (for example, value-based cursors).
In that case, position-based cursors from the parent class would be wrong, so cursor_for_position
should call the overridden cursor_for instead.
The result is cached on each class, since it's the same for every instance.
26 27 28 29 30 31 |
# File 'lib/graphql/pagination/connection.rb', line 26 def custom_cursor_for?(default_owner) # :nodoc: if !defined?(@custom_cursor_for) @custom_cursor_for = instance_method(:cursor_for).owner != default_owner end @custom_cursor_for end |
Instance Method Details
#after ⇒ Object
Returns
String, nil— the client-provided cursor.""is treated asnil.
:call-seq:
after() -> String | nil
80 81 82 83 84 85 86 |
# File 'lib/graphql/pagination/connection.rb', line 80 def after if defined?(@after) @after else @after = @after_value == "" ? nil : @after_value end end |
#before ⇒ Object
Returns
String, nil— the client-provided cursor.""is treated asnil.
:call-seq:
before() -> String | nil
66 67 68 69 70 71 72 |
# File 'lib/graphql/pagination/connection.rb', line 66 def before if defined?(@before) @before else @before = @before_value == "" ? nil : @before_value end end |
#cursor_for(item) ⇒ Object
323 324 325 |
# File 'lib/graphql/pagination/connection.rb', line 323 def cursor_for(item) raise PaginationImplementationMissingError, "Implement #{self.class}#cursor_for(item) to return the cursor for #{item.inspect}" end |
#cursor_for_position(item, _position) ⇒ Object
:nodoc:
327 328 329 |
# File 'lib/graphql/pagination/connection.rb', line 327 def cursor_for_position(item, _position) # :nodoc: cursor_for(item) end |
#default_page_size ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/graphql/pagination/connection.rb', line 164 def default_page_size if @has_default_page_size_override @default_page_size else context.schema.default_page_size end end |
#default_page_size=(new_value) ⇒ Object
159 160 161 162 |
# File 'lib/graphql/pagination/connection.rb', line 159 def default_page_size=(new_value) @has_default_page_size_override = true @default_page_size = new_value end |
#edge_nodes ⇒ Object
A dynamic alias for compatibility with GraphQL::Types::Relay::BaseConnection. Deprecated: use nodes instead
262 263 264 |
# File 'lib/graphql/pagination/connection.rb', line 262 def edge_nodes nodes end |
#edges ⇒ Object
227 228 229 230 231 232 233 |
# File 'lib/graphql/pagination/connection.rb', line 227 def edges @edges ||= nodes.each_with_index.map do |n, index| edge = @edge_class.new(n, self) edge.graphql_connection_position = index edge end end |
#end_cursor ⇒ Object
307 308 309 |
# File 'lib/graphql/pagination/connection.rb', line 307 def end_cursor nodes.last && cursor_for_position(nodes.last, nodes.length - 1) end |
#has_default_page_size_override? ⇒ Boolean
172 173 174 |
# File 'lib/graphql/pagination/connection.rb', line 172 def has_default_page_size_override? @has_default_page_size_override end |
#has_max_page_size_override? ⇒ Boolean
155 156 157 |
# File 'lib/graphql/pagination/connection.rb', line 155 def has_max_page_size_override? @has_max_page_size_override end |
#has_next_page ⇒ Object
Returns
Boolean— True if there are more items after this page
:call-seq:
has_next_page() -> bool
277 278 279 |
# File 'lib/graphql/pagination/connection.rb', line 277 def has_next_page raise PaginationImplementationMissingError, "Implement #{self.class}#has_next_page to return the next-page check" end |
#has_previous_page ⇒ Object
Returns
Boolean— True if there were items before these items
:call-seq:
has_previous_page() -> bool
287 288 289 |
# File 'lib/graphql/pagination/connection.rb', line 287 def has_previous_page raise PaginationImplementationMissingError, "Implement #{self.class}#has_previous_page to return the previous-page check" end |
#max_page_size ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/graphql/pagination/connection.rb', line 147 def max_page_size if @has_max_page_size_override @max_page_size else context.schema.default_max_page_size end end |
#max_page_size=(new_value) ⇒ Object
142 143 144 145 |
# File 'lib/graphql/pagination/connection.rb', line 142 def max_page_size=(new_value) @has_max_page_size_override = true @max_page_size = new_value end |
#nodes ⇒ Object
Returns
Array<Object>— A slice of items, constrained by@first_value/@after_value/@last_value/@before_value
:call-seq:
nodes() -> Array[Object]
256 257 258 |
# File 'lib/graphql/pagination/connection.rb', line 256 def nodes raise PaginationImplementationMissingError, "Implement #{self.class}#nodes to paginate `@items`" end |
#page_info ⇒ Object
The connection object itself implements PageInfo fields
267 268 269 |
# File 'lib/graphql/pagination/connection.rb', line 267 def page_info self end |
#range_add_edge(item) ⇒ Object
This is called by Relay::RangeAdd -- it can be overridden
when item needs some modifications based on this connection's state.
Parameters
item(Object) — An item newly added toitems
Returns
Edge
:call-seq:
range_add_edge(Object item) -> Edge
206 207 208 |
# File 'lib/graphql/pagination/connection.rb', line 206 def range_add_edge(item) edge_class.new(item, self) end |
#start_cursor ⇒ Object
297 298 299 |
# File 'lib/graphql/pagination/connection.rb', line 297 def start_cursor nodes.first && cursor_for_position(nodes.first, 0) end |
#was_authorized_by_scope_items? ⇒ Boolean
138 139 140 |
# File 'lib/graphql/pagination/connection.rb', line 138 def @was_authorized_by_scope_items end |