GraphQL mutations: Partial updates implementation

How to manage partial changes on existing resources with GraphQL mutations

Arnaud Bezançon
WorkflowGen
4 min readMay 29, 2017

--

In REST, we can use PUT or PATCH HTTP verbs to manage server-side changes, with PUT to update an existing resource and PATCH to apply a set of changes. In GraphQL, you have to create mutations to modify server-side data, and different implementations are possible to support partial updates.

This article will focus on the implementation that seems the most “natural” to us thanks to some features introduced in GraphQL-js v0.8.0 with its support for null values as field arguments.

I’ll use CodeSandbox.io for my live examples, so you can just play the queries or change the GraphQL server code directly to do your own experiments.

Important: I recommend either duplicating the code (via the Fork menu) for live testing, or downloading the sample.

PUT equivalent

In this new sandbox we’ve created a new mutation called updateAuthor to update an author:

The updateAuthor resolver:

In this version of the updateAuthor resolver, the mutation input field values are used to update an existing author. Because the firstName and lastName input fields are defined as non-required in the schema, you can set their values to null if needed.

If one input field is omitted in the mutation the corresponding value is set to null, which means that this solution is not usable for partial updates.

This mutation updates firstName and lastName with the provided input field values:

This mutation implicitly sets lastName to null:

This mutation explicitly sets lastName to null:

Partial update management

This other sandbox includes a new version of the updateAuthor resolver:

In the resolver function, before updating the author fields, some tests check if the corresponding property is defined in the mutation arguments.

Destructuring syntax is used in this resolver to unpack values from the arguments.

But if you use this syntax for your resolver (without destructuring the arguments):

you can check if the property is defined in args like this:

This mutation works as expected: lastName is not altered (no implicit null).

With this pattern, a non-required input field can:

  • have a value,
  • have a value set to null, or
  • not exist (no changes to do on the existing resource)

You can add some logic in the resolver to reject null values for some non-required fields if needed according to your business rules.

Partial updates using query variables

We use query variables in the mutation like this:

Partial updates can be managed by defining the variables for the fields we want to update and omitting the ones we don’t want to update. For example, to update firstName only, the query variables will be:

Conclusion

This implementation of partial updates in GraphQL is straightforward for API users since they only have to use a single mutation per resource (e.g. updateUser) to manage PUT and PATCH operations by including the input fields corresponding to the values to update on the server-side.

In our front-ends using Apollo Client (React-Native and React), we only have to define the query variables to update when calling the mutations; there are no new GraphQL query templates to create.

Other implementations and patterns are possible thanks to GraphQL’s flexibility and openness, and you can choose the one that best fits your requirements and constraints.

--

--

Arnaud Bezançon
WorkflowGen

Full Stack Architect, Creator of WorkflowGen, Advantys co-founder and CTO.