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

aws-appsync: support passing dynamic values from CDK to APPSYNC_JS #26848

Closed
2 tasks
samturner3 opened this issue Aug 23, 2023 · 9 comments
Closed
2 tasks

aws-appsync: support passing dynamic values from CDK to APPSYNC_JS #26848

samturner3 opened this issue Aug 23, 2023 · 9 comments
Labels
@aws-cdk/aws-appsync Related to AWS AppSync closing-soon This issue will automatically close in 4 days unless further comments are made. effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@samturner3
Copy link

samturner3 commented Aug 23, 2023

Describe the feature

javascript resolver support was added: #22921
However for some configurations context in the cdk file is required to be passed to the APPSYNC_JS file.

Use Case

For example when using other DynamoDB operations such as, TransactionWriteItem, the table name is required. If the table name is different for each application environment there is currently no way to provide a dynamic value to the resolver. The work arounds are to write the resolver code inline in the CDK stack or provide the table name as part of the GraphQL request context.

Another example is in this aws-sample step functions example where we need to pass in stateMachineArn to the request template.

Proposed Solution

something like:

  1. Add codeArgs

cdk-stack.ts

    httpdatasource.createResolver('execute-state-machine', {
      typeName: 'Mutation',
      fieldName: 'executeSearch',
      runtime: appsync.FunctionRuntime.JS_1_0_0,
      code: appsync.Code.fromAsset(path.join(__dirname, 'appSyncResolver.js')),
      codeArgs: { stateMachineArn: stateMachine.stateMachineArn}
    })
  1. (prefered) in AssetCode options add a new option such as codeArgs (may need a better name):
code: appsync.Code.fromAsset(path.join(__dirname, 'appSyncResolver.js'), { codeArgs: { stateMachineArn: stateMachine.stateMachineArn} })

Then in the code file:

  1. (prefered) Have it come through from Context ,ctx.codeArgs

appSyncResolver.js

import { util } from '@aws-appsync/utils'

export const request = (ctx) => {
  return {
    ...
      body: JSON.stringify({
        stateMachineArn: ctx.codeArgs.stateMachineArn,
        input: {
         ...
  1. Have it come through from a new arg ,codeArgs

appSyncResolver.js

import { util } from '@aws-appsync/utils'

export const request = (ctx, codeArgs) => {
  return {
    ...
      body: JSON.stringify({
        stateMachineArn: codeArgs.stateMachineArn,
        input: {
         ...

Other Information

This is a much wanted feature as mentioned:

I am using amplify that transpiles TS files, I then reference the built file into appsync.Code.fromAsset.

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

v2 (unknown exactly as using amplify)

Environment details (OS name and version, etc.)

Mac OS 11.6

@samturner3 samturner3 added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Aug 23, 2023
@github-actions github-actions bot added the @aws-cdk/aws-appsync Related to AWS AppSync label Aug 23, 2023
@samturner3
Copy link
Author

I don't know enough to create a PR, however it may be something like;
in packages/aws-cdk-lib/aws-appsync/lib/code.ts:

// New:
export interface CodeOptions extends s3_assets.AssetOptions {
  codeArgs?: { key: string; value: string }[];
}

// Modified: 
public static fromAsset(path: string, options?: CodeOptions): AssetCode {
  return new AssetCode(path, options);
}

Not sure if extending s3_assets.AssetOptions is the right approach...

Then I have no idea how to get this into the file, ie ctx.codeArgs.

@danrivett
Copy link

I totally agree with solving this use case in a first class way.

FWIW we solved it for now via creating a factory function for creating a dynamic Pipeline Resolver that added the value to the context's stash. Later Pipeline Resolvers can then read that value to create requests using the dynamic Table name.

Here's our factory function verbatim, including all our comments and reference to what inspired our solution as they may be helpful.

This is used in a CDK construct, as the comments below explain, via Code.fromInline(createTableNameInjector(table.tableName)) where table is a CDK ITable resource managed via CDK.

// For Batch DynamoDB operations the AppSync Resolvers need access to the table name to read from
// This isn't available at runtime so we need to inject it at build time using the following Pipeline Resolver factory function
//
// Note: This returns generated string with the resolvered table name embedded and so needs to be used with Code.fromInline() rather than Code.fromAsset().
// Inspired by: https://kieron-mckenna.medium.com/appsync-resolvers-with-typescript-cdk-getting-a-dynamodb-table-name-for-transactions-d8060f438b6b

export const createTableNameInjector = (tableName: string) => `
  export function request(ctx) {
    ctx.stash.tableName = "${tableName}"
    return {}
  }
  export function response(ctx) {
    return ctx.prev.result
  }
`;

Hope that's at least helpful as a possible solution in the short term.

@sudokar
Copy link

sudokar commented Aug 23, 2023

Right now, I am using this construct https://constructs.dev/packages/cdk-appsync-typescript-resolver which transpiles TS to JS and replace strings (for example ENV or TABLE_NAME)

PS: I created the construct

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Aug 23, 2023
@pahud
Copy link
Contributor

pahud commented Aug 23, 2023

Thank you for the feature request and we welcome pull requests from the community.

@onlybakam
Copy link
Contributor

We released support for env vars in appsync: https://docs.aws.amazon.com/appsync/latest/devguide/environmental-variables.html
This PR exposes the feature in L2 construct: #29064

@ashishdhingra
Copy link
Contributor

@samturner3 Good morning. Could you please check if support for env vars in appsync: https://docs.aws.amazon.com/appsync/latest/devguide/environmental-variables.html helps resolve your use case?

Thanks,
Ashish

@ashishdhingra ashishdhingra added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 5, 2024
Copy link

github-actions bot commented Jun 7, 2024

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jun 7, 2024
@samturner3
Copy link
Author

samturner3 commented Jun 10, 2024

I am not able to test the solution as the specific use case engagement has ended, however from just reading the docs it looks like it is solved in principle, thanks! 👍

Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-appsync Related to AWS AppSync closing-soon This issue will automatically close in 4 days unless further comments are made. effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

6 participants