我已经这样定义了一个UserType:
class Types::UserType < GraphQL::Schema::Object
field :id, ID, null: false
field :username, String, null: false
field :full_name, String, null: true
end
这些字段中的每一个都存在于Rails模型上,并且在GraphQL Gem 1.8之前的升级中,我能够在查询中使用full_name很好。
当我运行查询时:
query {
users {
username
id
full_name
}
}
我得到:"message": "Field 'full_name' doesn't exist on type 'User'",
如果删除full_name
,则会得到期望的数据。我以什么方式错误地解决了这个问题?作为参考,我的QueryType
定义为:
class Types::QueryType < GraphQL::Schema::Object
# Add root-level fields here.
# They will be entry points for queries on your schema.
field :users, [UserType, null: true], null: false do
argument :id, Integer, required: false
end
def users(**args)
args[:id]
if args[:id]
User.where(id: args[:id])
else
User.all
end
end
end
答案 0 :(得分:2)
我认为问题在于您的查询中full_name
应该是fullName
。使用1.8.x版本时,架构中的字段会自动显示。
字段和参数名称应作为惯例强调。它们将被转换为基础GraphQL类型的camelCase,而将成为架构本身中的camelCase。