Monitoring GraphQL Uptime
Tags: graphql, node, testing, devops
Published: 2020-07-07 13:02:39

Monitoring the uptime of a GraphQL application can't be done by just checking status codes. Asserted lets you write sophisticated uptime tests and even lets you use your client of choice if you prefer.

> Example on [GitHub](https://github.com/assertedio/graphql-uptime)

If you are unfamiliar with GraphQL, or just need a refresher on it, I strongly recommend reading through this [blog post series](https://medium.com/naresh-bhatia/graphql-concepts-i-wish-someone-explained-to-me-a-year-ago-514d5b3c0eab). It provides a suitably complicated example to demonstrate most of the features of GraphQL and how you would build a production application with it. I also used modified versions of the [code from that post](https://github.com/nareshbhatia/graphql-bookstore) in my example below.

## Example Server

The full example GraphQL server (even the simplified version for this example) is too large and complicated to be completely shown here. I recommend cloning the repo to take a look at the code, but I'll include snippets where I can.

The core of this example is a books model that has associated authors and publishers. The [book-related type definitions](https://github.com/assertedio/graphql-uptime/blob/master/src/graphql/typedefs/book.graphql) can be seen below.

{% gist https://gist.github.com/ehacke/0c8f41c9cc5747414d925a833428e1b0.js %}

These are handled by the [book resolvers](https://github.com/assertedio/graphql-uptime/blob/master/src/graphql/resolvers/book-resolvers.ts).

And the resolvers connect to the [book service](https://github.com/assertedio/graphql-uptime/blob/master/src/datasources/book-service.ts), which is too large to include here.

The [server itself](https://github.com/assertedio/graphql-uptime/blob/master/src/index.ts) is just a straightforward [ApolloServer](https://www.apollographql.com/server/). I did not include any authentication in this example in the interests of simplicity, but you can see that in the [Node API post](https://asserted.io/posts/node-api-uptime-tests).

{% gist https://gist.github.com/ehacke/0fd9804473439fab241dca7db34faab9.js %}

## Routine Configuration

As with the Node API example, the GraphQL routine doesn't require any special dependencies, so just the fixed dependencies are used.

{% gist https://gist.github.com/ehacke/34ff99876f850983f556ecdfda92fef9.js %}

If you wanted to include an Apollo client in the tests, or some other GraphQL specific libraries, you would need to upgrade to a paid plan to use the custom dependencies.

## Continuous Integration Tests

In these tests we do not have any special environment variables that we need to load, and we're just using the [got](https://www.npmjs.com/package/got) client to execute our requests. 

We create a unique book name at the beginning of the test, just to ensure we don't conflict with other books that may already exist in our theoretical production system.

{% gist https://gist.github.com/ehacke/7ea30a48e718a85fd1f9dcd2b97fc25c.js  %}

All of the tests we wrote can be seen [here](https://github.com/assertedio/graphql-uptime/blob/master/.asserted/simple.asrtd.js), but I'll list a few specific examples.

This test uses a more sophisticated query to get all of the other books written by a specific author. 

By being able to write arbitrarily sophisticated queries, you can deeply test all of the resolvers in your API.

{% gist https://gist.github.com/ehacke/05453ee9b3e07f7844cbfec081ddc5d3.js %}

Beyond just queries, we can create, update, and remove books as well.

{% gist https://gist.github.com/ehacke/dd6fc29d262d48a879c80f04b5c52ecb.js %}

By adding before and after hooks, we could further ensure that anything created during the test is wiped from production before the test exits.

## Next Steps

While the example shown [here](https://github.com/assertedio/graphql-uptime) can be cloned and run locally without an account, you'll need to do a few extra steps if you want to create your own Asserted routine to integration test your API in production.

1. Create an [Asserted](https://asserted.io) account. It's free and easy.
2. Complete the 2 minute onboarding to ensure that your environment is ready. You can also reference the docs [here](https://docs.asserted.io).
3. Start writing and running tests in prod!