graphql - 如何修复 GraphQL 中的 "Syntax Error: Expected Name, found String\"查询\""

标签 graphql postman

我正在尝试通过使用 Postman 将 GraphQL 查询发送到服务器来测试我构建的 GraphQL 服务器。

当我使用原始单选按钮时它可以工作,但是当我尝试使用 GraphQL 单选按钮时,它会返回 "message": "语法错误:预期名称,找到字符串\"query\""

我尝试过更改语法:主要是添加或删除大括号,但没有任何反应。

我在原始模式下发送的查询(工作):

{
    person(id:"123456789") {
        personal_info {
            address
        }
    }
} 

我在 GraphQL 模式下发送的查询:

查询:

query getPerson ($id: String){
    person(id: $id){
        personal_info {
            address
        }
    }
}

GRAPHQL 变量:

{
    "id": "123456789"
}

我希望获得我要求的数据,但收到错误消息:

{
    "errors": [
        {
            "message": "Syntax Error: Expected Name, found String \"query\"",
            "locations": [
                {
                    "line": 1,
                    "column": 2
                }
            ]
        }
    ]
}

最佳答案

我也遇到了同样的问题。在研究过程中,我在stackoverflow上找到了下一个答案。 ,谢谢@gbenga_ps。

通过向 Postman 请求添加正确的 header 来解决: Adding header Content-Type: application/json

请求正文应如下所示:

{
    courses {
        title
    }
}

Request example

如果内容类型设置不正确,则会发生如下错误:

{
    "errors": [
        {
            "message": "Syntax Error: Expected Name, found String \"query\"",
            "locations": [
                {
                    "line": 1,
                    "column": 2
                }
            ]
        }
    ]
}

Error response example

关于graphql - 如何修复 GraphQL 中的 "Syntax Error: Expected Name, found String\"查询\"",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57680975/

相关文章:

node.js - Edge.node 字段类型必须是 Output Type 但得到 : undefined

node.js - 标识符...已经声明

node.js - Postman 将 Mongo 数组转换为对象

javascript - Graphql参数传递

ios - 安装 GraphQL iOS 框架

javascript - graphQL 查询中的变量

Postman - 仅在多个请求*多次迭代的情况下执行第一次迭代的第一个请求

javascript - API 请求适用于 postman,但不适用于 Node.js(请求)

android - Webservice 在 Android Retrofit 中不工作,但在 Postman 和 Swift/iOS 中工作,获得 401 Unauthorized

node.js - Sequelize 不会将值插入到新添加的列中