【问题标题】:Apollo server - GraphQL - Syntax Error: Expected Name, found )Apollo 服务器 - GraphQL - 语法错误:预期名称,找到)
【发布时间】: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


【解决方案1】:

这个问题的代码是错误的,但是错误说明了问题所在:

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

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

【讨论】:

  • 感谢您,问题已解决。实际上,我忘了检查 Postgraphile 查询。非常感谢您的快速回复。
猜你喜欢
  • 2020-04-18
  • 2018-06-28
  • 2020-10-03
  • 2023-03-31
  • 2021-02-18
  • 1970-01-01
  • 2021-12-09
  • 2019-04-05
  • 1970-01-01
相关资源
最近更新 更多