How to Test GraphQL APIs
  August 20, 2019

Amazon (AWS), Facebook, Twitter, GitHub and many other iconic tech giants are transitioning from RESTful to GraphQL APIs for both internal and external use.

GraphQL is a query language for APIs and a runtime for fulfilling those queries with existing data in a database. Rather than patching together data from multiple RESTful server responses, GraphQL enables apps to get all of the data they need in a single request — it's like SQL for APIs rather than databases.

There are many different resources for learning GraphQL, but there's less information out there covering how to test GraphQL. While it's relatively straightforward to test RESTful APIs endpoints, testing GraphQL APIs can be a bit more challenging since many existing tools don’t support GraphQL.

Let's take a look at how to test GraphQL APIs, differences with testing RESTful APIs and some best practices to keep in mind.

There are many different resources for learning GraphQL, but testing is often neglected.

What is GraphQL?

GraphQL is a query language for APIs and a runtime for fulfilling those queries with existing data. While REST APIs require loading from multiple resources, or URLs, GraphQL APIs get all of the data that the app needs in a single request by using types and fields rather than individual URL endpoints with canned responses.

GraphQL services (or resolvers) can be written in any language, but the GraphQL schema language keeps things uniform in a language-agnostic way. Schemas contain object types that represent a kind of object that you can fetch and its associated fields in a format that should be familiar to most developers.

    type Character {
        name: String!
        appearsIn: [Episode]!

Each field on each type is backed by a function called the resolver, which is provided by the GraphQL server developer. When a field is executed, the corresponding resolver is called to produce the next value. When all of the fields produce a scalar value, rather than returning another object, then the execution completes.

One key benefit of this approach is that backend developers don’t have to worry about writing specific responses or versioning APIs. This means that front-end teams can move a lot faster without having to wait on backend changes to deploy. Another benefit is that the front-end only requests the data it needs, which cuts down on the number of queries that need to be made and thereby improves both back-end and front-end performance.

How to Test GraphQL

Most functional GraphQL testing is designed to ensure that the schema, queries and mutations work as-expected on the front-end. There is a growing ecosystem of tools available to run these kinds of tests depending on your language, platform and test infrastructure, as well as your specific testing requirements.

For example, EasyGraphQL is one of the most popular functional GraphQL testing tools for JavaScript developers. By integrating with a library like Mocha, you can easily test assertions to verify API responses as part of an automated test suite.

Here’s an example of an assertion using EasyGraphQL:

    t(‘should pass if the query is valid’, () => {
        const validQuery = `
                getUserByTestResult(result: 4.9) {

        tester.test(true, validQuery)

There are a few different types of tests that you should use:

  • Query tests: Query tests ensure that a given query (and any parameter(s)) returns the correct response.
  • Mutation tests: Mutation tests ensure that a given query (and any parameter(s)) successfully save data in a database.
  • Load tests: Load tests ensure that an API remains performant (within SLAs) with a high number of requests.
  • Security tests: Security tests ensure that APIs don't return sensitive data without necessary precautions.

If you're testing external web services using GraphQL, such as GitHub V4, you may also want to simulate responses to avoid unnecessary usage and reduce test run times. Mocks and fixtures can effectively simulate these services in some cases, but in other cases, it helps to virtualize them to analyze usage and other metrics.

Using ReadyAPI for GraphQL

ReadyAPI makes it easy to test GraphQL queries and mutations, assess performance with load testing and even virtualize web services. You can create, manage and execute all of these automated tests in one centralized interface without writing a ton of code, which accelerates API quality for Agile and DevOps teams.

Don't forget to download our free checklist of GraphQL testing libraries to see your options.

You can get started by importing API definitions created in tools like Swagger and begin immediately testing and recording live API traffic to build a profile. Using our virtualization capabilities, you can also remove pipeline dependencies from third-party APIs to speed up your test suite and reduce unnecessary API usage.

GraphQL testing is built-in to ReadyAPI out of the box, enabling you to easily test queries and mutations, just as you would any RESTful API.


Example of the ReadyAPI interface for testing GraphQL.

Using this interface, simply write queries using the GraphQL syntax and specify any variables used in the query in JSON format. You can then run the test step on its own, or add an assertion to verify a particular response using properties like contains, equals, counts and even regular expression matches.

These tests can be easily integrated with Jenkins, Docker, Visual Studio Team Services, Team City or other continuous integration or builder tools that your team uses. You can also view test results, review errors and accomplish other standard functionality using a web-based interface rather than the command line.

The Bottom Line

GraphQL has become tremendously popular since its introduction. In addition to Facebook, many tech companies have adopted the query language for their internal and external APIs. The good news for DevOps teams is that testing isn't all that complicated — it just requires the right tools and processes in place.

Sign up for a free trial of ReadyAPI today to see how easy it is to consolidate and automate your API tests!