【发布时间】:2021-01-05 22:20:43
【问题描述】:
我定义了以下 GQL 类型:
class Types::User < GraphQL::Schema::Object
field :firstName, String, null: true
field :lastName, String, null: false
field :email, String, null: false
end
我现在正尝试将它包含在我的根级 QueryType 中,如下所示:
module Types
class QueryType < Types::BaseObject
# Add root-level fields here.
# They will be entry points for queries on your schema.
field :user, [Types::User], "Users who can see this list", null: true do
argument :id, ID, required: true
end
def user(id:)
::User.find_by(id: id)
end
# TODO: remove me
field :test_field, String, null: false, description: 'An example field added by the generator'
def test_field
'Hello World!'
end
end
end
但是我得到的错误是:
User的多个定义。之前找到InputTypes::UserType(Class),然后在Query.user处找到Types::User(Class)
想到我在这里做错了什么?
【问题讨论】:
-
没有使用 graphql 的经验,但是在 QueryType 类中,您定义了两次用户,一次作为字段字段 :user,一次作为方法 def user(id:) 对我来说似乎是错误的
-
尝试将您的字段 :user 重命名为 :permitted_users 或类似的名称
-
@peter 这样就可以了,但是我发现问题似乎与第二个参数(返回类型)有关。当我将其从
[Types::User]→String更改时,我没有收到错误消息。但是这个返回类型显然是错误的 -
我将类型文件的名称更改为
User以外的其他名称,现在可以使用了。 -
我很高兴听到,我已经回复了我的评论,你能接受吗?