AppSync: skipping nullable nested resolvers by returning early

Yan Cui

I help clients go faster for less using serverless technologies.

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.

type University {
  name: String!
  categories: [Category!]
}

type Category {
  name: String!
  sports: [CategorySport!]
}

type CategorySport {
  name: String!
  description: String
  coach: Coach # in the DynamoDB table, this is stored as a String
}

type Coach {
  id: ID!
  firstName: String!
  lastName: String!
}

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.

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 to help me configure everything.

mappingTemplates:
  - type: CategorySport
    field: coach
    dataSource: userTable
      
dataSources:
  - type: AMAZON_DYNAMODB
    name: userTable
    config:
      tableName: !Ref UserTable

And I configured the VTL templates for the request:

{
  "version" : "2017-02-28",
  "operation" : "GetItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($context.source.coach)
  }  
}

and response:

$util.toJson($context.result)

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

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. He pointed me to this helpful post which mentioned #return keyword that lets you short-circuit a resolver execution and return early.

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

#if ($util.isNullOrEmpty($context.source.coach))
  #return
#end

{
  "version" : "2017-02-28",
  "operation" : "GetItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($context.source.coach)
  }
}

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.


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.