Skip to content

Commit 2d1aa93

Browse files
author
Dane Pilcher
committed
feat: add api id and amplify environment name to stash
1 parent 273a9ba commit 2d1aa93

File tree

3 files changed

+81
-26
lines changed

3 files changed

+81
-26
lines changed

.changeset/strong-toes-sniff.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-data': minor
3+
---
4+
5+
Add GraphQL API ID and Amplify environment name to custom JS resolver stash

packages/backend-data/src/convert_js_resolvers.test.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Template } from 'aws-cdk-lib/assertions';
1+
import { Match, Template } from 'aws-cdk-lib/assertions';
22
import assert from 'node:assert';
33
import { beforeEach, describe, it } from 'node:test';
44
import { App, Duration, Stack } from 'aws-cdk-lib';
@@ -158,4 +158,52 @@ void describe('convertJsResolverDefinition', () => {
158158

159159
template.resourceCountIs('AWS::AppSync::Resolver', 1);
160160
});
161+
162+
void it('adds api id and environment name to stash', () => {
163+
const absolutePath = resolve(
164+
fileURLToPath(import.meta.url),
165+
'../../lib/assets',
166+
'js_resolver_handler.js'
167+
);
168+
169+
const schema = a.schema({
170+
customQuery: a
171+
.query()
172+
.authorization((allow) => allow.publicApiKey())
173+
.returns(a.string())
174+
.handler(
175+
a.handler.custom({
176+
entry: absolutePath,
177+
})
178+
),
179+
});
180+
const { jsFunctions } = schema.transform();
181+
convertJsResolverDefinition(stack, amplifyApi, jsFunctions);
182+
183+
const template = Template.fromStack(stack);
184+
template.hasResourceProperties('AWS::AppSync::Resolver', {
185+
Runtime: {
186+
Name: 'APPSYNC_JS',
187+
RuntimeVersion: '1.0.0',
188+
},
189+
Kind: 'PIPELINE',
190+
TypeName: 'Query',
191+
FieldName: 'customQuery',
192+
Code: {
193+
'Fn::Join': [
194+
'',
195+
[
196+
"\n /**\n * Pipeline resolver request handler\n */\n export const request = (ctx) => {\n ctx.stash.apiId = '",
197+
{
198+
'Fn::GetAtt': [
199+
Match.stringLikeRegexp('amplifyDataGraphQLAPI.*'),
200+
'ApiId',
201+
],
202+
},
203+
"';\n ctx.stash.amplifyEnvironmentName = 'NONE';\n return {};\n };\n /**\n * Pipeline resolver response handler\n */\n export const response = (ctx) => {\n return ctx.prev.result;\n };\n ",
204+
],
205+
],
206+
},
207+
});
208+
});
161209
});

packages/backend-data/src/convert_js_resolvers.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,12 @@ import { Construct } from 'constructs';
22
import { AmplifyData } from '@aws-amplify/data-construct';
33
import { CfnFunctionConfiguration, CfnResolver } from 'aws-cdk-lib/aws-appsync';
44
import { JsResolver } from '@aws-amplify/data-schema-types';
5-
import { resolve } from 'path';
6-
import { fileURLToPath } from 'node:url';
75
import { Asset } from 'aws-cdk-lib/aws-s3-assets';
86
import { resolveEntryPath } from './resolve_entry_path.js';
97

108
const APPSYNC_PIPELINE_RESOLVER = 'PIPELINE';
119
const APPSYNC_JS_RUNTIME_NAME = 'APPSYNC_JS';
1210
const APPSYNC_JS_RUNTIME_VERSION = '1.0.0';
13-
const JS_PIPELINE_RESOLVER_HANDLER = './assets/js_resolver_handler.js';
14-
15-
/**
16-
*
17-
* This returns the top-level passthrough resolver request/response handler (see: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-reference-overview-js.html#anatomy-of-a-pipeline-resolver-js)
18-
* It's required for defining a pipeline resolver. The only purpose it serves is returning the output of the last function in the pipeline back to the client.
19-
*
20-
* Customer-provided handlers are added as a Functions list in `pipelineConfig.functions`
21-
*/
22-
const defaultJsResolverAsset = (scope: Construct): Asset => {
23-
const resolvedTemplatePath = resolve(
24-
fileURLToPath(import.meta.url),
25-
'../../lib',
26-
JS_PIPELINE_RESOLVER_HANDLER
27-
);
28-
29-
return new Asset(scope, 'default_js_resolver_handler_asset', {
30-
path: resolveEntryPath(resolvedTemplatePath),
31-
});
32-
};
3311

3412
/**
3513
* Converts JS Resolver definition emitted by data-schema into AppSync pipeline
@@ -44,8 +22,6 @@ export const convertJsResolverDefinition = (
4422
return;
4523
}
4624

47-
const jsResolverTemplateAsset = defaultJsResolverAsset(scope);
48-
4925
for (const resolver of jsResolvers) {
5026
const functions: string[] = resolver.handlers.map((handler, idx) => {
5127
const fnName = `Fn_${resolver.typeName}_${resolver.fieldName}_${idx + 1}`;
@@ -71,12 +47,38 @@ export const convertJsResolverDefinition = (
7147

7248
const resolverName = `Resolver_${resolver.typeName}_${resolver.fieldName}`;
7349

50+
const amplifyEnvironmentName =
51+
scope.node.tryGetContext('amplifyEnvironmentName') ?? 'NONE';
7452
new CfnResolver(scope, resolverName, {
7553
apiId: amplifyApi.apiId,
7654
fieldName: resolver.fieldName,
7755
typeName: resolver.typeName,
7856
kind: APPSYNC_PIPELINE_RESOLVER,
79-
codeS3Location: jsResolverTemplateAsset.s3ObjectUrl,
57+
/**
58+
* The top-level passthrough resolver request/response handler (see: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-reference-overview-js.html#anatomy-of-a-pipeline-resolver-js)
59+
* It's required for defining a pipeline resolver. Adds the GraphQL API ID and Amplify environment name to the context stash.
60+
* Returns the output of the last function in the pipeline back to the client.
61+
*
62+
* Customer-provided handlers are added as a Functions list in `pipelineConfig.functions`
63+
*
64+
* Use inline code to remove circular dependency when adding the API ID.
65+
*/
66+
code: `
67+
/**
68+
* Pipeline resolver request handler
69+
*/
70+
export const request = (ctx) => {
71+
ctx.stash.apiId = '${amplifyApi.apiId}';
72+
ctx.stash.amplifyEnvironmentName = '${amplifyEnvironmentName}';
73+
return {};
74+
};
75+
/**
76+
* Pipeline resolver response handler
77+
*/
78+
export const response = (ctx) => {
79+
return ctx.prev.result;
80+
};
81+
`,
8082
runtime: {
8183
name: APPSYNC_JS_RUNTIME_NAME,
8284
runtimeVersion: APPSYNC_JS_RUNTIME_VERSION,

0 commit comments

Comments
 (0)