GraphQL API Workflows

API Fortress can easily test GraphQL API workflows. What’s GraphQL? Well, it might be best just to take a look at how they describe themselves:

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier
to evolve APIs over time, and enables powerful developer tools. – graphQL.com

GraphQL allows developers to access their data in a more organic manner by using queries and mutations to retrieve information and affect changes. It’s more dynamic than your typical API transaction, because a consumer can request the format that the provider returns the data in. This is true of both principal types of GraphQL transactions. Queries allow us to retrieve information from a server and are closer in function to a GET request. Mutations allow us to affect changes on data housed in the server and are closer in type to POST, PUT, PATCH or DELETE in function. GraphQL is a fantastic tool for creating versatile, eminently flexible servers. With API Fortress, testing GraphQL queries is as easy as testing regular REST endpoints.

API Fortress provides a demonstration GraphQL environment at:

GraphServerlet

The query and mutation below are formatted for this environment.

Testing a Query:

If the server has GraphiQL enabled, creating your queries through that tool and then copying the generated URLs into an API Fortress GET component is an acceptable process. However, this process does not lend itself to either readability or replicability. The preferred method is passing the query as a POST body.

If we’re sending a query request to our GraphQL server, we would format our POST body as follows:

The above object says the following: we are querying for a specific course by ID, and wish our response body to contain the ID, Title, Author, Description, Topic, and URL of that entry. If we send this body as a POST to our test GraphQL environment, our response will look like this:

Since our response is a JSON body, API Fortress is fully capable of automatically generating a test to validate future responses from this query.

Testing a Mutation:

A mutation is a GraphQL operation that makes a change to the data being provided. Whereas a GraphQL Query that performs a READ, a Mutation performs a CREATE, UPDATE, or DELETE, rounding out our CRUD operations.

A Mutation is also passed as a POST body to the GraphQL endpoint in question:

This Mutation is executing the ‘updateCourseTopic’ operation on the database entry with the ID of 1 and changing its ‘topic’ value to ‘Ruby’. Note the ‘mutation’ keyword in place of the ‘query’ keyword from the Query example. This Mutation would return the following response from our test GraphQL environment:

Again, as this is a valid JSON response body, API Fortress is able to generate a test automatically to validate its schema in the future.

GraphQL is an extremely versatile tool that allows for complex data management. Rather than setting up multiple endpoints or conditional logic, a developer can create a framework for queries that allows dynamic data retrieval and modification. API Fortress then allows for the automatic generation of schema validation tests which will ensure that these queries and mutations are providing the expected result every time.