【问题标题】:Multiple definitions for User type graphql用户类型 graphql 的多个定义
【发布时间】: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 以外的其他名称,现在可以使用了。
  • 我很高兴听到,我已经回复了我的评论,你能接受吗?

标签: ruby graphql


【解决方案1】:

我没有使用 graphql 的经验,但是在 QueryType 类中,你定义了两次用户,一次作为字段字段 :user,一次作为方法 def user(id:) 对我来说似乎是错误的。

尝试将您的字段 :user 重命名为 :permitted_users 或类似的名称。

【讨论】:

    猜你喜欢
    • 2021-11-20
    • 2018-09-07
    • 2021-03-04
    • 2017-12-11
    • 2018-11-21
    • 2016-10-21
    • 2020-03-01
    • 2020-12-29
    • 2020-06-01
    相关资源
    最近更新 更多