Graphql post body"必须提供查询字符串。"

时间:2016-04-18 10:36:17

标签: express graphql graphql-js

我使用Express-graphql中间件。 我在正文中发送以下请求:

POST /graphql HTTP/1.1
Host: local:8083
Content-Type: application/graphql
Cache-Control: no-cache
Postman-Token: d71a7ea9-5502-d5fe-2e36-0ae49c635a29

{
   testing {
      pass(id: 1) {
        idn
      }
    }
}

并有错误

{
  "errors": [
    {
      "message": "Must provide query string."
    }
  ]
}
在graphql中我可以在URL中发送更新。

网址字符串太短。我必须发送像

这样的更新模型
mutation {
  update(id: 2, x1: "zazaza", x2: "zazaza", x3: "zazaza" ...(more more fields)...) {
    idn
  }
}

我认为它必须在请求体内。我如何发送'更新'查询或我做错了吗?

7 个答案:

答案 0 :(得分:36)

发布请求需要管理标题信息。

  1. 使用Http客户端:Content-Type application / json

  2. 使用Postman客户端:Content-Type application / graphql

  3. 但请求正文看起来像字符串

    {"query":"mutation{update(id:1,x1:\"zazaz\",x2:\"zazaz\"......){id x1 x2}}"}
    

答案 1 :(得分:8)

如果您正在使用graphql并希望使用邮递员或任何其他Rest客户端进行测试,请执行此操作。

在邮递员中,选择POST方法并输入您的URL并将Content-Type设置为application/graphql,然后在正文中传递您的查询。

示例:

http://localhost:8080/graphql
Mehtod: POST
Content-Type: application/graphql
Body: 
  query{
    FindAllGames{
    _id
    title
    company
    price
    year
    url
   }
 }

多数民众赞成你会得到答复。

enter image description here

答案 2 :(得分:1)

对我来说工作如下:

体内

Body postman

在标题中

Header postman

不要忘记在 Body 设置上标记 GraphQl [x] enter image description here

以及如何在将动词更改为 POST 之前引用。

答案 3 :(得分:0)

将内容类型切换为JSON。  像这样see Postman image

答案 4 :(得分:0)

使用邮递员版本 7.2.2 我遇到了类似的问题。此版本的Postman开箱即用支持Graphql。将Content-type更改为ImportInvoice::withoutGlobalScope('approve')->get(); 对我来说固定。 Image showing the Content-type in Postman

答案 5 :(得分:0)

通常在您的'express-graphql'没有收到任何参数时发生。您需要在应用程序中添加json / applicaton解析器。

例如-app.use(bodyParser.json()); // application/json

答案 6 :(得分:0)

  1. 转到相关网页并打开“检查”(通过写入单击-> 检查 || Ctrl+Shift+I 铬)

  2. 转到网络选项卡并复制 cURL 命令 enter image description here

  3. 打开邮递员,然后导入 -> 原始文本

  4. 粘贴复制的命令 enter image description here

  5. 然后,继续 ->

enter image description here

相关问题