Skip to content

Commit 4eff70c

Browse files
author
Robert Mosolgo
authored
Merge pull request #3438 from rmosolgo/fix-field-page-size-override
Fix when field max_page_size override is nil
2 parents 58cd563 + 56f6668 commit 4eff70c

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/graphql/pagination/connections.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def wrap(field, parent, items, arguments, context)
7979
context: context,
8080
parent: parent,
8181
field: field,
82-
max_page_size: field.max_page_size || context.schema.default_max_page_size,
82+
max_page_size: field.has_max_page_size? ? field.max_page_size : context.schema.default_max_page_size,
8383
first: arguments[:first],
8484
after: arguments[:after],
8585
last: arguments[:last],

spec/graphql/pagination/connections_spec.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class OtherArrayConnection < GraphQL::Pagination::ArrayConnection; end
3535
end
3636

3737
it "returns connections by class, using inherited mappings and local overrides" do
38-
field_defn = OpenStruct.new(max_page_size: 10, type: GraphQL::Types::Relay::BaseConnection)
38+
field_defn = OpenStruct.new(has_max_page_size?: true, max_page_size: 10, type: GraphQL::Types::Relay::BaseConnection)
3939

4040
set_wrapper = schema.connections.wrap(field_defn, nil, Set.new([1,2,3]), {}, nil)
4141
assert_instance_of SetConnection, set_wrapper
@@ -114,12 +114,45 @@ def things2
114114

115115
it "lets unrelated NoMethodErrors bubble up" do
116116
err = assert_raises NoMethodError do
117-
pp ConnectionErrorTestSchema.execute("{ things2 { name } }")
117+
ConnectionErrorTestSchema.execute("{ things2 { name } }")
118118
end
119119

120120
assert_includes err.message, "undefined method `no_such_method' for <BadThing!>"
121121
end
122122

123+
it "uses a field's `max_page_size: nil` configuration" do
124+
user_type = Class.new(GraphQL::Schema::Object) do
125+
graphql_name 'User'
126+
field :name, String, null: false
127+
end
128+
129+
query_type = Class.new(GraphQL::Schema::Object) do
130+
graphql_name 'Query'
131+
field :users, user_type.connection_type, null: true, max_page_size: nil
132+
def users
133+
[{ name: 'Yoda' }, { name: 'Anakin' }, { name: 'Obi Wan' }]
134+
end
135+
end
136+
137+
schema = Class.new(GraphQL::Schema) do
138+
# This value should be overriden by `max_page_size: nil` in the field definition above
139+
default_max_page_size 2
140+
query(query_type)
141+
end
142+
143+
res = schema.execute(<<-GRAPHQL).to_h
144+
{
145+
users {
146+
nodes {
147+
name
148+
}
149+
}
150+
}
151+
GRAPHQL
152+
153+
assert_equal ["Yoda", "Anakin", "Obi Wan"], res['data']['users']['nodes'].map { |node| node['name'] }
154+
end
155+
123156
class SingleNewConnectionSchema < GraphQL::Schema
124157
class Query < GraphQL::Schema::Object
125158
field :strings, GraphQL::Types::String.connection_type, null: false

0 commit comments

Comments
 (0)