【发布时间】:2021-07-28 04:15:03
【问题描述】:
我看到有很多与我有关的问题,但不幸的是,它们都不适合我。这就是我问这个问题的原因,因为我还在学习 GraphQL。
我正在使用 NestJS、GraphQL 和 Angular 开发一个项目。我正在使用 Apollo 客户端和 Apollo 服务器来实现 GraphQL。我的方法是 code first 来创建模式。我在执行 更新突变 时遇到错误。我使用 PostgreSQL 作为数据库
这里是生成的schema
的变异部分type Mutation {
updateVehicle(email: String!, vinNumber: String!, lastName: String!, firstName: String!, id: Int!): Vehicle!
deleteVehicle(id: Int!): Vehicle!
}
这是更新突变的解析器
@Mutation(returns => Vehicle)
async updateVehicle(
@Args({name: 'id', type: () => Int }) id: number,
@Args({name: 'firstName', type: () => String }) firstName: string,
@Args({name: 'lastName', type: () => String }) lastName: string,
@Args({name: 'vinNumber', type: () => String }) vinNumber: string,
@Args({name: 'email', type: () => String }) email: string) {
....
...
}
这是我在 apollo 服务器的 playground 上尝试的查询。
mutation MyMutation(
$id: Int!,
$email:String!,
$firstName:String!,
$lastName:String!,
$vinNumber:String!){
updateVehicle(
id: $id,
email: $email,
firstName: $firstName,
lastName: $lastName,
vinNumber: $vinNumber){
id
email
firstName
lastName
vinNumber
}
}
在查询变量部分,我将我的值如下所示
{
"id": 2,
"firstName":"test",
"lastName": "test2",
"vinNumber": "1234",
"email": "test@live.com"
}
当我执行查询时,我收到以下错误消息。其实我也搞不懂是什么意思。
{
"errors": [
{
"message": "Syntax Error: Expected Name, found \")\".: {\"response\":{\"errors\":[{\"message\":\"Syntax Error: Expected Name, found \\\")\\\".\",\"locations\":[{\"line\":3,\"column\":5}]}],\"status\":400},\"request\":{\"query\":\"{updateVehicleById(\\n input: {vehiclePatch: {id: 2}\\n ) {\\n vehicle {\\n ageOfVehicle \\n carMake \\n carModel \\n email\\n firstName \\n id\\n lastName\\n manufacturedDate\\n vinNumber\\n }\\n }}\"}}",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"updateVehicle"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"response": {
"errors": [
{
"message": "Syntax Error: Expected Name, found \")\".",
"locations": [
{
"line": 3,
"column": 5
}
]
}
],
"status": 400
},
"request": {
"query": "{updateVehicleById(\n input: {vehiclePatch: {id: 2}\n ) {\n vehicle {\n ageOfVehicle \n carMake \n carModel \n email\n firstName \n id\n lastName\n manufacturedDate\n vinNumber\n }\n }}"
},
"stacktrace": [
"Error: Syntax Error: Expected Name, found \")\".: {\"response\":{\"errors\":[{\"message\":\"Syntax Error: Expected Name, found \\\")\\\".\",\"locations\":[{\"line\":3,\"column\":5}]}],\"status\":400},\"request\":{\"query\":\"{updateVehicleById(\\n input: {vehiclePatch: {id: 2}\\n ) {\\n vehicle {\\n ageOfVehicle \\n carMake \\n carModel \\n email\\n firstName \\n id\\n lastName\\n manufacturedDate\\n vinNumber\\n }\\n }}\"}}",
" at GraphQLClient.<anonymous> (E:\\Virtusa\\vehicle-managment-app\\vehicle-mgt-server3\\node_modules\\graphql-request\\dist\\index.js:170:35)",
" at step (E:\\Virtusa\\vehicle-managment-app\\vehicle-mgt-server3\\node_modules\\graphql-request\\dist\\index.js:63:23)",
" at Object.next (E:\\Virtusa\\vehicle-managment-app\\vehicle-mgt-server3\\node_modules\\graphql-request\\dist\\index.js:44:53)",
" at fulfilled (E:\\Virtusa\\vehicle-managment-app\\vehicle-mgt-server3\\node_modules\\graphql-request\\dist\\index.js:35:58)",
" at processTicksAndRejections (internal/process/task_queues.js:97:5)"
]
}
}
}
],
"data": null
}
我把完整的错误信息供你参考。我可以知道这个错误的原因吗?我也尝试更改查询和突变。但它不起作用。感谢您的有益回复。
【问题讨论】:
-
某事一团糟...您的查询/突变和错误来自 2 个不同的世界/部分 - 看起来像
updateVehicle解析器在内部发出请求updateVehicleById- 查询中断、缺少参数等跨度> -
在我的突变中,我调用了一个 Postgraphile 端点。它是
updateVehicleById。问题就在那里。我现在解决了。感谢您的回复。
标签: graphql nestjs apollo-server