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'?
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.
GraphQL responses are in JSON.
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
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With