首页 > 解决方案 > 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
}

我把完整的错误信息供您参考。我可以知道这个错误的原因吗?我也尝试更改查询和突变。但它不起作用。感谢您的有益回应。

标签: graphqlnestjsapollo-server

解决方案


这个问题中的代码是错误的,但错误清楚地表明了问题所在:

  "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}

此输入对象不匹配},因此}在行尾需要一个额外的。


推荐阅读