Skip to content

Commit 7f3349d

Browse files
authored
feat(resolvers): JavaScript resolvers (#534)
1 parent 3b7022d commit 7f3349d

22 files changed

+1039
-511
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ jobs:
1717
matrix:
1818
node: [16]
1919
steps:
20-
- uses: actions/setup-node@v1
20+
- uses: actions/setup-node@v3
2121
with:
2222
node-version: ${{ matrix.node }}
2323
- name: Checkout code
24-
uses: actions/checkout@v2
24+
uses: actions/checkout@v3
2525
- name: Install dependencies
2626
run: npm install
2727
- name: Lint

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- name: Checkout
15-
uses: actions/checkout@v2
15+
uses: actions/checkout@v3
1616
- name: Setup Node.js
17-
uses: actions/setup-node@v2
17+
uses: actions/setup-node@v3
1818
with:
1919
node-version: 16
2020
- name: Install dependencies

doc/general-config.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ appSync:
4848
- `caching`: See [Cacing](caching.md)
4949
- `waf`: See [Web Application Firefall](WAF.md)
5050
- `logging`: See [Logging](#Logging)
51-
- `defaultMappingTemplates`:
52-
- `request`: Optional. A default request mapping template filename for all resolvers.
53-
- `response`: Optional. A default response mapping template filename for all resolvers.
54-
- `mappingTemplatesLocation`:
55-
- `resolvers`: The location where to find resolver mapping templates, relative to the service path. Defaults to `mapping-templates`.
56-
- `pipelineFunctions`: The location where to find pipeline functions mapping templates. Defaults to the same value as `mappingTemplatesLocation.resolvers`.
5751
- `xrayEnabled`: Boolean. Enable or disable X-Ray tracing.
5852
- `tags`: A key-value pair for tagging this AppSync API
5953

doc/pipeline-functions.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,33 @@ When you use `PIPELINE` [resolvers](resolvers.md), you will also need to define
44

55
It's a key-value pair object whose key is the name of the function and the value is its configuration.
66

7-
The definition can also be a string in which case it's the [dataSource](dataSources.md) name to use. The other attributes use the default values.
8-
97
## Quick start
108

119
```yaml
1210
appSync:
1311
pipelineFunctions:
14-
myFunction: myDataSource
15-
myOtherFunction:
16-
dataSource: myOtherDataSource
12+
myFunction:
13+
dataSource: myDataSource
14+
code: myFunction.js
1715
```
1816
1917
## Configutation
2018
2119
- `dataSource`: The name of the dataSource to use.
2220
- `description`: An optional description for this pipeline function.
23-
- `request`: The request mapping template file name to use for this resolver, or `false` for [direct lambda](https://docs.aws.amazon.com/appsync/latest/devguide/direct-lambda-reference.html). Defaults to `{functionName}.request.vtl`.
24-
- `response`: The request mapping template file name to use for this resolver, or `false` for [direct lambda](https://docs.aws.amazon.com/appsync/latest/devguide/direct-lambda-reference.html). Defaults to `{functionName}.response.vtl`.
21+
- `code`: The path to the JS resolver handler file, relative to `serverless.yml`.
22+
- `request`: The path to the VTL request mapping template file, relative to `serverless.yml`.
23+
- `response`: The path to the VTL response mapping template file, relative to `serverless.yml`.
2524
- `maxBatchSize`: The maximum [batch size](https://aws.amazon.com/blogs/mobile/introducing-configurable-batching-size-for-aws-appsync-lambda-resolvers/) to use (only available for AWS Lambda DataAources)
2625
- `substitutions`: See [VTL template substitutions](substitutions.md)
2726
- `sync`: [See SyncConfig](syncConfig.md)
2827

28+
## JavaScript vs VTL vs Direct Lambda
29+
30+
When `code` is specified, the JavaScript runtime is used. When `request` and/or `response` are specified, the VTL runtime is used.
31+
32+
To use [direct lambda](https://docs.aws.amazon.com/appsync/latest/devguide/direct-lambda-reference.html), don't specify anything (only works with Lambda function data sources).
33+
2934
## Inline DataSources
3035

3136
Just like with `UNIT` resolvers, you can [define the dataSource inline](resolvers.md#inline-datasources) in pipeline functions.

doc/resolvers.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ All the Resolvers in your AppSync API can be found in serverless.yml under the `
44

55
Resolvers are defined using key-value pairs where the key can either be an arbitrary name for the resolver or the `type` and `field` in the schema it is attached to separated with a dot (`.`), and the value is the configuration of the resolver.
66

7-
The definition can also be a string in which case it's the [dataSource](dataSources.md) name to use. The other attributes use the default values.
8-
97
## Quick start
108

119
```yaml
@@ -14,8 +12,6 @@ appSync:
1412
Query.user:
1513
dataSource: myDataSource
1614

17-
Query.users: myDataSource
18-
1915
getPosts:
2016
type: Query
2117
field: getPosts
@@ -26,15 +22,34 @@ appSync:
2622
2723
- `type`: The Type in the schema this resolver is attached to. Optional if specified in the configuration key.
2824
- `field`: The Field in the schema this resolver is attached to. Optional if specified in the configuration key.
29-
- `kind`: The kind of resolver. Can be `UNIT` or `PIPELINE` ([see below](#PIPELINE-resolvers)). Defaults to `UNIT`
25+
- `kind`: The kind of resolver. Can be `UNIT` or `PIPELINE` ([see below](#PIPELINE-resolvers)). Defaults to `PIPELINE`
3026
- `dataSource`: The name of the [dataSource](dataSources.md) this resolver uses.
3127
- `maxBatchSize`: The maximum [batch size](https://aws.amazon.com/blogs/mobile/introducing-configurable-batching-size-for-aws-appsync-lambda-resolvers/) to use (only available for AWS Lambda DataSources)
32-
- `request`: The request mapping template file name to use for this resolver, or `false` for [direct lambda](https://docs.aws.amazon.com/appsync/latest/devguide/direct-lambda-reference.html). Defaults to `{type}.{field}.request.vtl`.
33-
- `response`: The request mapping template file name to use for this resolver, or `false` for [direct lambda](https://docs.aws.amazon.com/appsync/latest/devguide/direct-lambda-reference.html). Defaults to `{type}.{field}.response.vtl`.
28+
- `code`: The path of the JavaScript resolver handler file, relative to `serverless.yml`. If not specified, a [minimalistic default](#javascript-vs-vtl) is used.
29+
- `request`: The path to the VTL request mapping template file, relative to `serverless.yml`.
30+
- `response`: The path to the VTL response mapping template file, relative to `serverless.yml`.
3431
- `substitutions`: See [VTL template substitutions](substitutions.md)
3532
- `caching`: [See below](#Caching)
3633
- `sync`: [See SyncConfig](syncConfig.md)
3734

35+
## JavaScript vs VTL
36+
37+
When `code` is specified, the JavaScript runtime is used. When `request` and/or `response` are specified, the VTL runtime is used.
38+
39+
If neither are specified, by default, the resolver is a PIPELINE JavaScript resolver, and the following minimalistic resolver handler is used.
40+
41+
```js
42+
export function request() {
43+
return {};
44+
}
45+
46+
export function response(ctx) {
47+
return ctx.prev.result;
48+
}
49+
```
50+
51+
To use [direct lambda](https://docs.aws.amazon.com/appsync/latest/devguide/direct-lambda-reference.html), set `kind` to `UNIT` and don't specify `request` and `response` (only works with Lambda function data sources).
52+
3853
## PIPELINE resolvers
3954

4055
When `kind` is `PIPELINE`, you can specify the [pipeline function](pipeline-functions.md) names to use:
@@ -73,6 +88,19 @@ appSync:
7388
handler: 'functions/getUser.handler'
7489
```
7590

91+
## Inline function definitions
92+
93+
If a [Pipeline function](pipeline-functions.md) is only used in a single resolver, you can also define it inline in the resolver configuration.
94+
95+
```yaml
96+
appSync:
97+
resolvers:
98+
Query.user:
99+
functions:
100+
- dataSource: 'users'
101+
code: 'getUser.js'
102+
```
103+
76104
## Caching
77105

78106
```yaml

doc/upgrading-from-v1.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ custom:
3535
3636
Place your APIs into defferent stacks. Unfortunately, this WILL require **the replacement of the APIs**. You can probably use [custom domains](custom-domain.md) to workaround that, if that's an option.
3737
38+
### Defaults to PIPELINE and JavaScript resolvers
39+
40+
The new default runtime is JavaScript.
41+
42+
The new default `KIND` for resolvers is `PIPELINE`. For several reasons:
43+
44+
- The JavaScript runtime, is only supportsed with PIPELINE resolvers
45+
- It makes migrations easier later, if you need to add functions to your resolvers.
46+
47+
> 💡 To simulate a UNIT resolver, use a PIPELINE with only one function.
48+
49+
```yml
50+
resolvers:
51+
Query.getPost:
52+
functions:
53+
- dataSource: posts
54+
code: resolvers/getPost.js
55+
```
56+
57+
### No more defaults for resolver handler paths.
58+
59+
In `v1`, if you did not specify a path to your mapping templates, a default based on the type, field or function name was used. (e.g. `Query.getPost.request.vtl`).
60+
61+
To avoid unexpected behaviours, you are now required to explicitely specify the path to your resolver handlers. i.e. use `code` for Pipeline JS resolvers or `request`/`response` for VTL.
62+
63+
There is also no more `mappingTemplatesLocation` option. Paths must be relative to the `serverless.yml`. This aligns more with how Serverless Framework handles Lambda function handlers' paths.
64+
3865
### Graphiql "playground"
3966

4067
The `graphql-playground` command which started a graphiql server pointing to the AppSync API has been removed.

0 commit comments

Comments
 (0)