Testing Mavens

What you need to know when testing GraphQL APIs

Tue Aug 13 2024

NP
Nitin P
thumbnail

What is GQL?

As the name suggests in Graph Query language, the data you require is sent in the form of a query, and the server sends an adequate response for that specific query.

With GQL, what you ask is what you get!

Send a GraphQL query to your API and get what you need, nothing more and nothing less.
It is open source and developed by Facebook.

The reason for the development of GQL

Let’s take an example:
We have a user named “Alex” and we need to figure out the number of posts made by this user and the name of his last three followers.

How Typical REST API will be implemented:

Step 1: Get the user details

API: https://facebook.com/v6/member

HTTP GET

{
    "user": {
        "id": "sd2342345df",
        "name": "Alex",
        "address": "1 Park Ave, NYC",
        "DOB": "22-02-1991"
    }
}

Step 2: Get the Post from this user

API: https://facebook.com/v6/member/sd2342345df/post

HTTP GET

{
    "post": {
        "id": "vdf234234df",
        "post": "How to test GQL?",
        "description": "Easy simple ways to test GQL…",
        "comment": "this is awesome"
    }
}

Step 3: Get the followers for this user

API: https://facebook.com/v6/member/sd2342345df/followers

HTTP GET

{
    "followers": {
        "id": "kkak234234df",
        "name": "Jerry",
        "address": "25 Park Ave, NYC",
        "DOB": "01-01-1990"
    }
}

So, we are hitting 3 APIs consecutively to fetch the details. These APIs is used by the app/web application to provide the user with the requested data. The problem with this method is that the user fetches the details using three different service requests on the server and receives a massive chunk of data as a response. Further, this data needs to be filtered before being displayed on the client’s mobile application or web application.

How GraphQL solves this problem?

GraphQL will call single end point, considering the data is available on GraphQL server.

API: https://graph.facebook.com/facebook/me

Provide the Access Token which allows your app to access the Graph API

HTTP POST

query UserDataCatalog
    { userDataCatalog 
        { info{
             name 
             post { 
                 title 
                 follower 
            } 
        }
     } 
}

Response

{
    "data": {
        "userDataCatalog": {
            "info": [
                {
                    "name": "James",
                    "post": {
                        "title": "Bend the trend",
                        "follower": "jame254"
                    }
                }
            ]
        }
    }
}

The 3 Step Process

Explain the data you want > What exactly you want from client > Get the result.

First and foremost, the GraphQL schema is defined that explains the data we require, then Front-end and backend developers are authorized for development and can work independently. Once the front end and backend are ready, the GraphQL can be linked to fetch the data.

Advantages of GraphQL

  1. More efficient in terms of performance as it filters out and fetches simultaneously.

  2. Changes are easy to make on the UI side. For instance, If you need one more attribute and display in Frontend, add that in one GraphQL layer, and you are good to go.

  3. You don’t have to send multiple requests to fetch a little data.

  4. Over-fetching of data is avoided.

  5. Supports data analytics such as hits on a request and improves data visualization.

Features to be tested of GraphQL:

  1. Schema testing: A shorthand notation to understand the basic shape and structure of GraphQL schema and its type.

    Interface Entity { 
                 id: ID!
                 name: String 
    }
  2. Mutation testing: Mutations change the data in our database and provide us with a result. Mutations are comparable to CRUD operations in the REST API. Testing mutations is essential because it needs data access and database addition.

  3. Query Testing: Query testing refers to checking the queries based on the operation requirement and parameters passed. These parameters are used by the front end to call that operation and get a response.

Understanding before start testing GQL

GraphQL is an abstraction layer between the front-end systems and the backend APIs making it critical for the testing. GraphQL queries allow you to access multiple resources in the backend and cluster that data in a single meaningful response. The functions then assemble the necessary data into a single response with the same shape as the request. It makes it easy to understand which data is associated with which element in the request.

How GraphQL is integrated Logic?

The core is built on a microservice architecture of several services and database instances that handle different aspects of our business logic. While most of these services offer similar entry points to partitioned DB logic, our GraphQL service is unique.

The graph service acts as a middle layer between the core services to provide an appearance of an adaptive data structure for clients and provides these benefits over a traditional direct client to REST structure:

  1. Efficient data loading (great for complex CMS integrations)

  2. Automated validation and type checking

  3. Centralized entry point for our many microservices.

Points to remember while testing the GQL:

  1. In GraphQL, we use an endpoint with many different queries, and internally GQL can call the SB (Spring boot – the layer on which GQL is wrapped) APIs as per the operation name and parameter sent from the query. 

  2. GraphQL queries always return an HTTP status code of 200, so testing a defect based on the status code of the response is difficult.

  3. If ever there is an error, then there will always be an error code in the response body. Based on this error code, we can understand – what went wrong, which can be further verified on the confluence page.

Background

Your Quality Gatekeepers,

Partner with us today.