You are viewing the preview version of this book
Click here for the full version.

Pipeline resolvers

AppSync resolvers come in two varieties: unit and pipeline. Unit resolvers are "normal" resolvers with a data source and the glue code attached to a field. When AppSync resolves the field, it calls the resolver and interacts with the data source.

The main drawback of unit resolvers is that they allow only a single call to a data source. How to implement a resolver when it needs to call the database multiple times? Or when you need to interact with a different system and write some data? For example, if users are managed by Cognito but all of them also needs to be inserted into a database, then you'd need two data sources: an HTTP that interacts with the Cognito API, and a second one to put an item to DynamoDB.

Unit resolvers don't support Javascript

Currently, unit resolvers don't support writing resolver code in Javascript, the only runtime available for them is the original VTL-based one. Since VTL is horribly error-prone and very hard to use, unit resolvers are not useful at this moment. But since a unit resolver is a special case of a pipeline resolver, all the examples in this book that would otherwise be units are written as 1-step pipelines.

With only unit resolvers, this is only possible with a Lambda function that the resolver calls and that does both operations.

A Lambda resolver can call other services

Pipeline resolvers provide a way to solve this without opting out of the AppSync resolver architecture. In practice, they allow extending a unit resolver into a pipeline for simple cases, so you only need to resort to Lambda functions when the resolver becomes too complicated. Note though that there is a hard limit of 10 functions per pipeline.

A pipeline is made up of individual functions, each behaving like a unit resolver with a data source, a request, and a response mapping template. Moreover, each of them gets the result of the previous function, allowing them to generate a response in steps. Finally, the whole pipeline also has a request and response mapping template acting as a transformer for the first and the last functions.

As each function is a full-blown resolver, they can interact with different data sources. The first might make a HTTP request, while the second executes a DynamoDB transaction. In practice, pipeline resolvers are powerful: especially as the HTTP resolver can call AWS APIs, AppSync can handle a surprising amount of processes.

Pipeline resolver

On the console, functions have a dedicated menu where you can manage them:

Resolver functions have a separate menu entry

Note that each function has a data source configured.

Then for the resolver, add a Pipeline for the field:

A pipeline resolver is configured for a field

And in the resolver, specify the functions to run:

A pipeline resolver with two functions

In this chapter, we'll see the programming constructs available for pipeline resolvers and the best practices when working with them.

Previous result

Since each function returns a value and in a pipeline they run sequentially, each function has access to the previous one's result. This value is in the ctx.prev.result and is constantly overwritten when a function finishes.

This value is the primary connecting link between the functions in the pipeline: each interfacing with a data source, and each getting the result of the previous function, and the last value is the result of the pipeline itself.

ctx.prev.result is the result of the previous function

For example, the first function might fetch an SSM parameter:

return {
  version: "2018-05-29",
  method: "POST",
  params: {
    headers: {
      "Content-Type": "application/x-amz-json-1.1",
      "X-Amz-Target": "AmazonSSM.GetParameter"
    },
    body: {
      Name: "${aws_ssm_parameter.value.name}",
      WithDecryption: true
    }
  },
  resourcePath: "/"
}

Then return the stored value:

There is more, but you've reached the end of this preview
Read this and all other chapters in full and get lifetime access to:
  • all future updates
  • full web-based access
  • PDF and Epub versions