How To Test GraphQL Using Postman

In this article, we will learn What is GraphQL API, How it is different from Rest services and how we can test GraphQL Using Postman.

What is GraphQL?

GraphQL is a data query and manipulation language for API. It was developed by Facebook in 2012. It is becoming a preferred alternative to Rest Services as it simplifies data fetching for applications and gives the ability to query only the required information through a single end-point.

Advantages Of GraphQL

Here are a few advantages of GraphQL:

  • Apps using graphQL are fast and stable because GraphQL queries return exactly what we need, Not More Not Less. GraphQL prevents over-fetching and under-fetching of data.
  • GraphQL uses a single endpoint to fetch all the required data in a single query (/graphql), while REST APIs require loading from multiple endpoints for different resources.
  • It allows developers to add new fields and types without impacting existing queries.

It is important to remember that GraphQL is not the replacement for the Rest API. GraphQL and REST services can co-exist in the same stack. This can be done by masking your REST endpoint into a GraphQL endpoint using Root resolvers as per the GraphQL documentation.

GraphQL vs Rest API

In this section, I will explain How GraphQL API different then traditional Rest API.

How GraphQL API is different from Rest API?

1) Rest services use common HTTP methods like GET, PUT, POST etc and JSON to exchange payload data while GraphQL uses the HTTP Post method the most.

2) GraphQL follows client-driven architecture while REST services follow server-driven architecture.

3) GraphQl does not have any caching mechanism while Rest services use caching automatically.

4) GraphQl provides consistency across platforms which is somewhat difficult in Restful API.

5) Rest services use fixed endpoints for different resources (for example: /users, /posts, /id) while GraphQL API uses a single endpoint for all queries (for example: /graphql).

6) Rest services require versioning to support backward compatibility while GraphQl strongly avoids the versioning concept due to the frequent schema changes.

7) Real-time updates are achieved in REST through web socket or Polling while GraphQL supports real-time updates through subscriptions.

Here is the Sample GraphQL Syntax

query {
  user(id: 1) {
    id
    name
    email
    posts {
      title
      content
    }
  }
}

Why does a QA need to know about GraphQL?

As QA we should be aware of new tools and technologies which are in high demand. GraphQL is quickly making its place in the industry. Big companies like Facebook, GitHub, Airbnb, Atlassian, Paypal, and Plural Sight are listed as GraphQL customers on their Sites.

Streaming platforms like Netflix or Spotify leverage GraphQL to provide personalized content recommendations. More companies are adapting to GraphQl because of the efficiency and flexibility of GraphQl API.

GraphQL Testing Tools

The majority of functional GraphQL testing is done to ensure that the front-end schema, queries, and mutations work as intended. There are multiple tools are available to test GraphQL. Some of them are Apollo’s GraphQL Platform and GraphiQL. Postman and ReadyAPI also provide support for GraphQL testing. Recently Katalon Studio also started supporting GraphQL testing.

How to Test GraphQL API Using Postman

Postman is a very popular tool for API testing. Postman also supports testing of GraphQL API in the same way as we test Rest Services. Postman’s user-friendly interface and robust features make it an ideal choice for developers and testers looking to validate GraphQL APIs.

GraphQL Query & GraphQL Mutation

Two important terms used for GraphQL are Query and Mutation. Simply put, Query in GraphQL is a read-only operation used to fetch data while Mutation is like a write operation used to perform insert/Update/Delete Operations, however, both are sent to the same endpoint.

Sample GraphQL Mutation Syntax

mutation {
  createUser(input: { name: "John Kelly", email: "[email protected]" }) {
    id
    firstname
    lastname
    email
    phonenumber
  }
}

Let’s understand the above code. createUser is the name of the mutation which takes name and email as input to create a new user and returns { id, first name, last name, email, phone number} as response payload.

GraphQL Query

GraphQL Query is a read-only operation. We are requesting a resource with book ID ‘123’. This is similar to the GET API of Rest Services.

query {
  book(id: "123") {
    title
    author {
      name
      books {
        title
      }
    }
  }
}

I am using the below GraphQL API for demo purposes:

query {
  movies{
    name
    genre
    actor{
      name
    }
  }
}

This query is designed to fetch the movie details like movie name, genre, and actor of the movie. To test this in Postman Create a new request and add the endpoint as “https://n7b67.sse.codesandbox.io/graphql”.

Test GraphQL in Postman

  1. Open the Postman tool.
  2. Create a new request, name it ‘GraphQL query’ and save it.
  3. Add the Request URL and select the HTTP method ‘POST’.
  4. Go to the body tab and select the GraphQL radio button. You will see two sections ‘Query’ and ‘Variable’.
  5. Paste the movie query in the query section
  6. Hit the send button.
  7. See the response in default JSON format along with the Status code and execution time.

Suppose you want to fetch only actors’ names and all other fields are irrelevant to you then you will use only the actor{ name } field in your query and you will get your required information, so you observe that no over-fetching of data, no multiple end-points, Hence execution time is less.

How To Test GraphQL
Test GraphQL

Data-driven GraphQL API testing

Suppose you want to test a query with different variable values like data-driven API testing, How would you do that? Postman provides a GraphQL variable section where you can either directly put the values or drive data from global variables. Let’s see this in action. For demo purposes, I am using open-source Pokemon API.

Consider the case where we want to fetch complete information about a Pokemon based on the name provided. I have created a variable as “$Name” which is of string type as per the internal implementation of API and in the variable section provided its value as “Pikachu”. It will fetch all the details related to this Pokemon. One important point to note here is that to test a GraphQL API you must have some basic understanding of its schema like what data type is used for which variable field.

How To Test GraphQL
GraphQL with Variable

Here we can give any valid Pokemon name and we will get our desired result.

An important point to remember in GraphQL API testing is that whether the execution is successful or not GraphQL API always returns a Status code of “200”. If there is an error while processing GraphQL queries, a detailed error message is sent to the customer with the status code 200. 

Another thing is that GraphQL queries use the ‘Post’ method in place of ‘Get’ unlike the rest services. This is a common practice however it is not mandatory.

Testing Tips for GraphQL Testing

  1. Make sure that the GraphQL schema is updated before executing the test cases.
  2. Validate the schema and structure of GraphQL API.
  3. Don’t rely on the status code as it always returns ‘200’.
  4. Test queries and mutations with different sets of data.
  5. Check edge cases like null values, and invalid values.
  6. Verify authorization and authentication for queries and mutations.
  7. Perform load testing.
  8. Check for SQL injections and security vulnerabilities.
  9. Check for race conditions and data integrity.

Conclusion

Learning GraphQL API testing using the Postman tool opens up a world of possibilities for developers and testers. By following the steps outlined in this guide, now we are well versed in how to test GraphQL Using Postman.

Visit Here if you want to learn how to automate GraphQL APIs using rest assured.

1 thought on “How To Test GraphQL Using Postman”

Leave a Reply

Discover more from AutomationQaHub

Subscribe now to keep reading and get access to the full archive.

Continue reading