How to Test GraphQL in Postman: Step-by-Step Guide
To test
GraphQL in Postman, create a new POST request to your GraphQL endpoint, set the Content-Type header to application/json, and send your query or mutation in the request body as a JSON object with a query field. You can also include variables in a variables field for dynamic testing.Syntax
In Postman, a GraphQL request uses a POST method with a JSON body containing the query string and optionally variables. The Content-Type header must be application/json. The basic structure is:
- POST URL: Your GraphQL endpoint URL
- Headers: Set
Content-Type: application/json - Body: Raw JSON with
queryand optionalvariables
json
{
"query": "query { fieldName }",
"variables": { "var1": "value1" }
}Example
This example shows how to test a simple GraphQL query in Postman that fetches a user's name and email by ID.
json
{
"query": "query GetUser($id: ID!) { user(id: $id) { name email } }",
"variables": { "id": "123" }
}Output
{
"data": {
"user": {
"name": "Alice",
"email": "alice@example.com"
}
}
}
Common Pitfalls
Common mistakes when testing GraphQL in Postman include:
- Not setting the
Content-Typeheader toapplication/json, causing the server to reject the request. - Sending the GraphQL query as plain text instead of JSON in the body.
- Incorrectly formatting variables or omitting them when required.
- Using GET instead of POST for queries that require variables or mutations.
Always verify your JSON syntax and ensure variables match the query parameters.
json
Wrong way:
{
"query": "{ user(id: \"123\") { name } }"
}
Right way:
{
"query": "query GetUser($id: ID!) { user(id: $id) { name } }",
"variables": { "id": "123" }
}Quick Reference
| Step | Description |
|---|---|
| Set POST URL | Enter your GraphQL endpoint URL in Postman |
| Set Headers | Add header Content-Type: application/json |
| Write Body | Use raw JSON with 'query' and optional 'variables' fields |
| Send Request | Click Send to execute the GraphQL query or mutation |
| Check Response | Verify the JSON response data and errors |
Key Takeaways
Always use POST method with Content-Type set to application/json for GraphQL requests in Postman.
Send your GraphQL query inside a JSON object with a 'query' field and optional 'variables' for dynamic data.
Check your JSON syntax carefully to avoid request errors.
Use variables to make your queries reusable and easier to test.
Review the response JSON for data and error fields to validate your API behavior.