return graphql 中的对象数组 rails

return array of objects in graphql rails

我需要编写代码来显示用户和组织,但是 return 下一个查询、搜索错误,但有错误

我的代码

module Types
  class UserType < Types::BaseObject
    field :id, ID, null: false
    field :email, String, null: false
    field :role, String, null: true
    field :organization_ids, [Types::OrganizationType], null: false
  end
end

module Types
  class OrganizationType < Types::BaseObject
    field :id, ID, null: false
    field :name, String, null: false
  end
end

但是return下一个错误

这是我的查询

您似乎在尝试 return 关联:一个用户有多个组织?

确保在 user.rb 中您有使用 has_many :organizations 定义的关联...并包括 Rails 理解关联所需的任何中间模型

将您的用户类型更新为:

module Types
  class UserType < Types::BaseObject
    ...
    field :organizations, [Types::OrganizationType], null: false
  end
end

那么你应该可以这样查询:

query getUsers{
  users {
    id
    email
    role
    organizations {
      id
    }
  }
}