gpt4 book ai didi

带有args的graphql查询不适用于用户ID

转载 作者:行者123 更新时间:2023-12-05 01:08:59 24 4
gpt4 key购买 nike

我被骗了。我最初创建了用户查询,它给了我我认为是语法错误的错误。但后来我为完美运行的车辆创建了一个相同的查询。我怀疑它与ID有关!类型,但我已经用完了线索。任何帮助将不胜感激!

这是我的 typedef 和解析器。

//TYPEDEFS//

   type User {
id: ID!
fname: String
lname: String
email: String
password: String
vehicles: [Vehicle]
}

type Vehicle {
id: ID!
vin: String
model: String
make: String
drivers: [User]
}

type Query {
users: [User]
user(id: ID!): User
vehicles: [Vehicle]
vehicle(vin: String): Vehicle
}

//解决者//

   user: async (parent, args, context) => {
const { id } = args
return context.prisma.user.findUnique({
where: {
id,
},
})
},
vehicle: async (parent, args, context) => {
const { vin } = args
return context.prisma.vehicle.findUnique({
where: {
vin,
}
})
}

//查询//

**这个是坏的,有错误:在 prisma.findOneUser 上得到无效值 '1'。提供的字符串,预期的 Int**我试过做 id: "1"user(where: {id: 1})

query {
user(id:1){
id
fname
}
}

**这个按预期工作

query {
vehicle(vin:"123123123"){
vin
make
}
}

//完全错误*//

{
"errors": [
{
"message": "\nInvalid `prisma.user.findUnique()` invocation:\n\n{\n where: {\n id: '1'\n ~~~\n }\n}\n\nArgument id: Got invalid value '1' on prisma.findOneUser. Provided String, expected Int.\n\n",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"user"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"clientVersion": "2.13.1",
"stacktrace": [
"Error: ",
"Invalid `prisma.user.findUnique()` invocation:",
"",
"{",
" where: {",
" id: '1'",
" ~~~",
" }",
"}",
"",
"Argument id: Got invalid value '1' on prisma.findOneUser. Provided String, expected Int.",
"",
"",
" at Document.validate (/home/atran/workspace/m4m/m4m_server/node_modules/@prisma/client/runtime/index.js:76090:19)",
" at NewPrismaClient._executeRequest (/home/atran/workspace/m4m/m4m_server/node_modules/@prisma/client/runtime/index.js:77796:17)",
" at resource.runInAsyncScope (/home/atran/workspace/m4m/m4m_server/node_modules/@prisma/client/runtime/index.js:77733:52)",
" at AsyncResource.runInAsyncScope (async_hooks.js:188:21)",
" at NewPrismaClient._request (/home/atran/workspace/m4m/m4m_server/node_modules/@prisma/client/runtime/index.js:77733:25)",
" at Object.then (/home/atran/workspace/m4m/m4m_server/node_modules/@prisma/client/runtime/index.js:77850:39)",
" at process._tickCallback (internal/process/next_tick.js:68:7)"
]
}
}
}
],
"data": {
"user": null
}
}

最佳答案

Prisma 需要一个 Int:

"Argument id: Got invalid value '1' on prisma.findOneUser. ProvidedString, expected Int.",

因此,您需要将 id 转换为数字。也许是这样的:

user: async (parent, args, context) => {
const id = +args.id;
return context.prisma.user.findUnique({
where: { id }
});
}

关于带有args的graphql查询不适用于用户ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65534216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com