Testing an Apollo GraphQL server using apollo-server-testing, Jest and msw

Bart Wijnants
3 min readNov 14, 2020
Photo by Alex Kondratiev on Unsplash

How do you easily test an Apollo GraphQL server that gets data from external api’s?

I had the same question, so I tried it myself and a couple hours later I had Grates.

Grates, which is short for GRAphql TESting of course, is a minimal setup for testing an Apollo GraphQL server that gets data from external api’s using apollo-server-testing, Jest and msw.

In this article I’ll guide you through creating your own Grates.

If you don’t want to read all of this just head to the GitHub repo!

Prerequisites

  • node
  • some knowledge about GraphQL
  • not enough knowledge about testing a GraphQL server

Setup the Apollo GraphQL server

Get a terminal going and type in some stuff:

mkdir grates
cd grates
npm init --yes
npm install apollo-server graphql axios
mkdir src
touch src/index.js src/resolvers.js src/server.js src/type-defs.js

Add a start script to package.json:

"start": "node src/index.js"

Get a dad joke from the icanhazdadjoke api in resolvers.js:

--

--