GraphQL Testing

Applies to ReadyAPI 3.57, last modified on December 20, 2024

This topic provides basic information on GraphQL and how to work with it in ReadyAPI.

About GraphQL

Queries

GraphQL is a query language for getting data from APIs. This language provides a flexible way of getting all the needed data in one request.

How does it work? A request to a GraphQL service contains a query of the needed data. The service gathers all the requested data and forms it in a JSON object. The structure of the object is relevant to the request query, so the client application understands where to get the exact piece of data to work with.

For example, if you need to get customer names, your GraphQL request may look like this:

GraphQL Query

{
  customers {
    name
  }
}

In this case, the response may look like this:

JSON

{   "data": {
    "customers": [
      {
        "name": "Eddie Griffith"
      },
      {
        "name": "Mea Cousins"
      }
    ]
  }
}

Mutations

A client can also add or modify data on the server. Such a request is called mutation. In this case, the request calls the needed mutation on the server and provides the server with the required fields. The service responses with the modified data. As for a usual query, the mutation request specifies which fields should be returned in the response. For example, the following mutation adds a customer to the service and requests the id of the added customer:

GraphQL

mutation {
  addCustomer(name: "Beulah Glass", email: "[email protected]") {
    id
  }
}

The response may look like the following JSON object. Pay attention to the fact that the response does not contain the name and email of the added customer because is has not been requested.

JSON

{ "data": {
  addCustomer: {
    "id": 3
    }
  }
}

Schema

A GraphQL schema describes which data user can get from the service. Also, the schema defines object types, their types and fields, as well as possible queries and mutations.

Requirements

You need to have any ReadyAPI license to add a GraphQL API to the project. However, to add a GraphQL request to a functional test case, you will need a ReadyAPI Test Pro license. If you do not have it, try a ReadyAPI trial.

Working with GraphQL in ReadyAPI

In ReadyAPI, you can send queries and mutation requests to GraphQL services to simulate client interaction and check that your service works as expected.

If you have a GraphQL schema, you can add a GraphQL API to your project. ReadyAPI saves the schema to the project and generates queries and mutations described in the schema. Default requests include all the possible fields to be fetched from the service.

When you add the requests to a functional test, you use either GraphQL Query to send query requests or GraphQL Mutation to send mutation requests. These test steps are linked to the service, so you can validate the request and response against the schema and reuse environment and authorization settings.

In case you do not have an API schema, you use the GraphQL Request test step and manually configure the required query or mutation. This test step does not support environments, and you cannot inherit authentication settings from the service.

Next Steps

See Also

Creating Projects

Whatch the video
 
Highlight search results