【发布时间】: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