graphql - Apollo 服务器 - GraphQL - 语法错误:预期名称,找到)
问题描述
我看到有很多与我有关的问题,但不幸的是,它们都不适合我。这就是为什么我要问我的问题,因为我还在学习 GraphQL。
我正在使用 NestJS、GraphQL 和 Angular 开发一个项目。我正在使用 Apollo 客户端和 Apollo 服务器来实现 GraphQL。我的方法是先编写代码来创建模式。我在进行更新突变时遇到错误。我使用 PostgreSQL 作为数据库
这是生成的模式的突变部分
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) {
....
...
}
这是我在阿波罗服务器的操场上尝试的查询。
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
}
我把完整的错误信息供您参考。我可以知道这个错误的原因吗?我也尝试更改查询和突变。但它不起作用。感谢您的有益回应。
解决方案
这个问题中的代码是错误的,但错误清楚地表明了问题所在:
"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)"
]
}
}
特别有request
块显示抛出错误的查询是
{updateVehicleById(
input: {vehiclePatch: {id: 2}
) {
vehicle {
ageOfVehicle
carMake
carModel
email
firstName
id
lastName
manufacturedDate
vinNumber
}
}}
错误说第 3 行第 5 列我们可以看到错误在) {
并且仔细观察,我们可以看到真正的错误在前一行:
input: {vehiclePatch: {id: 2}
此输入对象不匹配}
,因此}
在行尾需要一个额外的。
推荐阅读
- php - 函数返回数组 PHP
- c# - 如何从模板中访问主 DataContext
- c++ - 在 Qt 中修改文件的 lastModified/lastAccessed/created 日期
- python - 如何在python函数中按DataFrame的日期过滤
- bash - 用于打开多个终端选项卡并运行一些脚本的 Bash 脚本
- c# - 在实现多个协变接口的类中选择协变接口的类型
- typo3 - 为什么 TYPO3 调用错误的文件引用表
- php - 使用通过 PHP 从 MySQL 数据库下载的 Adobe Reader 打开 PDF 文件时出现问题
- apache-spark - Spark Structured Streaming - 使用触摸文件优雅关闭
- c# - 拔下设备后,前台服务中的 Xamarin 循环(线程)不会继续