the GraphQL::Schema::Field initialize , ` type` is nil after upgrading GraphQL to the latest version?
xiaoduwuer opened this issue · 5 comments
/After upgrading to the latest version of GraphQL Ruby, I have defined a field variants and used a resolver for it. However, kwargs[:type] in BaseField is nil when using the resolver. When I directly specify the type in the field definition, kwargs[:type] is correctly set.
Here are the relevant code snippets:
Types::QueryType:
module Types
class QueryType < Types::BaseObject
field :variants, resolver: Resolvers::Variants
end
end
Resolvers::Variants:
module Resolvers
class Variants < Resolvers::Base
type Types::VariantType.page_type, null: true
def resolve
authorized_scope ::Variant.all
end
end
end
module Types
class VariantType < Types::BaseObject
field :id, ID, null: true
end
end
Types::BaseObject and Types::BasePage:
module Types
class BaseObject < GraphQL::Schema::Object
include ActionPolicy::GraphQL::Behaviour
field_class Types::BaseField
def self.page_type
@page_type ||= BasePage.create(self)
end
end
end
module Types
class BasePage < Types::BaseObject
def self.create(node_class)
Class.new(self) do
graphql_name("#{node_class.graphql_name}Page")
field :nodes, [node_class], null: false
end
end
end
end
module Types
class BaseField < GraphQL::Schema::Field
argument_class Types::BaseArgument
def initialize(**kwargs, &block)
super
puts kwargs
end
end
end
Actual Issue:
When checking kwargs[:type] in BaseField, it shows as nil:
# Output kwargs:
# {:owner=>Types::QueryType, :resolver_class=>Resolvers::Variants, :name=>:variants}
However, when I explicitly specify the type in the field definition, kwargs[:type] is correctly set:
module Types
class QueryType < Types::BaseObject
field :variants, Types::VariantType.page_type, null: true, resolver: Resolvers::Variants
end
end
# Output kwargs:
# {:owner=>Types::QueryType, :null=>true, :resolver_class=>Resolvers::Variants, :name=>:variants, :type=>#<Class:0x000000011d2ad9b0>}
Hey, sorry for the trouble! What version were you upgrading from? That info will help me narrow my search for the issue.
嘿,很抱歉打扰你了!你从哪个版本升级的?这些信息将帮助我缩小问题的搜索范围。
graphql (1.11.10)
You can use self.type to get it. However I’m not sure why kwargs cannot retrieve the type
I'm glad you found something that works for you!
If you want to debug further, please "bisect" the version change to see which version caused the problem. That is, try v2.0.0 and see if the bug is there. If it is, then try a smaller version; if it's not, then try a bigger version. Repeat until you identify which version introduces the bug. Then, we can investigate the specific changes more closely 👍
Initial Version: graphql-1.11.10
The output of kwards is:
{:type=>#<Class:0x000000012fadc840>,
:description=>nil,
:extras=>[],
:resolver_method=>:resolve_with_support,
:resolver_class=>Resolvers::Dealers,
:arguments=>{},
:null=>true,
:complexity=>1,
:extensions=>[],
:broadcastable=>nil,
:owner=>Types::QueryType,
:name=>:dealers}
In Version 2.0.0
The output of kwards is:
{:type=>#<Class:0x0000000112ac8e48>,
:description=>"Find all dealers with pagination",
:extras=>[],
:resolver_method=>:resolve_with_support, :
resolver_class=>Resolvers::Dealers,
:arguments=>{},
:null=>true,
:complexity=>1,
:broadcastable=>nil,
:owner=>Types::QueryType,
:name=>:dealers}
version > 2.0.0, the :type field is no longer present.
The output of kwards is:
{:owner=>Types::QueryType, :resolver_class=>Resolvers::Dealers, :name=>:dealers}