Skip to content

Commit

Permalink
Merge pull request #2561 from rmosolgo/custom-connection-extension-class
Browse files Browse the repository at this point in the history
Support custom .connection_extension
  • Loading branch information
Robert Mosolgo authored Oct 24, 2019
2 parents b4e7ff3 + 00e54e1 commit 85ecd80
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/graphql/schema/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Field
include GraphQL::Schema::Member::AcceptsDefinition
include GraphQL::Schema::Member::HasArguments
include GraphQL::Schema::Member::HasPath
extend GraphQL::Schema::FindInheritedValue

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

# This extension is applied to fields when {#connection?} is true.
#
# You can override it in your base field definition.
# @return [Class] A {FieldExtension} subclass for implementing pagination behavior.
# @example Configuring a custom extension
# class Types::BaseField < GraphQL::Schema::Field
# connection_extension(MyCustomExtension)
# end
def self.connection_extension(new_extension_class = nil)
if new_extension_class
@connection_extension = new_extension_class
else
@connection_extension ||= find_inherited_value(:connection_extension, ConnectionExtension)
end
end

# @param name [Symbol] The underscore-cased version of this field name (will be camelized for the GraphQL API)
# @param type [Class, GraphQL::BaseType, Array] The return type of this field
# @param owner [Class] The type that this field belongs to
Expand Down Expand Up @@ -246,7 +263,7 @@ def initialize(type: nil, name: nil, owner: nil, null: nil, field: nil, function
# The problem with putting this after the definition_block
# is that it would override arguments
if connection?
self.extension(ConnectionExtension)
self.extension(self.class.connection_extension)
end

if definition_block
Expand Down
30 changes: 30 additions & 0 deletions spec/graphql/schema/field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,34 @@ def company(id:)
assert_equal "1", res["data"]["company"]["id"]
end
end

describe ".connection_extension" do
class CustomConnectionExtension < GraphQL::Schema::Field::ConnectionExtension
def apply
super
field.argument(:z, String, required: false)
end
end

class CustomExtensionField < GraphQL::Schema::Field
connection_extension(CustomConnectionExtension)
end

class CustomExtensionObject < GraphQL::Schema::Object
field_class CustomExtensionField

field :ints, GraphQL::Types::Int.connection_type, null: false, scope: false
end

it "can be customized" do
field = CustomExtensionObject.fields["ints"]
assert_equal [CustomConnectionExtension], field.extensions.map(&:class)
assert_equal ["after", "before", "first", "last", "z"], field.arguments.keys.sort
end

it "can be inherited" do
child_field_class = Class.new(CustomExtensionField)
assert_equal CustomConnectionExtension, child_field_class.connection_extension
end
end
end

0 comments on commit 85ecd80

Please sign in to comment.