Enter a GraphQL query to convert it to a JSON body for a POST request.
Want to connect to a GraphQL API in Airtable?If you're connecting to a GraphQL server using HTTPS requests, the server probably expects you to send a POST request with the query in the JSON body like this:
{ "query": "query { countries { code name capital continent { name } }}" }
So you need your GraphQL query to be in this format to send it to the server.
Unfortunately, most GraphQL API docs/ playgrounds just give you straight GraphQL queries like this:
query { countries { code name capital continent { name } } }
You cannot use this directly in the request body as it is not valid JSON.
Converting a GraphQL query to a JSON body is simple. First, we remove new lines/ extra spaces from the GraphQL query. Then we add it under a "query" key in a JSON object. We also add any variables under the "variables" key in the JSON. This is what the converter above does!
You might want to add the optional "operationName" field. You can add this field to the JSON yourself if you need it.
{ "query": "query { countries { code name capital { name } } }", "operationName": "MY OPERATION NAME", "variables": { "code": "Gb" } }
Having trouble? Try clicking the "Beautify" button to validate and format your query. You will be shown any errors.