Skip to content

How To Test Graphql Api Using Postman

API Testing Blog

Testing GraphQL APIs Using Postman

Postman is a popular API testing tool that can be used to test GraphQL APIs. It provides a user-friendly interface for sending requests, viewing responses, and managing your tests.

Setting Up a GraphQL Request

  1. Create a new request: In Postman, click the “New” button, and select “Request”.
  2. Set the request method: Select “POST” as the request method.
  3. Enter the GraphQL endpoint: Replace https://api.example.com/graphql with your actual endpoint in the address bar.
  4. Set the request body: In the “Body” tab, select “raw” and choose “GraphQL” as the format.
  5. Write your GraphQL query: Enter your GraphQL query in the editor. For example:
query {
users {
id
name
email
}
}

Sending GraphQL Mutations

  1. Define a mutation: Write your GraphQL mutation within the request body. For example:
mutation {
createUser(name: "John Doe", email: "john.doe@example.com") {
id
name
email
}
}
  1. Send the request: Click the “Send” button to execute the request.
  2. View the response: The response will be displayed in the “Body” tab. It will contain the data returned by the GraphQL mutation, including the newly created user’s information.

Organizing Your Tests with Collections

  1. Create a collection: Collections allow you to group related requests together for easier management. Click the “Collections” tab and create a new collection named “GraphQL API Tests”.
  2. Add your requests to the collection: Drag and drop your existing requests into the “GraphQL API Tests” collection.
  3. Organize requests by type: You can create folders within the collection to group related requests. For example, you can create folders for “Queries”, “Mutations”, and “Subscriptions”.

Validating Responses with Assertions

  1. Add a test: In the “Tests” tab, you can add assertions to validate the response data.
  2. Write your assertions: Use Javascript code to assert properties of the response. For example:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("User's name is correct", function () {
pm.expect(pm.response.json().data.createUser.name).to.be.equal("John Doe");
});

Testing with Variables

  1. Define variables: In the “Variables” tab, you can define variables for your GraphQL queries and mutations.
  2. Use variables in your query: Reference the defined variables within your GraphQL query using the $ prefix. For example:
query ($name: String!) {
user(name: $name) {
id
email
}
}
  1. Set variables values: In the “Variables” tab, set the values for each variable. For instance, for the $name variable, you could set the value to “Jane Doe”.

Handling Errors

  1. Trigger errors: You can intentionally trigger errors in your GraphQL API to test error handling.
  2. Check error responses: Validate the error responses in the “Tests” tab. For example:
pm.test("Error code is 400", function () {
pm.response.to.have.status(400);
});
pm.test("Error message is correct", function () {
pm.expect(pm.response.json().errors[0].message).to.be.equal("Invalid input");
});

Running Your Tests

  1. Run individual requests: You can run individual requests by clicking the “Send” button.
  2. Run collections: You can run entire collections of requests. This can be useful for running a suite of tests against your GraphQL API.
  3. Automate your tests: Postman allows you to automate your tests using scripts and integrations with CI/CD tools.

Example: Testing a User Query

Request:

query {
users {
id
name
email
}
}

Test Script:

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response data is an array", function () {
pm.expect(pm.response.json().data.users).to.be.an('array');
});
pm.test("First user's name is correct", function () {
pm.expect(pm.response.json().data.users[0].name).to.be.equal("John Doe");
});

Expected Response:

{
"data": {
"users": [
{
"id": "1",
"name": "John Doe",
"email": "john.doe@example.com"
},
{
"id": "2",
"name": "Jane Doe",
"email": "jane.doe@example.com"
}
]
}
}

By following these steps and incorporating the sample codes, you can effectively test GraphQL APIs using Postman. This will help you to identify and resolve issues early in the development lifecycle, ensuring the reliability and stability of your GraphQL API.

API Testing Blog