Effective Testing a GraphQL Server…
… with Jest and Apollo.
While working on a relatively simple demo with GraphQL using Apollo server, I suddenly realise that having a functional end-to-end test of features wasn’t the most straightforward thing to achieve … and literally no Medium post out there to help me out 😱!
This quick read outlines an approach to functional test GraphQL Queries and Mutations simply using basic mock injection and a powerful feature of GraphQL.
What’s out there for Testing
The recommended approach out of Apollo documentation covers quite well how to unit test your schema. While it is a bit confusing at the beginning it is quite powerful as you can actually specify your custom mocks and assert with confidence 😏.
The strongly-typed nature of a GraphQL API lends itself extremely well to mocking. This is an important part of a…www.apollographql.com
I am stating it is a bit confusing 🤨 because the quick setup pretty much fills your data result with a bunch of random integers and “Hello World” 🤗 strings just to prove that Types are correct. You can specify your custom Mocks and that works well (once you accept you get a collection of 2 items as data model result), but still very focused to the Schema Types part of the system.
Unit testing Resolvers is quite a straightforward task, as they should be developed as pure function 💪.
By testing the Schema and Resolver in isolation you can get to a decent code coverage but still, you won’t know how the system will behave when put together ☹️.
A strategy to create functional tests for Queries and Mutations from request to response with predictable mocked responses is needed. This can be added on top of the Schema Types and Resolvers unit tests … or if you feel very brave, it can even replace those isolated units 🤐.
Two strategical pieces of the puzzle to achieve the goal:
- Make proper use of
contextduring graphQL server setup 🙌. Context is an elegant way to provide (better said inject 💉) your resolvers with everything needed to deliver their job. An initial ‘quick & dirty’ implementation may not make use of
contextletting the resolvers figure out where to get the data from.
This is the root of all the evils. Instead, the dependency injection technique that
contextallows will help greatly when it comes to use Mocked Services.
- Get rid of
addMockFunctionsToSchema.That is a great solution for testing GraphQL Schema in isolation, but not what we want here.
Here a quick example on how to test a Query that fetches Movies and related Directors. There are comments on relevant line to explain what is going on.
Finally, the main server Setup (Apollo with Express) to support this kind of Mocking strategy.
Testing Schema and Resolvers in isolation is an ok technique and there are plenty tutorials out there on how to achieve this by using the valid
addMockFunctionsToSchema technique out of Apollo Tools.
Adding functional end to end Queries and Mutations tests by providing Mocked Services gives a great confidence your system is under control. Eventually, some of the units for Schema Types and Resolvers may then be consider redundant, allowing you to have less, but bigger, cases.
The full repository with a working ‘Movie & Directors’ GraphQL server is available at:
graphql-server-addc-2018 - Introduction to React Native and GraphQL for App Development - ADDC 2018 Workshopgithub.com
The server is the companion repository of the below React Native project:
react-native-graphql-addc-2018 - Introduction to React Native and GraphQL for App Development - ADDC 2018 Workshopgithub.com
Both repositories have been developed as part of the Workshop for developers: Introduction to React Native and GraphQL for App Development at ADDC Conference 2018 in Barcelona.
Please help improve this article with your thoughts & comments!