GraphQL Testing Guide

Ashish Padiyar
Kingfisher-Technology
4 min readAug 1, 2024

Before diving into GraphQL concepts required for testing, lets first have a clear understanding of “What GraphQL is ?”

GraphQL is a query language for APIs and a runtime for querying and manipulating data. We use GraphQL to query data from various sources, such as databases, third-party APIs, or services by defining the specific data structure and this allows clients to request exactly the data they need, no more and no less.

How GraphQL is different from REST?

GraphQL and REST are both API architectures, but they differ significantly. GraphQL uses a single endpoint and allows clients to request exactly the data they need, which prevents over-fetching and under-fetching of data. In REST, we hit different HTTP endpoints for different resources, which often leads to over-fetching of data.

Let’s take an example. we will use the open source project “Playground — https://graphql-teas-endpoint.netlify.app/

Scenario: if we want to get the producer and all the teas this particular producer produces, then in REST, we would have different endpoints to get producers and teas and save the values in global variables to use them in subsequent requests but with GraphQL, we can just get all this with a single query.

GraphQL schema

The GraphQL schema is composed of types and fields that make up the blueprint of things we can do with the data on a GraphQL server. The two main types of operations in a GraphQL schema are query and mutation.

Like in REST, we use HTTP methods POST, PUT/PATCH, and DELETE to manipulate the data. Likewise, in GraphQL we use “mutation ” type.

Similarly to read data, we use GET request in REST but in GraphQL we have “query ” type.

Query Type

It is used to fetch data.

In the above example, it is of query type and “LearningGraph” is the operation name of the query(any user-friendly name we can use) and “teas” is a root level field which takes 5 non-mandatory arguments and returns not null [Tea]! array of teas.

Mutation Type

It is used to insert, update and delete data.

In the example above, it is of mutation type and “LearningMutation” is the operation name of the query(any user-friendly name we can use) and “addTea” is a root level field which takes mandatory and not null(!) arguments and returns value for many fields such as id, name.

Things which will help to understand and create the query

1. Fields : Fields in a GraphQL Schema declare the type of data the field will return. They can return either an:

object

Or can return an array of objects

2. Arguments: Along with fields, arguments provide the power to request data in a very specific and customized manner. In GraphQL, every field in nested objects can have its own set of arguments.

Like: “producers” has its own set of arguments and nested object, “teas” has its own arguments.

Note: The above example is for demonstration purposes only, and it will not work since “producer.teas doesn’t take any argument”.

3. Aliases: It is helpful when fetching data from the same field with different arguments. We get conflicts when we want to get different sets of data from the same field. To overcome this, we use alias names.

Conflicts when we don’t use alias:

After alias, conflicts are resolved:

4. Variables: Are used to parameterise the arguments.

5. Fragment: It is a repetitive piece of code just like functions which can be called inside queries and mutations.

Below we have created 2 fragments, “fragment1” and “fragment2”. We can call these 2 fragments a number of times, and in a single query “LearnFragment” we get data from 2 fields (producers and teas).

GraphQL API as an interactive graph

To visually explore your GraphQL API as an interactive graph, we can use GraphQL Voyager.

GraphQL Voyager (graphql-kit.com)
Just change the schema and add the introspection.

If you are interested in joining us on our journey, please check out our careers page.

Thanks for reading!

--

--