How to set up a powerful API with GraphQL, Koa, and MongoDB — scalability, and testing

So far we achieved basic CRUD functionality. Check out the previous episode before continuing;

As our app grows, so does our mutation count. In order to have a clean codebase as we can, we should extract the mutations to dedicated files. This way we can assure our code is modular and separated into maintainable chunks.

Let’s create a folder graphql/mutations and inside the folder create addGadget.js, updateGadget, and removeGadget files.

We simply place the mutation objects into the files and export them.

Moving addGadget mutation to separate file


Update mutation;


And finally the delete mutation.


After we have them in separate files, we’re ready to change our graphql/mutations.js file.

Lovely — much cleaner!

Here’s how our project should look like now;

Our GraphQL API is getting into great shape. So far we have a dedicated folder for mutations. Let’s do the same for queries.

Create a folder for the queries — graphql/queries and place the rootQuery.js file there.

Inside the rootQuery.js file we place all the queries which just fetch data. This way we have separate folders for queries and mutations. The difference between queries and mutations is simple–queries just read data from the database, mutations change the state of our database.


Import the rootQuery to our schema.js

Now inside the rootQuery we specify all the queries. Create a file for fetching all gadgets and fetching gadgets by id.

  • graphql/queries/queryAllGadgets.js
  • graphql/queries/queryGadgetById.js

Place the following logic inside graphql/queries/queryGadgetById.js

And for fetching all gadgets — graphql/queries/queryAllGadgets.js

Notice the new type GraphQLList – this is used if we want to return a list of objects–all gadgets in this case.

Now we have to import the queries to our rootQuery


Now, Let’s query all the gadgets from our database.

Here’s the GraphQL query;

queryAllGadgets {

Our project structure should look like the following;

Now we have dedicated folders for querying and mutating.

Types Folder

Ideally, we want to have a folder for GraphQL types as well. So let’s move the gadgetType.js there.


Don’t forget to update the paths where we use the gadgetType (mutations and queries)

const gadgetGraphQLType = require('./../types/gadgetType');

Testing our queries

Before moving to more advanced concepts, I’d like to show you a tool which lets us query graphql queries, if you ever used postman or insomnia you feel at home.

Head over to and download the client. I use Insomnia since it’s free, has great functionality out of the box, and is open sourced.

Disclaimer: I’m not affiliated with Insomnia– I just like the tool.

Open the tool, create a new request called queryAllGadgets. At the top place our localhost address (http://localhost:9000/graphql) and specify that the body is graphQL.

And finally, place the graphQL query inside the body request.

Voila! We can query our graphQL in a headless manner now, just like we do with REST. If REST can do it, so can GraphQL.

Finishing touches

Adding all our queries to insomnia and hitting save. This way we have quick access to our queries.

Save the project. We’ll need it for later

Beautiful! We have successfully refactored our graphQL to a scalable version with headless testing.

Here’s the source code for the chapter.

Checkout out part 4 where we deploy our GraphQL API and make it secure.

Thanks for reading!