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

feat: add api id and amplify environment name to stash #2273

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

Conversation

dpilch
Copy link
Member

@dpilch dpilch commented Nov 26, 2024

Problem

Customers are not able to use DDB batch operations in custom JS resolvers. The customer needs the DDB table name to perform batch operations. The customer can construct the table name from the GraphQL API ID and the Amplify environment name (<model-name>-<graphql-api-id>-<environment-name>).

Issue number, if available: aws-amplify/amplify-category-api#408 (comment)

Changes

Add GraphQL API ID and the Amplify environment name to resolver context stash.

Corresponding docs PR, if applicable: TODO

Validation

  • Add unit test
  • Test in sample app

Checklist

  • If this PR includes a functional change to the runtime behavior of the code, I have added or updated automated test coverage for this change.
  • If this PR requires a change to the Project Architecture README, I have included that update in this PR.
  • If this PR requires a docs update, I have linked to that docs PR above.
  • If this PR modifies E2E tests, makes changes to resource provisioning, or makes SDK calls, I have run the PR checks with the run-e2e label set.

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

Copy link

changeset-bot bot commented Nov 26, 2024

🦋 Changeset detected

Latest commit: bc9b37c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@aws-amplify/backend-data Minor
@aws-amplify/backend Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

* Pipeline resolver request handler
*/
export const request = (ctx) => {
ctx.stash.apiId = '${amplifyApi.apiId}';
Copy link
Member

Choose a reason for hiding this comment

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

I'd normally recommend we namespace all amplify variables, as in:

  • amplifyAppsyncApiId
  • amplifyEnvironmentName
  • amplifyFooBar...

However! Since we already have an output name for API ID, I wonder if we should use that:

  • awsAppsyncApiId

For parallelism, that makes the other variable something like:

  • awsAmplifyEnvironmentName

@dpilch dpilch marked this pull request as ready for review November 27, 2024 16:35
@dpilch dpilch requested a review from a team as a code owner November 27, 2024 16:35
import { Asset } from 'aws-cdk-lib/aws-s3-assets';
import { resolveEntryPath } from './resolve_entry_path.js';

const APPSYNC_PIPELINE_RESOLVER = 'PIPELINE';
const APPSYNC_JS_RUNTIME_NAME = 'APPSYNC_JS';
const APPSYNC_JS_RUNTIME_VERSION = '1.0.0';
const JS_PIPELINE_RESOLVER_HANDLER = './assets/js_resolver_handler.js';
Copy link
Member

Choose a reason for hiding this comment

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

if we're dropping this. Shall we remove unused assets?

(before you do please read other comments).

Comment on lines 57 to 81
/**
* 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)
* It's required for defining a pipeline resolver. Adds the GraphQL API ID and Amplify environment name to the context stash.
* Returns the output of the last function in the pipeline back to the client.
*
* Customer-provided handlers are added as a Functions list in `pipelineConfig.functions`
*
* Uses synth-time inline code to avoid circular dependency when adding the API ID as an environment variable.
*/
code: `
/**
* Pipeline resolver request handler
*/
export const request = (ctx) => {
ctx.stash.awsAppsyncApiId = '${amplifyApi.apiId}';
ctx.stash.awsAmplifyEnvironmentName = '${amplifyEnvironmentName}';
return {};
};
/**
* Pipeline resolver response handler
*/
export const response = (ctx) => {
return ctx.prev.result;
};
`,
Copy link
Member

Choose a reason for hiding this comment

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

Can we keep this in ts file that is under compiler checks? And have unit tests?
and fs.readSync the compiled lib/<name>.js file at runtime?

We should avoid JS/TS code in string literals. Otherwise the only way we can assert it's correct in such setup is e2e tests.

See example here:

/**
* This code concatenates the contents of the ssm resolver and invoker into a single line that can be used as the esbuild banner content
* This banner is responsible for resolving the customer's SSM parameters at runtime
*/
const bannerCode = readFileSync(ssmResolverFile, 'utf-8')
.concat(readFileSync(invokeSsmResolverFile, 'utf-8'))
.split(new RegExp(`${EOL}|\n|\r`, 'g'))
.map((line) => line.replace(/\/\/.*$/, '')) // strip out inline comments because the banner is going to be flattened into a single line
.join('');
.

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.

3 participants