@@ -2,34 +2,12 @@ import { Construct } from 'constructs';
2
2
import { AmplifyData } from '@aws-amplify/data-construct' ;
3
3
import { CfnFunctionConfiguration , CfnResolver } from 'aws-cdk-lib/aws-appsync' ;
4
4
import { JsResolver } from '@aws-amplify/data-schema-types' ;
5
- import { resolve } from 'path' ;
6
- import { fileURLToPath } from 'node:url' ;
7
5
import { Asset } from 'aws-cdk-lib/aws-s3-assets' ;
8
6
import { resolveEntryPath } from './resolve_entry_path.js' ;
9
7
10
8
const APPSYNC_PIPELINE_RESOLVER = 'PIPELINE' ;
11
9
const APPSYNC_JS_RUNTIME_NAME = 'APPSYNC_JS' ;
12
10
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
- } ;
33
11
34
12
/**
35
13
* Converts JS Resolver definition emitted by data-schema into AppSync pipeline
@@ -44,8 +22,6 @@ export const convertJsResolverDefinition = (
44
22
return ;
45
23
}
46
24
47
- const jsResolverTemplateAsset = defaultJsResolverAsset ( scope ) ;
48
-
49
25
for ( const resolver of jsResolvers ) {
50
26
const functions : string [ ] = resolver . handlers . map ( ( handler , idx ) => {
51
27
const fnName = `Fn_${ resolver . typeName } _${ resolver . fieldName } _${ idx + 1 } ` ;
@@ -71,12 +47,38 @@ export const convertJsResolverDefinition = (
71
47
72
48
const resolverName = `Resolver_${ resolver . typeName } _${ resolver . fieldName } ` ;
73
49
50
+ const amplifyEnvironmentName =
51
+ scope . node . tryGetContext ( 'amplifyEnvironmentName' ) ?? 'NONE' ;
74
52
new CfnResolver ( scope , resolverName , {
75
53
apiId : amplifyApi . apiId ,
76
54
fieldName : resolver . fieldName ,
77
55
typeName : resolver . typeName ,
78
56
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
+ ` ,
80
82
runtime : {
81
83
name : APPSYNC_JS_RUNTIME_NAME ,
82
84
runtimeVersion : APPSYNC_JS_RUNTIME_VERSION ,
0 commit comments