GraphQL nested mutations with Apollo: small fatigue, max gain

Alessandro Annini
HackerNoon.com
5 min readFeb 10, 2017

--

The Scene

I am doing some experimentation and I want to try something with GraphQL on a React project. I already know something about GraphQL but I never really used it by myself.

I begin searching for a good GraphQL backend and I settle with Graphcool which seems the best out there at this moment: has a lot of guides and tutorials, it integrates with other services, it is very flexible and it’s free for developers. Bright green lights!

Graphcool has quickstart guides for both Apollo and Relay but I already decided what to use so I begin yarn-ing what it needs on my React project:

The Mission

I think about something not so original but always fun to work with: music bands and albums and songs and stuff.. always more exciting than Northwind Traders or Contoso Ltd. :)

So I just want to read/write data from/to Graphcool.

In GraphQL when you want to read you need Queries, when you want to write you need Mutations.

I am not writing about queries because the focus of this story is about nested mutations and because queries are well explained and I got no problems during the implementation.

LET THE PARTY BEGINS

With the Graphcool “Console” (an awesome interface for db modelling) I created a rough-and-ready structure.

As you can see we nested two arrays on two different levels starting from the albums field of the Band model.

So, to be clear, I want to store all this info using some data about STORMO music band, and I want to do it with a single mutation!

The Problem

The party suddenly crashes (and STORMO stop playing!) when I realize I don’t know how to create nested mutations, nor the (anyway very good) Apollo documentation can help me out.

Moreover I was brave enough to use arrays and now my eyes are lost in the window… the physical one!!

The Help

After a few not-worth-mentioning tries i decide to use the salvific Graphcool slack support where I begin stalking in the general channel.

Nilan Marktanner, from Graphcool staff begins to help me in no time and when i finally explain in a comprehensible way my situation, he hands me…

The Solution

Thanks to Nilan I relaize three things:

and that was exactly what i needed.

Code Time

The mutation I have to build

Using Apollo Client, I specify input arguments with GraphQL variables, so let’s see how it looks in this case:

Using a form I compose the data like this: (but pay attention, apollo wants this object called ‘variables’ and nothing else!)

Finally, inside my React component I can use all of this to send my data to Graphcool

Conclus… But wait! This is the beginning actually

  • GraphQL is not the future, is the present, and it’s soo disarmingly good, in loving memory of REST
  • Graphcool is an awesome service and its staff is careful and hyperactive (i’m quite sure they don’t sleep neither)
  • Was there some fatigue?… Yes, of course! But with this kind of fatigue, for every minute of time that you invest, you will save A LOT more later, it depends on the project.

P.S. The party now is going crazy, thanks to Storm{O}!!

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising &sponsorship opportunities.

To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--