GraphQL is an open-source query and manipulation language for APIs developed by Facebook. Industry leaders and big companies such as Facebook, Airbnb, and Github are adopting GraphQL.1 Developers use GraphQL in conjunction with REST APIs or sometimes even as a direct alternative.
Just like REST API testing, GraphQL testing is necessary for users to ensure the stability of GraphQL APIs. Contrary to REST API testing, however, GraphQL testing can be challenging because few tools support GraphQL. This article aims to close the information gap by giving information about the types and components of GraphQL testing.
What is GraphQL testing?
Figure 1: Representation of GraphQL
The main objective of GraphQL testing is to ensure that the schemas, queries, and mutations created for an API operate as expected. As GraphQL is a service located between front-end systems and backend APIs, GraphQL testing is crucial for maintaining the stability of the services offered by APIs.
GraphQL has a single endpoint (see Figure 1 above), making it a direct connection between the data source and the customer. For that reason, GraphQL is essential for testing purposes.
API testing is a key practice to ensure an API’s stability. PULSE is an API test automation tool created by Testifi. It can reduce cycle time and cost by 50% and improve documentation. Well-known companies like BMW and Amazon use Testifi services.
Top 3 benefits of GraphQL Testing
GraphQL testing provides the following benefits:
- Better overview of applications: GraphQL testing will enable the tester to understand better objects and data structures returned by the queries.
- Eliminate unforeseen bugs: GraphQL is flexible, and testing helps users ensure that the number of errors is minimal.
- Stability: GraphQL testing improves the overall quality and stability of GraphQL APIs by checking and ensuring the functions of queries, resolvers, schemas, and mutations.
Types of GraphQL testing
There are multiple methods for testing GraphQL:
We can implement the Trophy Scheme to get an idea for the preference of the methods (See Figure 2).
Figure 2: The Testing Trophy
We list the reasoning behind the testing trophy (see Figure 2) as follows:
- Static type checks the code and shows if there is anything wrong.
- Unit tests are relatively easy to make because they test small units of code and can run fast.
- Integration tests are slower because they test multiple layers of code. The tester can use server mock and check the fluxes of the test.
- End-to-End tests are much slower and harder to make because they involve multiple products such as servers, clients, and databases. These tests are used to check a routine.
Components to test in GraphQL
Here are the following components you should conduct test on:
- Schema: Schemas can be tested using static type checks and mock schema & test queries.
- Mutations: Mutations can be tested by using EasyGraphQL’s tester function or GraphiQL’s mutation query in its client. It will ensure that the database functions properly.
Which tools to test the components of GraphQL?
Est-lint-plugin-graphql is a linter to extend Eslint rules, and graphql-schema-linter is a tool to validate the schema definition. In addition to static type checks, mocking the schema using a mock-server via graphql-tools7(a set of npm packages) while paying attention to possible combinations will be beneficial for schema testing.
Testing queries and mutations
While we can use an automated approach by using libraries “request” and “supertest” for query testing, we can mock queries and mutations via easygraphql-tester.8 We can also make test assertions directly via easygraphql-tester instead of mocking. Below, you can see the direct test assertion example.
Source: NDC Conferences9
Source: NDC Conferences 10
If you have further questions concerning API testing you can reach us:
Next to Read
Your email address will not be published. All fields are required.