In this short tutorial, I wanna show you how to set up a Fastify web server implementing GraphQL via the mercurius module, and adding the first automated test using supertest while using TypeScript.

To provide you some boilerplate for your future projects and as a reference for this article I created a small GitHub repository that includes all the code mentioned in this article, so make sure to clone it and have it open as a reference while reading this article.

Abszissex/medium-fastify-graphql

First of all, we have a very simple file structure. On the root level, we have our config files like tsconfig.json.eslintrc.jsonjest.config.jsand our package.json. Right beneath we only have our src , dist and node_modules folders, so nothing really fancy here.

The whole source (in our case only one index.ts ) is located under the src folder and right beneath it we have a test folder containing our tests. Of course, you are absolutely free to change the structure to your likes. I for myself like it when I have one test file right beneath the actual implementation file it should test, even though I did a bit otherwise here.

#javascript #graphql #programming #testing #nodejs

Set up GraphQL with Fastify, Mercurius, TypeScript, and Automated Testing
34.75 GEEK