How to Test GraphQL APIs Using Postman

In this guide, we will explore how to effectively use Postman to test GraphQL APIs. You'll learn about the basics of GraphQL, how to set up Postman for testing, and practical examples to get you started.
By Jamie

Understanding GraphQL

GraphQL is a query language for APIs that allows clients to request exactly the data they need. Unlike REST, which has multiple endpoints for different resources, GraphQL operates on a single endpoint. This flexibility makes testing GraphQL APIs essential for developers.

Setting Up Postman for GraphQL

  1. Download and Install Postman: If you haven’t already, download Postman from the official website.
  2. Create a New Request: Open Postman, and click on New > Request.
  3. Set Request Type: Change the request type from GET to POST, as GraphQL typically uses POST requests.
  4. Enter GraphQL Endpoint: In the URL bar, enter the GraphQL endpoint you want to test (e.g., https://api.example.com/graphql).

Example 1: Basic Query

To fetch data from a GraphQL API, you need to structure your query as follows:

Request Body

{
  "query": "{ users { id name email } }"
}

Steps

  • In the Body tab, select raw and choose JSON from the dropdown menu.
  • Paste the above JSON structure into the body.
  • Click Send to execute the request.

Expected Response

You should receive a response similar to this:

{
  "data": {
    "users": [
      { "id": "1", "name": "Alice", "email": "alice@example.com" },
      { "id": "2", "name": "Bob", "email": "bob@example.com" }
    ]
  }
}

Example 2: Query with Variables

GraphQL allows the use of variables to make your queries more dynamic. Here’s how to do that in Postman:

Request Body

{
  "query": "query GetUser(\(id: ID!) { user(id: \)id) { name email } }",
  "variables": { "id": "1" }
}

Steps

  • Similar to the previous example, set the Body type to raw and JSON.
  • Paste the above JSON structure into the body.
  • Click Send to execute the request.

Expected Response

This will return data for the user with the specified ID:

{
  "data": {
    "user": { "name": "Alice", "email": "alice@example.com" }
  }
}

Example 3: Mutation Request

GraphQL also supports mutations for creating or updating data. Here’s how to test a mutation:

Request Body

{
  "query": "mutation CreateUser(\(input: UserInput!) { createUser(input: \)input) { id name } }",
  "variables": { "input": { "name": "Charlie", "email": "charlie@example.com" } }
}

Steps

  • In the Body tab, select raw and choose JSON.
  • Paste the above JSON structure into the body.
  • Click Send to execute the request.

Expected Response

You should receive a confirmation response:

{
  "data": {
    "createUser": { "id": "3", "name": "Charlie" }
  }
}

Conclusion

Testing GraphQL APIs with Postman is straightforward and efficient. By following the examples provided, you can easily create queries and mutations, allowing you to interact with your GraphQL endpoints effectively. As you become more familiar with GraphQL, you’ll find that Postman is a powerful tool for API testing.