AppSync: skipping nullable nested resolvers by returning early
Tags: appsync, aws, graphql, serverless
Published: 2020-04-04 08:53:07

---
title: AppSync: skipping nullable nested resolvers by returning early
published: true
date: 2020-04-04 00:31:55 UTC
tags: AppSync,AWS,GraphQL,Serverless
canonical_url: https://theburningmonk.com/2020/04/appsync-skipping-nullable-nested-resolvers/
---

Whilst working on a client project, I ran into an interesting problem with AppSync which I couldn’t find an answer after a lot of googling. I hope this article can save you the same pain should you run into the same problem.

The problem is that when you have a nullable nested field that you want to expand, you have to handle the null values in your VTL template and skip them. It was not obvious how to do this in a managed system like AppSync.

To give you a better idea of what I’m talking about, here is a drastically simplified version of the GraphQL schema.

{% gist https://gist.github.com/theburningmonk/1aaa84fd2c0e3ac97c72451b5fb74bc4 %}

![](https://theburningmonk.com/wp-content/uploads/2020/04/img_5e87cea7436bb.png)

In the DynamoDB table, universities are stored in the `University` table and users in the `User` table. Categories and sports are modelled as properties of a university to minimize the no. of DynamoDB reads. And the `CategorySport.coach` field is the profile ID for a user in the `User` table.

![](https://theburningmonk.com/wp-content/uploads/2020/04/img_5e87d1c215d56.png)

To make it easier for the UI to consume the data, we expand the `CategorySport.coach` field to be a fully fledged `Coach` type.

So, I set up the resolver for this nested field and point it at the `User` table. p.s. I’m using the Serverless framework with the excellent [serverless-appsync-plugin](https://github.com/sid88in/serverless-appsync-plugin) to help me configure everything.

{% gist https://gist.github.com/theburningmonk/87fbd6ea04c995510179ed265533286e %}

And I configured the VTL templates for the request and response.

{% gist https://gist.github.com/theburningmonk/7a4db8c4f14aa339955dee48d73c49b7 %}

{% gist https://gist.github.com/theburningmonk/5081146b94484efcc1aff4cf104759d3 %}

And everything works great, except when `CategorySport.coach` is `null`.

![](https://theburningmonk.com/wp-content/uploads/2020/04/img_5e87d4d02045d.png)

What I needed, was a way to short-circuit the `DynamoDB.GetItem` request when `CategorySport.coach` is `null`. After a lot of searching, my google fu yielded nothing and I turned to my good friend [Heitor Lessa](https://twitter.com/heitor_lessa). He pointed me to [this helpful post](https://adrianhall.github.io/cloud/2019/01/03/early-return-from-graphql-resolvers/) which mentioned `#return` keyword that lets you short-circuit a resolver execution and return early.

![](https://theburningmonk.com/wp-content/uploads/2020/04/img_5e87d6afae4ee.png)

With this, I was able to work around the problem by adding just 3 lines of code to my request template.

{% gist https://gist.github.com/theburningmonk/2c2f83b6c017cbbdb1af296d0ff1c121 %}

One thing to note is that you can’t have `#return(null)` but `#return` would `null` as the result for the resolver, and skip the DynamoDB `GetItem` operation and the response template altogether.

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.

[![](https://theburningmonk.com/wp-content/uploads/2019/10/Screenshot-2019-10-19-at-11.44.09.png)](https://www.patreon.com/bePatron?u=905909)

 ![](https://theburningmonk.com/wp-content/uploads/2019/10/img_5dab0c477fc72.png)

Hi, my name is **Yan Cui**. I’m an **[AWS Serverless Hero](https://aws.amazon.com/developer/community/heroes/yan-cui/)** and the author of [**Production-Ready Serverless**](https://bit.ly/production-ready-serverless). I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

You can contact me via [Email](mailto:theburningmonk.com), [Twitter](https://twitter.com/theburningmonk) and [LinkedIn](https://www.linkedin.com/in/theburningmonk/).

[Hire me.](https://theburningmonk.com/hire-me)

The post [AppSync: skipping nullable nested resolvers by returning early](https://theburningmonk.com/2020/04/appsync-skipping-nullable-nested-resolvers/) appeared first on [theburningmonk.com](https://theburningmonk.com).

 ![](http://feeds.feedburner.com/~r/theburningmonk/~4/q7hExyMsciI)