0
0
PostmanHow-ToBeginner ยท 4 min read

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 query and optional variables
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-Type header to application/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

StepDescription
Set POST URLEnter your GraphQL endpoint URL in Postman
Set HeadersAdd header Content-Type: application/json
Write BodyUse raw JSON with 'query' and optional 'variables' fields
Send RequestClick Send to execute the GraphQL query or mutation
Check ResponseVerify 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.