Apollo server - GraphQL - Syntax Error: Expected Name, found )

Apollo server - GraphQL - Syntax Error: Expected Name, found )

我看到有很多问题与我的问题相关,但不幸的是,none 其中对我有用。这就是我问问题的原因,因为我还在学习 GraphQL。

我正在使用 NestJS、GraphQL 和 Angular 开发一个项目。我正在使用 Apollo 客户端和 Apollo 服务器来实现 GraphQL.My 方法是 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
}

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

这个问题的代码是错误的,但是错误清楚地说明了问题是什么:

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

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