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.
If the server has GraphicQL 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.
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.