Skip to content

Commit 00e54e1

Browse files
committed
Support custom .connection_extension
1 parent b4e7ff3 commit 00e54e1

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/graphql/schema/field.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Field
1414
include GraphQL::Schema::Member::AcceptsDefinition
1515
include GraphQL::Schema::Member::HasArguments
1616
include GraphQL::Schema::Member::HasPath
17+
extend GraphQL::Schema::FindInheritedValue
1718

1819
# @return [String] the GraphQL name for this field, camelized unless `camelize: false` is provided
1920
attr_reader :name
@@ -135,6 +136,22 @@ def scoped?
135136
end
136137
end
137138

139+
# This extension is applied to fields when {#connection?} is true.
140+
#
141+
# You can override it in your base field definition.
142+
# @return [Class] A {FieldExtension} subclass for implementing pagination behavior.
143+
# @example Configuring a custom extension
144+
# class Types::BaseField < GraphQL::Schema::Field
145+
# connection_extension(MyCustomExtension)
146+
# end
147+
def self.connection_extension(new_extension_class = nil)
148+
if new_extension_class
149+
@connection_extension = new_extension_class
150+
else
151+
@connection_extension ||= find_inherited_value(:connection_extension, ConnectionExtension)
152+
end
153+
end
154+
138155
# @param name [Symbol] The underscore-cased version of this field name (will be camelized for the GraphQL API)
139156
# @param type [Class, GraphQL::BaseType, Array] The return type of this field
140157
# @param owner [Class] The type that this field belongs to
@@ -246,7 +263,7 @@ def initialize(type: nil, name: nil, owner: nil, null: nil, field: nil, function
246263
# The problem with putting this after the definition_block
247264
# is that it would override arguments
248265
if connection?
249-
self.extension(ConnectionExtension)
266+
self.extension(self.class.connection_extension)
250267
end
251268

252269
if definition_block

spec/graphql/schema/field_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,34 @@ def company(id:)
349349
assert_equal "1", res["data"]["company"]["id"]
350350
end
351351
end
352+
353+
describe ".connection_extension" do
354+
class CustomConnectionExtension < GraphQL::Schema::Field::ConnectionExtension
355+
def apply
356+
super
357+
field.argument(:z, String, required: false)
358+
end
359+
end
360+
361+
class CustomExtensionField < GraphQL::Schema::Field
362+
connection_extension(CustomConnectionExtension)
363+
end
364+
365+
class CustomExtensionObject < GraphQL::Schema::Object
366+
field_class CustomExtensionField
367+
368+
field :ints, GraphQL::Types::Int.connection_type, null: false, scope: false
369+
end
370+
371+
it "can be customized" do
372+
field = CustomExtensionObject.fields["ints"]
373+
assert_equal [CustomConnectionExtension], field.extensions.map(&:class)
374+
assert_equal ["after", "before", "first", "last", "z"], field.arguments.keys.sort
375+
end
376+
377+
it "can be inherited" do
378+
child_field_class = Class.new(CustomExtensionField)
379+
assert_equal CustomConnectionExtension, child_field_class.connection_extension
380+
end
381+
end
352382
end

0 commit comments

Comments
 (0)