Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in GraphQL, Can I send variables with Content-Type: application/graphql?

Tags:

graphql

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'?

like image 293
FourwingsY Avatar asked Apr 28 '17 08:04

FourwingsY


People also ask

What is the content type for GraphQL?

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.

What is the content type of the responses of a GraphQL API?

GraphQL responses are in JSON.


2 Answers

What prevents you from passing the variables in the query string and the query in the body?

POST /graphql?variables={"id":1234}
Content-Type: application/graphql
query ($id: ID!) {
    Post(id: $id) {
        id
        title
        body
    }
}
like image 172
François Zaninotto Avatar answered Oct 11 '22 06:10

François Zaninotto


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

like image 41
FourwingsY Avatar answered Oct 11 '22 07:10

FourwingsY