Skip to content

Commit 9c6ab19

Browse files
author
Dane Pilcher
committed
fix: use compiled file
1 parent bc9b37c commit 9c6ab19

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

packages/backend-data/src/assets/js_resolver_handler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
22
* Pipeline resolver request handler
33
*/
4-
export const request = () => {
4+
export const request = (ctx: Record<string, Record<string, string>>) => {
5+
ctx.stash.awsAppsyncApiId = '${amplifyApiId}';
6+
ctx.stash.amplifyBranchName = '${amplifyEnvironmentName}';
57
return {};
68
};
79
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ void describe('convertJsResolverDefinition', () => {
193193
'Fn::Join': [
194194
'',
195195
[
196-
"\n /**\n * Pipeline resolver request handler\n */\n export const request = (ctx) => {\n ctx.stash.awsAppsyncApiId = '",
196+
"/**\n * Pipeline resolver request handler\n */\nexport const request = (ctx) => {\n ctx.stash.awsAppsyncApiId = '",
197197
{
198198
'Fn::GetAtt': [
199199
Match.stringLikeRegexp('amplifyDataGraphQLAPI.*'),
200200
'ApiId',
201201
],
202202
},
203-
"';\n ctx.stash.amplifyBranchName = 'NONE';\n return {};\n };\n /**\n * Pipeline resolver response handler\n */\n export const response = (ctx) => {\n return ctx.prev.result;\n };\n ",
203+
"';\n ctx.stash.amplifyBranchName = 'NONE';\n return {};\n};\n/**\n * Pipeline resolver response handler\n */\nexport const response = (ctx) => {\n return ctx.prev.result;\n};\n",
204204
],
205205
],
206206
},

packages/backend-data/src/convert_js_resolvers.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,40 @@ 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';
7+
import { readFileSync } from 'fs';
58
import { Asset } from 'aws-cdk-lib/aws-s3-assets';
69
import { resolveEntryPath } from './resolve_entry_path.js';
710

811
const APPSYNC_PIPELINE_RESOLVER = 'PIPELINE';
912
const APPSYNC_JS_RUNTIME_NAME = 'APPSYNC_JS';
1013
const APPSYNC_JS_RUNTIME_VERSION = '1.0.0';
14+
const JS_PIPELINE_RESOLVER_HANDLER = './assets/js_resolver_handler.js';
15+
16+
/**
17+
*
18+
* 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)
19+
* 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.
20+
*
21+
* Customer-provided handlers are added as a Functions list in `pipelineConfig.functions`
22+
*
23+
* Add Amplify API ID and environment name to the context stash for use in the customer-provided handlers.
24+
*/
25+
const defaultJsResolverCode = (
26+
amplifyApiId: string,
27+
amplifyEnvironmentName: string
28+
): string => {
29+
const resolvedTemplatePath = resolve(
30+
fileURLToPath(import.meta.url),
31+
'../../lib',
32+
JS_PIPELINE_RESOLVER_HANDLER
33+
);
34+
35+
return readFileSync(resolvedTemplatePath, 'utf-8')
36+
.replace('${amplifyApiId}', amplifyApiId)
37+
.replace('${amplifyEnvironmentName}', amplifyEnvironmentName);
38+
};
1139

1240
/**
1341
* Converts JS Resolver definition emitted by data-schema into AppSync pipeline
@@ -54,31 +82,8 @@ export const convertJsResolverDefinition = (
5482
fieldName: resolver.fieldName,
5583
typeName: resolver.typeName,
5684
kind: APPSYNC_PIPELINE_RESOLVER,
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-
* Uses synth-time inline code to avoid circular dependency when adding the API ID as an environment variable.
65-
*/
66-
code: `
67-
/**
68-
* Pipeline resolver request handler
69-
*/
70-
export const request = (ctx) => {
71-
ctx.stash.awsAppsyncApiId = '${amplifyApi.apiId}';
72-
ctx.stash.amplifyBranchName = '${amplifyEnvironmentName}';
73-
return {};
74-
};
75-
/**
76-
* Pipeline resolver response handler
77-
*/
78-
export const response = (ctx) => {
79-
return ctx.prev.result;
80-
};
81-
`,
85+
// Uses synth-time inline code to avoid circular dependency when adding the API ID as an environment variable.
86+
code: defaultJsResolverCode(amplifyApi.apiId, amplifyEnvironmentName),
8287
runtime: {
8388
name: APPSYNC_JS_RUNTIME_NAME,
8489
runtimeVersion: APPSYNC_JS_RUNTIME_VERSION,

0 commit comments

Comments
 (0)