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;
How to set-up a powerful API with GraphQL, Koa, and MongoDB — CRUD
This is a series where we learn how to set-up a powerful API with GraphQL, Koa, and Mongo. The primary focus will be on…
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
We simply place the mutation objects into the files and export them.
addGadget mutation to separate file
And finally the delete mutation.
After we have them in separate files, we’re ready to change our
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.
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
Now inside the rootQuery we specify all the queries. Create a file for fetching all gadgets and fetching gadgets by id.
Place the following logic inside
And for fetching all gadgets —
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
Now, Let’s query all the gadgets from our database.
Here’s the GraphQL query;
Our project structure should look like the following;
Now we have dedicated folders for querying and mutating.
Ideally, we want to have a folder for GraphQL types as well. So let’s move the
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.
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.
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.
Tutorial how to set up koa with graphql and mongodb - wesharehoodies/koa-graphql-mongodb
Checkout out part 4 where we deploy our GraphQL API and make it secure.
How to set up a powerful API with GraphQL, Koa, and MongoDB — deploying to production
Our GraphQL runs smoothly locally, but what if we want to share it with the world?
Thanks for reading!