在 GraphQL 中,我可以使用 Content-Type: application/graphql 发送变量吗? [英] in GraphQL, Can I send variables with Content-Type: application/graphql?

查看:28
本文介绍了在 GraphQL 中,我可以使用 Content-Type: application/graphql 发送变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 graphql 中使用变量,但似乎不可能用application/graphql"发送变量.

我是否应该继续使用 Content-Type: 'application/json'?

解决方案

http://graphql.org/learn/serving-over-http/#post-request

<块引用>

标准的 GraphQL POST 请求应使用 application/json 内容类型,并包含以下形式的 JSON 编码主体:

<代码>{询问": "...","操作名称": "...","变量": { "myVariable": "someValue", ... }}

<块引用>

operationName 和 variables 是可选字段.仅当查询中存在多个操作时才需要 operationName.

除上述之外,我们建议支持另外两种情况:

...如果存在application/graphql"Content-Type 标头,则将 HTTP POST 正文内容视为 GraphQL 查询字符串.

在我的理解中,Content-Type: "application/graphql" 是无变量查询的简写.

所以我的回答是是的,如果我想使用变量字段,我必须使用 Content-Type: "application/json" 标头

I've tried to use variables with graphql, but it seems impossible to send variables with 'application/graphql'.

Should i have to move on to Content-Type: 'application/json'?

解决方案

http://graphql.org/learn/serving-over-http/#post-request

A standard GraphQL POST request should use the application/json content type, and include a JSON-encoded body of the following form:

{
  "query": "...",
  "operationName": "...",
  "variables": { "myVariable": "someValue", ... }
}

operationName and variables are optional fields. operationName is only required if multiple operations are present in the query.

In addition to the above, we recommend supporting two additional cases:

... If the "application/graphql" Content-Type header is present, treat the HTTP POST body contents as the GraphQL query string.

In my understanding, Content-Type: "application/graphql" is Shorthand for Querying without variables.

So my answer is "Yes, If I want to use variables field, I have to use Content-Type: "application/json" header

这篇关于在 GraphQL 中,我可以使用 Content-Type: application/graphql 发送变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆