【问题标题】:Apollo client and graphQl query error : Variable '$where' expected value of type 'OrganizationWhereUniqueInputApollo 客户端和 graphQl 查询错误:“OrganizationWhereUniqueInput”类型的变量“$where”预期值
【发布时间】:2020-11-27 12:17:19
【问题描述】:

您好,我正在使用 GraphQL、Apollo 客户端和 Prisma 制作后端服务器。我正在尝试编写一个查询来获取组织数据。发送查询的用户应该根据他们的 id 取回其组织数据。在操场上运行查询时出现此错误。

错误:

"message": "Variable '$where' expected value of type 'OrganizationWhereUniqueInput!' but got: {\"employees\":{\"id\":\"ckas83z13t9qk0992pucglc4k\"}}. Reason: 'employees' Field 'employees' is not defined in the input type 'OrganizationWhereUniqueInput'. (line 1, column 8):\nquery ($where: OrganizationWhereUniqueInput!) {\n       ^",

我看不出我做错了什么。我对这一切还是很陌生。我尝试以不同的方式在Query.js 中编写函数,但没有运气。另外,我仍然觉得您在操场上收到的错误消息非常令人困惑

架构:

type Query {
    getOrganization: Organization!
}

type Organization {
    id: ID!
    name: String!
    country: String!
    street: String!
    zipCode: Int!
    houseNumber: Int!
    addings: String
    employees: [User!]

}

type User {
    id: ID!
    firstname:String!
    lastname:String!
    email: String!
    services: [Service!]
    organization: Organization!
}

query.js

function getOrganization(parent, args, context, info){
    const userId = getUserId(context)
        return context.prisma.organization({employees:{id:userId}})   
}

// also tried this 
/*
function getOrganization(parent, args, context, info){
    const userId = getUserId(context)
        return context.prisma.organization({where: {employees:{id:userId}}})   
}*/

用户.js

function services (parent, args, context){
    return context.prisma.user({id: parent.id}).services()
}

function organization (parent, args, context){
    return context.prisma.user({id: parent.id}).organization()
}


module.exports={
    services,
    organization
}

Organization.js

function employees(parent, args, context){
    return context.prisma.organization({id: parent.id}).employees()
}

module.exports={
    employees
}

谁能帮我看看出了什么问题?

在操场上查询:

query{
  getOrganization{
    name 
    id 
  }}

HTTP HEADER:

{
  "Authorization": "Bearer {contains user token }"
}

【问题讨论】:

  • 你能分享一下你的查询吗?
  • 好的,我已经编辑了我的问题,所以它包含了游乐场查询
  • 我认为您不能使用变量employees 来查询特定组织。错误状态相同:OrganizationWhereUniqueInput 此输入可能只接受组织 ID 作为输入,因此此查询 return context.prisma.organization({employees:{id:userId}}) 必须更改为 return context.prisma.organization({ {id: 'some-id' })
  • 好的,但必须有办法找回用户所属的组织?
  • 您可以尝试通过这样的员工获取组织吗:function getOrganization(parent, args, context, info){ const userId = getUserId(context) return context.prisma.user( {id: userId }).organization() }

标签: javascript graphql apollo-client prisma


【解决方案1】:

只需使用 OrganizationWhereInput 而不是 OrganizationWhereUniqueInput。它将返回一个组织列表而不是单个结果(可能返回一个空数组),但它应该允许您使用员工 ID 搜索组织。

【讨论】:

    猜你喜欢
    • 2019-11-02
    • 2018-10-16
    • 2019-06-10
    • 2022-01-22
    • 2018-03-02
    • 2021-10-12
    • 2022-11-04
    • 2016-08-31
    • 2020-10-18
    相关资源
    最近更新 更多