Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: allow JS resolvers with S3 assets #2890

Open
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

atierian
Copy link
Member

@atierian atierian commented Sep 19, 2024

Important

No snapshots were harmed in the making of this PR.
No behavioral changes are included in this PR. This is prerequisite work to allow S3 asset JS resolver functions.

This is followup work to #2812.

Description of changes

  • Adds a few types to represent VTL and JS runtime mapping templates (reference).
export type VTLRuntimeTemplate = { requestMappingTemplate: MappingTemplateProvider; responseMappingTemplate: MappingTemplateProvider };
export type JSRuntimeTemplate = { codeMappingTemplate: MappingTemplateProvider };
export type FunctionRuntimeTemplate = VTLRuntimeTemplate | JSRuntimeTemplate;
  • Removes addToSlot from exported TransformerResolverProvider interface in favor of addVtlFunctionToSlot and addJsFunctionToSlot (reference)

  • Adds S3MappingJSResolverFunctionCode class for JS runtime specific S3MappingTemplateProvider (reference)

  • Adds MappingTemplate.s3MappingFunctionCodeFromString function to allow creation of (S3) asset based JS runtime resolver functions. (reference)

  • Updates TransformerResolver constructor and internal functions to accept new FunctionRuntimeTemplate type.

  • Updates callsites in various transformers to new addVtlFunctionToSlot, addJsFunctionToSlot functions See commits for individual changes made to each transformer.

  • Updates conversation and generation transformers to use JSRuntimeTemplate.

Note

I recommend conducting a very surface level review of the conversation and generation changes. This PR is a prerequisite to two follow up PRs (for conversation and generation respectively) that migrate to using S3 asset based resolver functions. The majority of the conversation and generation transformer specific changes in this PR are again changed in these subsequent PRs. This was done to isolate the changes in the PR as much as possible, while not breaking the conversation and generation transformers.

CDK / CloudFormation Parameters Changed

N/A

Issue #, if available

N/A

Description of how you validated changes

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)
  • New AWS SDK calls or CloudFormation actions have been added to relevant test and service IAM policies
  • Any CDK or CloudFormation parameter changes are called out explicitly

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@atierian atierian force-pushed the js-resolver-add-to-slot-overload branch from 3a00ab9 to 1799b63 Compare September 19, 2024 20:27
@atierian atierian marked this pull request as ready for review September 20, 2024 16:09
@atierian atierian requested review from a team as code owners September 20, 2024 16:09
@@ -431,11 +477,19 @@ export class TransformerResolver implements TransformerResolverProvider {
`
: '$util.toJson($ctx.prev.result)';

const initResolverMappingTemplate: FunctionRuntimeTemplate = isJsResolverFnRuntime(this.runtime)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function implementation could benefit from splitting things out in a cleaner way. A part of me feels bad for tacking on more runtime specific conditional checks here. But a bigger part wanted to keep the scope of this (already large) PR to a minimum.

throw new Error('Runtime provided with codeMappingTemplate is not a JavaScript runtime');
}
const codeTemplateLocation = codeMappingTemplate.bind(scope, api.assetProvider);
return codeMappingTemplate.type === MappingTemplateType.INLINE
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, the instanceof InlineTemplate check conducted for VTL resolvers below isn't a valid conditional for determining if a mapping template is inline or not. I'm not comfortable changing the behavior for VTL resolvers because I assume downstream things are depending on this behavior. But JS resolvers are a new concept here, so this is doing the "correct" check. Hence the discrepancy in conditionals between JS and VTL mapping templates.

dataSourceName?: string,
pipelineConfig?: string[],
scope?: Construct,
runtime?: CfnFunctionConfiguration.AppSyncRuntimeProperty,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're disambiguating the runtime by introducing new runtime specific methods, do we still need this argument?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, thanks. I removed them from the TransformHostProvider functions already, but missed them here. Addressed in 27f4ea1

@@ -187,6 +230,28 @@ export class DefaultTransformHost implements TransformHostProvider {
pipelineConfig?: string[],
scope?: Construct,
runtime?: CfnFunctionConfiguration.AppSyncRuntimeProperty,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here: should we let this method handle the runtime setting internally?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 27f4ea1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants