Photo by James Pond on Unsplash

Easy and readable React/GraphQL/Node Stack — Part 2

Zac Tolley
Apr 25 · 6 min read

In Part 1 I described a simple full stack that integrates React JS with hooks, GraphQL and PostgreSQL and created simple GraphQL server that lets you list todos or get information about a single todo. In this article we will enhance the GraphQL server by adding support for nested properties and mutations. You can find the code for part 2 on Github.

The project so far just has a simple list of todo items.

Let’s introduce the idea of ‘Projects’ to our model. A project is used to group together todos so you have todos for Project A and another list of todos for Project B.

Create a new database migration to add a project table and a project id field to tasks. From the server folder:

Lets add some seed data too

And update the database

before you can take a look at your ‘Todo’ and ask to see the ‘Project’ for it you need to tell GraphQL it exists. When you defined the ‘Todo’ type you used basic types like ‘String’ and ‘Boolean’ but in this case a ‘Todo’ has a ‘Project’ property. So let’s start off by updating the schema to say that a ‘Todo’ has a ‘Project’, and at the same time tell it a ‘Project’ has many ‘Todo’ values.

Now, if you were to try and see a ‘Project’ for a ‘Todo’ by starting the server and updating the ‘Query’ you’d be disappointed. It will just tell you the ‘Project’ is null and it’s not. When Apollo Server gets the data from the database and looks at the schema and query to figure out what to return it looks for a resolver for that type and if it doesn’t find one it uses a default one that just returns the fields from the database. In this case there is a ‘project’ property but no ‘project’ field in the database so it just says null. In order to intelligently figure out how to turn database data into GraphQL data you need to write a resolver for each type. So we need to write a resolver for the ‘Todo’ type and then another for the ‘Project’ type. Let’s do the ‘Todo’ resolver.

A type resolver returns an object/map that provides a function for each property in the type. The function signature for each field is:obj, args, context, info. In the case of most type property resolvers you are only interested in obj which in our case is the data returned from the database. For most of the fields we just need to return the original value from the database with the exception of the ‘project’. When we want a ‘project’ property what we actually want is to use the ‘projectId’ from the database record and lookup a record in the ‘project’ table. After that we just add the resolver for ‘Todo’ to the collection of resolvers, like we did for Query.

Now fire up the playground and build a query to ask for the ‘Project’ title, if you don’t know GraphQL well enough yet just copy from the query in the screenshot.

And if we want to do the same in reverse from ‘Project’.

You will also need Query resolvers for ‘project’ and 'projects’. I’m going to let you do that, just cut and paste the ones created for ‘todo’ and ‘todos’.

Go back to your playground and

Finally mutations, how do you add a new project? When we created the Query resolvers we put them into a folder named ‘Query’ and built a map with ‘resolvers.Query.queryName’ so let’s do the same with ‘Mutation’.

Mutation resolvers use the same signature as type resolvers. In this case we don’t want the original object, there isn’t one, we want the arguments sent to the resolver. The mutation function then creates a record in the database using Knex and returns that object which in turn will be processed by the type resolver.

With regards to nested mutations I’ve not come across the need to use them. In theory they should work but your mileage may vary. The addTodo mutations is a little different as it raises the question of how you specify the project you want to associate the todo with? In this example we’ll follow a convention of sending the project as ‘projectId’ which matches the field name in the table we use and also seems a good match for ‘’. It also makes writing the mutation no different

Feel free to finish things up and write the update mutations and wire them up.

We now have a complete GraphQL API including nested objects and mutations. The next article in this series moves onto the front-end and describes how we can use react and start using data.

As always the code for this part in the series can be found on Github


Building online services

Zac Tolley

Written by

Building online services



Building online services