graphql 1.9.17 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/graphql/templates/schema.erb +7 -0
- data/lib/graphql/analysis/ast/field_usage.rb +1 -1
- data/lib/graphql/analysis/ast/visitor.rb +3 -3
- data/lib/graphql/analysis/ast.rb +12 -11
- data/lib/graphql/argument.rb +7 -35
- data/lib/graphql/backtrace/table.rb +10 -2
- data/lib/graphql/base_type.rb +4 -0
- data/lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb +5 -9
- data/lib/graphql/define/assign_enum_value.rb +1 -1
- data/lib/graphql/define/assign_object_field.rb +3 -3
- data/lib/graphql/define/defined_object_proxy.rb +8 -2
- data/lib/graphql/define/instance_definable.rb +10 -106
- data/lib/graphql/directive/deprecated_directive.rb +1 -12
- data/lib/graphql/directive.rb +4 -1
- data/lib/graphql/enum_type.rb +5 -71
- data/lib/graphql/execution/directive_checks.rb +2 -2
- data/lib/graphql/execution/errors.rb +2 -3
- data/lib/graphql/execution/execute.rb +1 -1
- data/lib/graphql/execution/interpreter/runtime.rb +106 -55
- data/lib/graphql/execution/interpreter.rb +5 -11
- data/lib/graphql/execution/lazy/lazy_method_map.rb +4 -0
- data/lib/graphql/execution/lookahead.rb +5 -5
- data/lib/graphql/execution/multiplex.rb +13 -3
- data/lib/graphql/field.rb +9 -117
- data/lib/graphql/filter.rb +1 -1
- data/lib/graphql/function.rb +1 -30
- data/lib/graphql/input_object_type.rb +2 -24
- data/lib/graphql/interface_type.rb +2 -23
- data/lib/graphql/introspection/base_object.rb +2 -5
- data/lib/graphql/introspection/directive_type.rb +1 -1
- data/lib/graphql/introspection/entry_points.rb +7 -7
- data/lib/graphql/introspection/input_value_type.rb +27 -9
- data/lib/graphql/introspection/schema_type.rb +1 -6
- data/lib/graphql/introspection/type_type.rb +5 -5
- data/lib/graphql/language/definition_slice.rb +21 -10
- data/lib/graphql/language/document_from_schema_definition.rb +50 -44
- data/lib/graphql/language/nodes.rb +3 -3
- data/lib/graphql/language/parser.rb +644 -646
- data/lib/graphql/language/parser.y +6 -4
- data/lib/graphql/language.rb +1 -1
- data/lib/graphql/non_null_type.rb +0 -10
- data/lib/graphql/object_type.rb +1 -21
- data/lib/graphql/pagination/active_record_relation_connection.rb +35 -0
- data/lib/graphql/pagination/array_connection.rb +77 -0
- data/lib/graphql/pagination/connection.rb +171 -0
- data/lib/graphql/pagination/connections.rb +108 -0
- data/lib/graphql/pagination/mongoid_relation_connection.rb +25 -0
- data/lib/graphql/pagination/relation_connection.rb +151 -0
- data/lib/graphql/pagination/sequel_dataset_connection.rb +28 -0
- data/lib/graphql/pagination.rb +6 -0
- data/lib/graphql/query/arguments.rb +2 -1
- data/lib/graphql/query/context.rb +2 -5
- data/lib/graphql/query/literal_input.rb +30 -10
- data/lib/graphql/query/variable_validation_error.rb +1 -1
- data/lib/graphql/query/variables.rb +7 -3
- data/lib/graphql/query.rb +9 -5
- data/lib/graphql/relay/base_connection.rb +4 -0
- data/lib/graphql/relay/connection_type.rb +2 -1
- data/lib/graphql/relay/edge_type.rb +1 -0
- data/lib/graphql/relay/edges_instrumentation.rb +1 -1
- data/lib/graphql/relay/mutation.rb +1 -86
- data/lib/graphql/relay/node.rb +2 -2
- data/lib/graphql/scalar_type.rb +1 -58
- data/lib/graphql/schema/argument.rb +51 -6
- data/lib/graphql/schema/build_from_definition/resolve_map/default_resolve.rb +1 -1
- data/lib/graphql/schema/build_from_definition/resolve_map.rb +10 -4
- data/lib/graphql/schema/build_from_definition.rb +167 -178
- data/lib/graphql/schema/built_in_types.rb +5 -5
- data/lib/graphql/schema/directive/deprecated.rb +18 -0
- data/lib/graphql/schema/directive.rb +28 -2
- data/lib/graphql/schema/enum.rb +40 -3
- data/lib/graphql/schema/enum_value.rb +5 -1
- data/lib/graphql/schema/field/connection_extension.rb +11 -1
- data/lib/graphql/schema/field.rb +59 -31
- data/lib/graphql/schema/find_inherited_value.rb +13 -0
- data/lib/graphql/schema/finder.rb +13 -11
- data/lib/graphql/schema/input_object.rb +107 -2
- data/lib/graphql/schema/interface.rb +10 -7
- data/lib/graphql/schema/introspection_system.rb +108 -37
- data/lib/graphql/schema/late_bound_type.rb +1 -0
- data/lib/graphql/schema/list.rb +41 -0
- data/lib/graphql/schema/loader.rb +16 -4
- data/lib/graphql/schema/member/base_dsl_methods.rb +21 -11
- data/lib/graphql/schema/member/build_type.rb +5 -1
- data/lib/graphql/schema/member/cached_graphql_definition.rb +5 -0
- data/lib/graphql/schema/member/has_arguments.rb +2 -2
- data/lib/graphql/schema/member/has_ast_node.rb +17 -0
- data/lib/graphql/schema/member/has_fields.rb +4 -4
- data/lib/graphql/schema/member/validates_input.rb +33 -0
- data/lib/graphql/schema/member.rb +5 -0
- data/lib/graphql/schema/mutation.rb +1 -1
- data/lib/graphql/schema/non_null.rb +25 -0
- data/lib/graphql/schema/object.rb +15 -5
- data/lib/graphql/schema/printer.rb +1 -2
- data/lib/graphql/schema/relay_classic_mutation.rb +1 -1
- data/lib/graphql/schema/resolver.rb +3 -15
- data/lib/graphql/schema/scalar.rb +19 -3
- data/lib/graphql/schema/subscription.rb +5 -5
- data/lib/graphql/schema/traversal.rb +1 -1
- data/lib/graphql/schema/type_expression.rb +21 -13
- data/lib/graphql/schema/type_membership.rb +2 -2
- data/lib/graphql/schema/union.rb +2 -3
- data/lib/graphql/schema/validation.rb +2 -2
- data/lib/graphql/schema/warden.rb +45 -20
- data/lib/graphql/schema.rb +764 -151
- data/lib/graphql/static_validation/base_visitor.rb +10 -6
- data/lib/graphql/static_validation/rules/argument_literals_are_compatible.rb +9 -4
- data/lib/graphql/static_validation/rules/arguments_are_defined.rb +10 -7
- data/lib/graphql/static_validation/rules/directives_are_in_valid_locations.rb +1 -1
- data/lib/graphql/static_validation/rules/fields_are_defined_on_type.rb +4 -4
- data/lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb +5 -5
- data/lib/graphql/static_validation/rules/fields_will_merge.rb +4 -4
- data/lib/graphql/static_validation/rules/fragment_spreads_are_possible.rb +3 -3
- data/lib/graphql/static_validation/rules/required_input_object_attributes_are_present.rb +3 -3
- data/lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb +3 -3
- data/lib/graphql/static_validation/rules/variable_usages_are_allowed.rb +5 -6
- data/lib/graphql/static_validation/rules/variables_are_input_types.rb +1 -1
- data/lib/graphql/static_validation/rules/variables_are_used_and_defined.rb +1 -1
- data/lib/graphql/static_validation/type_stack.rb +2 -2
- data/lib/graphql/static_validation/validator.rb +1 -1
- data/lib/graphql/subscriptions/action_cable_subscriptions.rb +3 -3
- data/lib/graphql/subscriptions/event.rb +7 -4
- data/lib/graphql/subscriptions/instrumentation.rb +10 -5
- data/lib/graphql/subscriptions/subscription_root.rb +0 -1
- data/lib/graphql/subscriptions.rb +34 -9
- data/lib/graphql/tracing/active_support_notifications_tracing.rb +14 -10
- data/lib/graphql/tracing/appsignal_tracing.rb +8 -0
- data/lib/graphql/tracing/data_dog_tracing.rb +8 -0
- data/lib/graphql/tracing/new_relic_tracing.rb +8 -0
- data/lib/graphql/tracing/platform_tracing.rb +26 -6
- data/lib/graphql/tracing/prometheus_tracing.rb +8 -0
- data/lib/graphql/tracing/scout_tracing.rb +8 -0
- data/lib/graphql/tracing/skylight_tracing.rb +8 -0
- data/lib/graphql/tracing.rb +7 -3
- data/lib/graphql/types/int.rb +1 -1
- data/lib/graphql/types/relay/base_connection.rb +3 -1
- data/lib/graphql/union_type.rb +13 -28
- data/lib/graphql/unresolved_type_error.rb +2 -2
- data/lib/graphql/version.rb +1 -1
- data/lib/graphql.rb +2 -1
- metadata +15 -4
@@ -77,8 +77,11 @@ rule
|
|
77
77
|
}
|
78
78
|
|
79
79
|
type:
|
80
|
+
nullable_type { return val[0] }
|
81
|
+
| nullable_type BANG { return make_node(:NonNullType, of_type: val[0]) }
|
82
|
+
|
83
|
+
nullable_type:
|
80
84
|
name { return make_node(:TypeName, name: val[0])}
|
81
|
-
| type BANG { return make_node(:NonNullType, of_type: val[0]) }
|
82
85
|
| LBRACKET type RBRACKET { return make_node(:ListType, of_type: val[1]) }
|
83
86
|
|
84
87
|
default_value_opt:
|
@@ -165,7 +168,6 @@ rule
|
|
165
168
|
|
166
169
|
arguments_opt:
|
167
170
|
/* none */ { return EMPTY_ARRAY }
|
168
|
-
| LPAREN RPAREN { return EMPTY_ARRAY }
|
169
171
|
| LPAREN arguments_list RPAREN { return val[1] }
|
170
172
|
|
171
173
|
arguments_list:
|
@@ -187,7 +189,7 @@ rule
|
|
187
189
|
| object_literal_value
|
188
190
|
|
189
191
|
input_value:
|
190
|
-
|
192
|
+
literal_value
|
191
193
|
| variable
|
192
194
|
| object_value
|
193
195
|
|
@@ -452,7 +454,7 @@ def parse_document
|
|
452
454
|
# From the tokens, build an AST
|
453
455
|
@tracer.trace("parse", {query_string: @query_string}) do
|
454
456
|
if @tokens.empty?
|
455
|
-
|
457
|
+
raise GraphQL::ParseError.new("Unexpected end of document", nil, nil, @query_string)
|
456
458
|
else
|
457
459
|
do_parse
|
458
460
|
end
|
data/lib/graphql/language.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require "graphql/language/block_string"
|
3
3
|
require "graphql/language/printer"
|
4
|
-
require "graphql/language/definition_slice"
|
5
4
|
require "graphql/language/document_from_schema_definition"
|
6
5
|
require "graphql/language/generation"
|
7
6
|
require "graphql/language/lexer"
|
@@ -9,6 +8,7 @@ require "graphql/language/nodes"
|
|
9
8
|
require "graphql/language/parser"
|
10
9
|
require "graphql/language/token"
|
11
10
|
require "graphql/language/visitor"
|
11
|
+
require "graphql/language/definition_slice"
|
12
12
|
|
13
13
|
module GraphQL
|
14
14
|
module Language
|
@@ -1,8 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module GraphQL
|
3
|
-
class DoubleNonNullTypeError < GraphQL::Error
|
4
|
-
end
|
5
|
-
|
6
3
|
# A non-null type modifies another type.
|
7
4
|
#
|
8
5
|
# Non-null types can be created with `!` (`InnerType!`)
|
@@ -37,13 +34,6 @@ module GraphQL
|
|
37
34
|
|
38
35
|
attr_reader :of_type
|
39
36
|
def initialize(of_type:)
|
40
|
-
if of_type.is_a?(GraphQL::NonNullType)
|
41
|
-
raise(
|
42
|
-
DoubleNonNullTypeError,
|
43
|
-
"You tried to add a non-null constraint twice (!! instead of !)"
|
44
|
-
)
|
45
|
-
end
|
46
|
-
|
47
37
|
super()
|
48
38
|
@of_type = of_type
|
49
39
|
end
|
data/lib/graphql/object_type.rb
CHANGED
@@ -1,26 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module GraphQL
|
3
|
-
#
|
4
|
-
#
|
5
|
-
# @example defining a type for your IMDB clone
|
6
|
-
# MovieType = GraphQL::ObjectType.define do
|
7
|
-
# name "Movie"
|
8
|
-
# description "A full-length film or a short film"
|
9
|
-
# interfaces [ProductionInterface, DurationInterface]
|
10
|
-
#
|
11
|
-
# field :runtimeMinutes, !types.Int, property: :runtime_minutes
|
12
|
-
# field :director, PersonType
|
13
|
-
# field :cast, CastType
|
14
|
-
# field :starring, types[PersonType] do
|
15
|
-
# argument :limit, types.Int
|
16
|
-
# resolve ->(object, args, ctx) {
|
17
|
-
# stars = object.cast.stars
|
18
|
-
# args[:limit] && stars = stars.limit(args[:limit])
|
19
|
-
# stars
|
20
|
-
# }
|
21
|
-
# end
|
22
|
-
# end
|
23
|
-
#
|
3
|
+
# @api deprecated
|
24
4
|
class ObjectType < GraphQL::BaseType
|
25
5
|
accepts_definitions :interfaces, :fields, :mutation, :relay_node_type, field: GraphQL::Define::AssignObjectField
|
26
6
|
accepts_definitions implements: ->(type, *interfaces, inherit: false) { type.implements(interfaces, inherit: inherit) }
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "graphql/pagination/relation_connection"
|
3
|
+
|
4
|
+
module GraphQL
|
5
|
+
module Pagination
|
6
|
+
# Customizes `RelationConnection` to work with `ActiveRecord::Relation`s.
|
7
|
+
class ActiveRecordRelationConnection < Pagination::RelationConnection
|
8
|
+
def relation_count(relation)
|
9
|
+
if relation.respond_to?(:unscope)
|
10
|
+
relation.unscope(:order).count(:all)
|
11
|
+
else
|
12
|
+
# Rails 3
|
13
|
+
relation.count
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def relation_limit(relation)
|
18
|
+
relation.limit_value
|
19
|
+
end
|
20
|
+
|
21
|
+
def relation_offset(relation)
|
22
|
+
relation.offset_value
|
23
|
+
end
|
24
|
+
|
25
|
+
def null_relation(relation)
|
26
|
+
if relation.respond_to?(:none)
|
27
|
+
relation.none
|
28
|
+
else
|
29
|
+
# Rails 3
|
30
|
+
relation.where("1=2")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "graphql/pagination/connection"
|
3
|
+
|
4
|
+
module GraphQL
|
5
|
+
module Pagination
|
6
|
+
class ArrayConnection < Pagination::Connection
|
7
|
+
def nodes
|
8
|
+
load_nodes
|
9
|
+
@nodes
|
10
|
+
end
|
11
|
+
|
12
|
+
def has_previous_page
|
13
|
+
load_nodes
|
14
|
+
@has_previous_page
|
15
|
+
end
|
16
|
+
|
17
|
+
def has_next_page
|
18
|
+
load_nodes
|
19
|
+
@has_next_page
|
20
|
+
end
|
21
|
+
|
22
|
+
def cursor_for(item)
|
23
|
+
idx = items.find_index(item) + 1
|
24
|
+
context.schema.cursor_encoder.encode(idx.to_s)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def index_from_cursor(cursor)
|
30
|
+
decode(cursor).to_i
|
31
|
+
end
|
32
|
+
|
33
|
+
# Populate all the pagination info _once_,
|
34
|
+
# It doesn't do anything on subsequent calls.
|
35
|
+
def load_nodes
|
36
|
+
@nodes ||= begin
|
37
|
+
sliced_nodes = if before && after
|
38
|
+
items[index_from_cursor(after)..index_from_cursor(before)-1] || []
|
39
|
+
elsif before
|
40
|
+
items[0..index_from_cursor(before)-2] || []
|
41
|
+
elsif after
|
42
|
+
items[index_from_cursor(after)..-1] || []
|
43
|
+
else
|
44
|
+
items
|
45
|
+
end
|
46
|
+
|
47
|
+
@has_previous_page = if last
|
48
|
+
# There are items preceding the ones in this result
|
49
|
+
sliced_nodes.count > last
|
50
|
+
elsif after
|
51
|
+
# We've paginated into the Array a bit, there are some behind us
|
52
|
+
index_from_cursor(after) > 0
|
53
|
+
else
|
54
|
+
false
|
55
|
+
end
|
56
|
+
|
57
|
+
@has_next_page = if first
|
58
|
+
# There are more items after these items
|
59
|
+
sliced_nodes.count > first
|
60
|
+
elsif before
|
61
|
+
# The original array is longer than the `before` index
|
62
|
+
index_from_cursor(before) < items.length
|
63
|
+
else
|
64
|
+
false
|
65
|
+
end
|
66
|
+
|
67
|
+
limited_nodes = sliced_nodes
|
68
|
+
|
69
|
+
limited_nodes = limited_nodes.first(first) if first
|
70
|
+
limited_nodes = limited_nodes.last(last) if last
|
71
|
+
|
72
|
+
limited_nodes
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module Pagination
|
5
|
+
# A Connection wraps a list of items and provides cursor-based pagination over it.
|
6
|
+
#
|
7
|
+
# Connections were introduced by Facebook's `Relay` front-end framework, but
|
8
|
+
# proved to be generally useful for GraphQL APIs. When in doubt, use connections
|
9
|
+
# to serve lists (like Arrays, ActiveRecord::Relations) via GraphQL.
|
10
|
+
#
|
11
|
+
# Unlike the previous connection implementation, these default to bidirectional pagination.
|
12
|
+
#
|
13
|
+
# Pagination arguments and context may be provided at initialization or assigned later (see {Schema::Field::ConnectionExtension}).
|
14
|
+
class Connection
|
15
|
+
class PaginationImplementationMissingError < GraphQL::Error
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [Class] The class to use for wrapping items as `edges { ... }`. Defaults to `Connection::Edge`
|
19
|
+
def self.edge_class
|
20
|
+
self::Edge
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [Object] A list object, from the application. This is the unpaginated value passed into the connection.
|
24
|
+
attr_reader :items
|
25
|
+
|
26
|
+
# @return [GraphQL::Query::Context]
|
27
|
+
attr_accessor :context
|
28
|
+
|
29
|
+
# Raw access to client-provided values. (`max_page_size` not applied to first or last.)
|
30
|
+
attr_accessor :before_value, :after_value, :first_value, :last_value
|
31
|
+
|
32
|
+
# @return [String, nil] the client-provided cursor
|
33
|
+
def before
|
34
|
+
@before_value
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [String, nil] the client-provided cursor
|
38
|
+
def after
|
39
|
+
@after_value
|
40
|
+
end
|
41
|
+
|
42
|
+
# @param items [Object] some unpaginated collection item, like an `Array` or `ActiveRecord::Relation`
|
43
|
+
# @param context [Query::Context]
|
44
|
+
# @param first [Integer, nil] The limit parameter from the client, if it provided one
|
45
|
+
# @param after [String, nil] A cursor for pagination, if the client provided one
|
46
|
+
# @param last [Integer, nil] Limit parameter from the client, if provided
|
47
|
+
# @param before [String, nil] A cursor for pagination, if the client provided one.
|
48
|
+
# @param max_page_size [Integer, nil] A configured value to cap the result size. Applied as `first` if neither first or last are given.
|
49
|
+
def initialize(items, context: nil, first: nil, after: nil, max_page_size: nil, last: nil, before: nil)
|
50
|
+
@items = items
|
51
|
+
@context = context
|
52
|
+
@first_value = first
|
53
|
+
@after_value = after
|
54
|
+
@last_value = last
|
55
|
+
@before_value = before
|
56
|
+
@max_page_size = max_page_size
|
57
|
+
end
|
58
|
+
|
59
|
+
attr_writer :max_page_size
|
60
|
+
def max_page_size
|
61
|
+
@max_page_size ||= context.schema.default_max_page_size
|
62
|
+
end
|
63
|
+
|
64
|
+
attr_writer :first
|
65
|
+
# @return [Integer, nil]
|
66
|
+
# A clamped `first` value.
|
67
|
+
# (The underlying instance variable doesn't have limits on it.)
|
68
|
+
# If neither `first` nor `last` is given, but `max_page_size` is present, max_page_size is used for first.
|
69
|
+
def first
|
70
|
+
@first ||= begin
|
71
|
+
capped = limit_pagination_argument(@first_value, max_page_size)
|
72
|
+
if capped.nil? && last.nil?
|
73
|
+
capped = max_page_size
|
74
|
+
end
|
75
|
+
capped
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
attr_writer :last
|
80
|
+
# @return [Integer, nil] A clamped `last` value. (The underlying instance variable doesn't have limits on it)
|
81
|
+
def last
|
82
|
+
@last ||= limit_pagination_argument(@last_value, max_page_size)
|
83
|
+
end
|
84
|
+
|
85
|
+
# @return [Array<Edge>] {nodes}, but wrapped with Edge instances
|
86
|
+
def edges
|
87
|
+
@edges ||= nodes.map { |n| self.class.edge_class.new(n, self) }
|
88
|
+
end
|
89
|
+
|
90
|
+
# @return [Array<Object>] A slice of {items}, constrained by {@first_value}/{@after_value}/{@last_value}/{@before_value}
|
91
|
+
def nodes
|
92
|
+
raise PaginationImplementationMissingError, "Implement #{self.class}#nodes to paginate `@items`"
|
93
|
+
end
|
94
|
+
|
95
|
+
# A dynamic alias for compatibility with {Relay::BaseConnection}.
|
96
|
+
# @deprecated use {#nodes} instead
|
97
|
+
def edge_nodes
|
98
|
+
nodes
|
99
|
+
end
|
100
|
+
|
101
|
+
# The connection object itself implements `PageInfo` fields
|
102
|
+
def page_info
|
103
|
+
self
|
104
|
+
end
|
105
|
+
|
106
|
+
# @return [Boolean] True if there are more items after this page
|
107
|
+
def has_next_page
|
108
|
+
raise PaginationImplementationMissingError, "Implement #{self.class}#has_next_page to return the next-page check"
|
109
|
+
end
|
110
|
+
|
111
|
+
# @return [Boolean] True if there were items before these items
|
112
|
+
def has_previous_page
|
113
|
+
raise PaginationImplementationMissingError, "Implement #{self.class}#has_previous_page to return the previous-page check"
|
114
|
+
end
|
115
|
+
|
116
|
+
# @return [String] The cursor of the first item in {nodes}
|
117
|
+
def start_cursor
|
118
|
+
nodes.first && cursor_for(nodes.first)
|
119
|
+
end
|
120
|
+
|
121
|
+
# @return [String] The cursor of the last item in {nodes}
|
122
|
+
def end_cursor
|
123
|
+
nodes.last && cursor_for(nodes.last)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Return a cursor for this item.
|
127
|
+
# @param item [Object] one of the passed in {items}, taken from {nodes}
|
128
|
+
# @return [String]
|
129
|
+
def cursor_for(item)
|
130
|
+
raise PaginationImplementationMissingError, "Implement #{self.class}#cursor_for(item) to return the cursor for #{item.inspect}"
|
131
|
+
end
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
# @param argument [nil, Integer] `first` or `last`, as provided by the client
|
136
|
+
# @param max_page_size [nil, Integer]
|
137
|
+
# @return [nil, Integer] `nil` if the input was `nil`, otherwise a value between `0` and `max_page_size`
|
138
|
+
def limit_pagination_argument(argument, max_page_size)
|
139
|
+
if argument
|
140
|
+
if argument < 0
|
141
|
+
argument = 0
|
142
|
+
elsif max_page_size && argument > max_page_size
|
143
|
+
argument = max_page_size
|
144
|
+
end
|
145
|
+
end
|
146
|
+
argument
|
147
|
+
end
|
148
|
+
|
149
|
+
def decode(cursor)
|
150
|
+
context.schema.cursor_encoder.decode(cursor)
|
151
|
+
end
|
152
|
+
|
153
|
+
# A wrapper around paginated items. It includes a {cursor} for pagination
|
154
|
+
# and could be extended with custom relationship-level data.
|
155
|
+
class Edge
|
156
|
+
def initialize(item, connection)
|
157
|
+
@connection = connection
|
158
|
+
@item = item
|
159
|
+
end
|
160
|
+
|
161
|
+
def node
|
162
|
+
@item
|
163
|
+
end
|
164
|
+
|
165
|
+
def cursor
|
166
|
+
@connection.cursor_for(@item)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module Pagination
|
5
|
+
# A schema-level connection wrapper manager.
|
6
|
+
#
|
7
|
+
# Attach as a plugin.
|
8
|
+
#
|
9
|
+
# @example Using new default connections
|
10
|
+
# class MySchema < GraphQL::Schema
|
11
|
+
# use GraphQL::Pagination::Connections
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# @example Adding a custom wrapper
|
15
|
+
# class MySchema < GraphQL::Schema
|
16
|
+
# use GraphQL::Pagination::Connections
|
17
|
+
# connections.add(MyApp::SearchResults, MyApp::SearchResultsConnection)
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# @example Removing default connection support for arrays (they can still be manually wrapped)
|
21
|
+
# class MySchema < GraphQL::Schema
|
22
|
+
# use GraphQL::Pagination::Connections
|
23
|
+
# connections.delete(Array)
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# @see {Schema.connections}
|
27
|
+
class Connections
|
28
|
+
class ImplementationMissingError < GraphQL::Error
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.use(schema_defn)
|
32
|
+
if schema_defn.is_a?(Class)
|
33
|
+
schema_defn.connections = self.new
|
34
|
+
else
|
35
|
+
# Unwrap a `.define` object
|
36
|
+
schema_defn = schema_defn.target
|
37
|
+
schema_defn.connections = self.new
|
38
|
+
schema_defn.class.connections = schema_defn.connections
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def initialize
|
43
|
+
@wrappers = {}
|
44
|
+
add_default
|
45
|
+
end
|
46
|
+
|
47
|
+
def add(nodes_class, implementation)
|
48
|
+
@wrappers[nodes_class] = implementation
|
49
|
+
end
|
50
|
+
|
51
|
+
def delete(nodes_class)
|
52
|
+
@wrappers.delete(nodes_class)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Used by the runtime to wrap values in connection wrappers.
|
56
|
+
# @api Private
|
57
|
+
def wrap(field, object, arguments, context)
|
58
|
+
impl = nil
|
59
|
+
object.class.ancestors.each { |cls|
|
60
|
+
impl = @wrappers[cls]
|
61
|
+
break if impl
|
62
|
+
}
|
63
|
+
|
64
|
+
if impl.nil?
|
65
|
+
raise ImplementationMissingError, "Couldn't find a connection wrapper for #{object.class} during #{field.path} (#{object.inspect})"
|
66
|
+
end
|
67
|
+
|
68
|
+
impl.new(
|
69
|
+
object,
|
70
|
+
context: context,
|
71
|
+
max_page_size: field.max_page_size || context.schema.default_max_page_size,
|
72
|
+
first: arguments[:first],
|
73
|
+
after: arguments[:after],
|
74
|
+
last: arguments[:last],
|
75
|
+
before: arguments[:before],
|
76
|
+
)
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def add_default
|
82
|
+
add(Array, Pagination::ArrayConnection)
|
83
|
+
|
84
|
+
if defined?(ActiveRecord::Relation)
|
85
|
+
add(ActiveRecord::Relation, Pagination::ActiveRecordRelationConnection)
|
86
|
+
end
|
87
|
+
|
88
|
+
if defined?(Sequel::Dataset)
|
89
|
+
add(Sequel::Dataset, Pagination::SequelDatasetConnection)
|
90
|
+
end
|
91
|
+
|
92
|
+
if defined?(Mongoid::Criteria)
|
93
|
+
add(Mongoid::Criteria, Pagination::MongoidRelationConnection)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Mongoid 5 and 6
|
97
|
+
if defined?(Mongoid::Relations::Targets::Enumerable)
|
98
|
+
add(Mongoid::Relations::Targets::Enumerable, Pagination::MongoidRelationConnection)
|
99
|
+
end
|
100
|
+
|
101
|
+
# Mongoid 7
|
102
|
+
if defined?(Mongoid::Association::Referenced::HasMany::Targets::Enumerable)
|
103
|
+
add(Mongoid::Association::Referenced::HasMany::Targets::Enumerable, Pagination::MongoidRelationConnection)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "graphql/pagination/relation_connection"
|
3
|
+
|
4
|
+
module GraphQL
|
5
|
+
module Pagination
|
6
|
+
class MongoidRelationConnection < Pagination::RelationConnection
|
7
|
+
def relation_offset(relation)
|
8
|
+
relation.options.skip
|
9
|
+
end
|
10
|
+
|
11
|
+
def relation_limit(relation)
|
12
|
+
relation.options.limit
|
13
|
+
end
|
14
|
+
|
15
|
+
def relation_count(relation)
|
16
|
+
# Mongo's `.count` doesn't apply limit or skip, which we need. So we have to load _everything_!
|
17
|
+
relation.to_a.count
|
18
|
+
end
|
19
|
+
|
20
|
+
def null_relation(relation)
|
21
|
+
relation.without_options.none
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "graphql/pagination/connection"
|
3
|
+
|
4
|
+
module GraphQL
|
5
|
+
module Pagination
|
6
|
+
# A generic class for working with database query objects.
|
7
|
+
class RelationConnection < Pagination::Connection
|
8
|
+
def nodes
|
9
|
+
load_nodes
|
10
|
+
@nodes
|
11
|
+
end
|
12
|
+
|
13
|
+
def has_previous_page
|
14
|
+
load_nodes
|
15
|
+
@has_previous_page
|
16
|
+
end
|
17
|
+
|
18
|
+
def has_next_page
|
19
|
+
load_nodes
|
20
|
+
@has_next_page
|
21
|
+
end
|
22
|
+
|
23
|
+
def cursor_for(item)
|
24
|
+
load_nodes
|
25
|
+
# index in nodes + existing offset + 1 (because it's offset, not index)
|
26
|
+
offset = nodes.index(item) + 1 + (@paged_nodes_offset || 0) + (relation_offset(items) || 0)
|
27
|
+
context.schema.cursor_encoder.encode(offset.to_s)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# @param relation [Object] A database query object
|
33
|
+
# @return [Integer, nil] The offset value, or nil if there isn't one
|
34
|
+
def relation_offset(relation)
|
35
|
+
raise "#{self.class}#relation_offset(relation) must return the offset value for a #{relation.class} (#{relation.inspect})"
|
36
|
+
end
|
37
|
+
|
38
|
+
# @param relation [Object] A database query object
|
39
|
+
# @return [Integer, nil] The limit value, or nil if there isn't one
|
40
|
+
def relation_limit(relation)
|
41
|
+
raise "#{self.class}#relation_limit(relation) must return the limit value for a #{relation.class} (#{relation.inspect})"
|
42
|
+
end
|
43
|
+
|
44
|
+
# @param relation [Object] A database query object
|
45
|
+
# @return [Integer, nil] The number of items in this relation (hopefully determined without loading all records into memory!)
|
46
|
+
def relation_count(relation)
|
47
|
+
raise "#{self.class}#relation_count(relation) must return the count of records for a #{relation.class} (#{relation.inspect})"
|
48
|
+
end
|
49
|
+
|
50
|
+
# @param relation [Object] A database query object
|
51
|
+
# @return [Object] A modified query object which will return no records
|
52
|
+
def null_relation(relation)
|
53
|
+
raise "#{self.class}#null_relation(relation) must return an empty relation for a #{relation.class} (#{relation.inspect})"
|
54
|
+
end
|
55
|
+
|
56
|
+
# @return [Integer]
|
57
|
+
def offset_from_cursor(cursor)
|
58
|
+
decode(cursor).to_i
|
59
|
+
end
|
60
|
+
|
61
|
+
# Abstract this operation so we can always ignore inputs less than zero.
|
62
|
+
# (Sequel doesn't like it, understandably.)
|
63
|
+
def set_offset(relation, offset_value)
|
64
|
+
if offset_value >= 0
|
65
|
+
relation.offset(offset_value)
|
66
|
+
else
|
67
|
+
relation.offset(0)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Abstract this operation so we can always ignore inputs less than zero.
|
72
|
+
# (Sequel doesn't like it, understandably.)
|
73
|
+
def set_limit(relation, limit_value)
|
74
|
+
if limit_value > 0
|
75
|
+
relation.limit(limit_value)
|
76
|
+
elsif limit_value == 0
|
77
|
+
null_relation(relation)
|
78
|
+
else
|
79
|
+
relation
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Populate all the pagination info _once_,
|
84
|
+
# It doesn't do anything on subsequent calls.
|
85
|
+
def load_nodes
|
86
|
+
@nodes ||= begin
|
87
|
+
paginated_nodes = items
|
88
|
+
after_offset = after && offset_from_cursor(after)
|
89
|
+
before_offset = before && offset_from_cursor(before)
|
90
|
+
|
91
|
+
if after_offset
|
92
|
+
previous_offset = relation_offset(items) || 0
|
93
|
+
paginated_nodes = set_offset(paginated_nodes, previous_offset + after_offset)
|
94
|
+
end
|
95
|
+
|
96
|
+
if before_offset && after_offset
|
97
|
+
if after_offset < before_offset
|
98
|
+
# Get the number of items between the two cursors
|
99
|
+
space_between = before_offset - after_offset - 1
|
100
|
+
paginated_nodes = set_limit(paginated_nodes, space_between)
|
101
|
+
else
|
102
|
+
# TODO I think this is untested
|
103
|
+
# The cursors overextend one another to an empty set
|
104
|
+
paginated_nodes = null_relation(paginated_nodes)
|
105
|
+
end
|
106
|
+
elsif before_offset
|
107
|
+
# Use limit to cut off the tail of the relation
|
108
|
+
paginated_nodes = set_limit(paginated_nodes, before_offset - 1)
|
109
|
+
end
|
110
|
+
|
111
|
+
sliced_nodes_count = relation_count(paginated_nodes)
|
112
|
+
|
113
|
+
if first && (relation_limit(paginated_nodes).nil? || relation_limit(paginated_nodes) > first)
|
114
|
+
# `first` would create a stricter limit that the one already applied, so add it
|
115
|
+
paginated_nodes = set_limit(paginated_nodes, first)
|
116
|
+
end
|
117
|
+
|
118
|
+
if last
|
119
|
+
if (lv = relation_limit(paginated_nodes))
|
120
|
+
if last <= lv
|
121
|
+
# `last` is a smaller slice than the current limit, so apply it
|
122
|
+
offset = (relation_offset(paginated_nodes) || 0) + (lv - last)
|
123
|
+
paginated_nodes = set_offset(paginated_nodes, offset)
|
124
|
+
paginated_nodes = set_limit(paginated_nodes, last)
|
125
|
+
end
|
126
|
+
else
|
127
|
+
# No limit, so get the last items
|
128
|
+
offset = (relation_offset(paginated_nodes) || 0) + sliced_nodes_count - [last, sliced_nodes_count].min
|
129
|
+
paginated_nodes = set_offset(paginated_nodes, offset)
|
130
|
+
paginated_nodes = set_limit(paginated_nodes, last)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
@has_next_page = !!(
|
135
|
+
(before_offset && before_offset > 0) ||
|
136
|
+
(first && sliced_nodes_count > first)
|
137
|
+
)
|
138
|
+
|
139
|
+
@has_previous_page = !!(
|
140
|
+
(after_offset && after_offset > 0) ||
|
141
|
+
(last && sliced_nodes_count > last)
|
142
|
+
)
|
143
|
+
|
144
|
+
@paged_nodes_offset = relation_offset(paginated_nodes)
|
145
|
+
# Return an array so we can consistently use `.index(node)` on it
|
146
|
+
paginated_nodes.to_a
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|