You Just Found The Best Node + Typescript + MongoDB GraphQL API Starter Kit

Not because I made it, but because I followed the best practices and resources available online.

Photo by Kira auf der Heide on Unsplash

Motivation

I’ve been working with GraphQL and Typescript for about 3 years now and so far I’m loving it. Those two technologies are a delight to work with, but they can become hard to implement.

This kit is a collection of my experience of building GraphQL API using NodeJS and Typescript but also the best practices found online in order to have a clean structure that can scale.

The kit and the instructions on own to use it are available on my GitHub here: https://github.com/Rieranthony/ts-graphql-starter

Prerequisite

The goal of this article is to demonstrate how to orchestrate all those technologies together but is not an in-depth tutorial on them. I will assume that you already have knowledge of GraphQL, Typescript, and MongoDB!

Overview of the kit

A clean folder structure

The folder structure as been heavily inspired by this awesome article following the SOLID principle of separation of concerns. If you’ve never read it before, this is a gold mine: please stop here and come back afterward!

The starter kit folder structure

Define your types only once ✌️

One of the biggest issues when using GrapthQL with Typescript and MongoDB is that you quickly find yourself with three type definitions. This is fine on a small project but it quickly becomes very hard to maintain and even small changes can be painful to do.

This example is fine on a small project but doesn’t scale really well

Thanks to two awesome libraries (type-graphql and typegoose) that are both using TypeScript decorators, all of those declarations can be merged in one like so:

Only one declaration needed! 🤘

Easy and robust testing strategy

Testing your API is a crucial step to ship good quality features with peace of mind. Mixing the apollo-server-testing package, Jest, and MongoDB-memory-server integration testing of your API has never been easier and powerful than that.

This awesome combo allows you to test your API, from your resolver to your response passing through your service and your model; all at once. All of that with a real MongoDB instance running in memory! Awesome isn’t it?

In a nutshell, in this test we build our schema using our resolvers, services and models and then we create an Apollo Server instance with it.

We then create a test client that executes a real GraphQL mutation (createTodo) onto our newly created server.

We then store the result of the mutation in a snapshot and check the result is what we expected! That’s it!

The outputof the createTodo mutation in the Jest snapshot

If you want to learn more about the technologies involved in the starter I highly recommend you watch the full Ben Awad’s video series about it, it’s awesome!

Of course, there is always room for improvements but this is how we ended up doing on Peel a new dating app for serious relationship we recently launched in Toronto, and it works perfectly!

Don’t forget I’m always open to comments and changes on this starter-kit and I can’t wait to get your feedback on it!

Happy coding 🎉

Anthony Riera

Anthony Riera

Software developer — Trying to write more about tech — Currently working on https://peel.dating