-
Notifications
You must be signed in to change notification settings - Fork 4k
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: javascript resolver support #22921
Comments
We're bound by the CloudFormation API which requires a template within the CloudFormation template, or requires the template to reside in S3. I'm not sure how exactly we'd be able to implement this feature, or how this would be useful given the CloudFormation requirements. Could you explain exactly how this would be useful and how we might be able to accomplish this feature? |
Not really sure how cloudformation actually works but this would be the absolute most useful feature for appsync. VTL is really annoying to work with compared to javascript |
We on the AppSync team would be happy to help push this PR through based on our new release today 😊 https://aws.amazon.com/blogs/aws/aws-appsync-graphql-apis-supports-javascript-resolvers/ |
According to https://github.com/aws/aws-cdk/blob/04baba88b4e27e19b7d0ab237ff641043d089edc/packages/%40aws-cdk/cfnspec/spec-source/specification/000_cfn/000_official/000_AWS_AppSync.json which is currently being merged, there's |
Also for |
👍 strongly interested in this support at our startup already heavily using AppSync w/ CDK. |
@piotrmoszkowicz I have a CDK repo up (can finally make public 😅 ) that uses the L1 construct in conjunction with the The main points to call out are when I create a function, add the overrides and then create create the pipeline, then and those overrides. Would you be interested in making this contribution to the L2 alpha construct? |
@mtliendo Yes I will be interested! Could you pass me possible values for runtime (both name and version)? As CF docs are not yet published I can't look it up 😅 |
'1.0.0' for the version. It's represented as a string and should stay a string. Also, as seen in the repo, JS files have to be passed as string, so I'm using You're awesome, thanks so much for helping to bring this in and please let me know how I can help! |
@mtliendo So we just keep Runtime Name as ENUM, how about VTL? Because I would love to have statically typed solution, which basically tells you to specify runtime when you specify code and same approach for VTL 😊 I guess for VTLs nothing have change, yes? |
You got it :) For VTL the process is the same. JS resolver support is opt-in by specifying a JS file to the But your right, it would be nice to get type checking on that 🤔 I could be wrong, but this may actually be a good use case for the |
Cloudformation docs are updated :) Im told an automated PR should be available in less than a week to update the L1 construct. |
Awesome, will check that! |
Any chance to get this update? L1 is just fine for me. |
I hope to work on that during this weekend :) |
L1 support is landed and myself and @mtliendo are working on an implementation for the L2s. PRs also welcome if anyone gets to it in the meantime. |
Firstly, thank you for this implementation of this great new improvement of Appsync. Can anyone give some examples for L1 in TS code for adding a resolver? |
@mattiLeBlanc keep in mind that it’s still possible to use the L2 + the escape hatches: https://github.com/mtliendo/reinvent-chat/tree/main/lib/graphql |
@mtliendo Wonderful! Thank you so much. |
@mtliendo So just to be clear, is this production ready or should I rely on VTL resolvers for now and wait for this to mature a bit more? |
JS resolvers shipped production ready with no loss in functionality or increase in latency 🙂 the L1 construct is updated however many folks use the L2 construct ( rightfully so), and that’s what this weeks PR will be targeting. |
Adds support for resolvers using the APPSYNC_JS runtime. Adds new props for specifying `Code` and `Runtime` on both `Resolver` and `Function` constructs. Adds `Code` class with asset support for local files and inline code. Adds integ test covering basic JS function/resolver usage. Fix: #22921
|
@mtliendo Hi, thanks for you hard work! |
@mattiLeBlanc L2s are higher level than L1s. L1s are direct 1:1 representations of the underlying cloudformation resources. Escape hatches are a mechanism for usually used to manipulate the L1 constructs that are composed within an L2. Here are some docs explaining the relevant concepts: L2s are generally recommended but they can be opinionated so if you disagree with those built in opinions, L1s can sometimes be better for your use case. In the case of AppSync though the L2s don't really layer in too much beyond just convenience, IE bundling your resolver code etc. |
Following on from a great article from @bboure about a possible way to share common local functions across multiple JavaScript resolvers using esbuild, I was wondering how feasible it is to do with a CDK deployed Javascript Resolver? It would be fantastic to support this, as it's really the main missing piece I am facing. I'm currently copying and pasting common functions between resolvers as I'm not sure how else to automate running Given Lambda functions ( Update: I see #24548 has been created already which looks to have raised this question already. |
Thanks @danrivett for the mention and reference. TL;DR; you can bundle the code yourself (That's an example with TypeScript, but it should work just fine with JS too) |
That's fantastic @bboure, thank you so much for pointing me to your example, that is really helpful. I'll give it a go later today and also look forward to your blog in the future. We largely use TypeScript over JavaScript, but I reverted to JavaScript for our AppSync JavaScript resolvers as I wasn't clear how best to support transpiling TS to JS via our CDK-managed stack. Your solution looks really simple. I see it uses That likely isn't a huge issue, but I wondered if there is a simple way to use In that case I hope your example will also provide inspiration to AWS to to officially support this, as it would be great to simplify and standardize that support because I think it would really super-charge AppSync JavaScript resolvers adding both local imports and TypeScript support. Perhaps they could add |
Thanks @danrivett I wasn't aware of this limit. If this becomes an issue, an alternative it to pre-build the resolvers and use esbuild src/*.ts \
--bundle \
--outdir=build \
--external:"@aws-appsync/utils" \
--format=esm
--target=es2020 then point fromAsset to the code: Code.fromAsset('build/resolver.js'), tip: change cdk.json to "app": "esbuild src/*.ts ..... && npx ts-node --prefer-ts-exts --project=tsconfig.json ./src/index.ts ", |
any ideas why this approach (
? |
@lysachok usually this is caused by the account/region not being bootstrapped. |
@MrArnoldPalmer right, but I was trying to do that in an Amplify project so it didn't help anyway. Thank you for replying! |
This brilliant example also has an sync esbuild during CDK deployment: |
@MrArnoldPalmer I am trying to implement support for JS resolvers into my Appsync CDK script. I tried multiple things like building the TS in my dist but it becomes es6 (with arrow functions etc) and AmazonDeepdish doesn't like it.
which I hope is acceptable code, but still Deepdish doesn;t like it. Code generated by CDK Synth:
The TS source is:
Yes, I know I am using a class, but that is because I try to use the Decorator to pass in config information for the Resolver (Angular style). I filter out the Decorator and its import before I convert to es5. I must say, I like the JS support, but I find it very cumbersome;
I have to park my JS resolver implementation because it completely derailed me. I just got so excited. |
@mattiLeBlanc I also advise that you use the @aws-appsync/eslint-plugin npm package to validate your JS (see here) Finally, you can also use the evaluate-code command to test and get better feedback about errors than CloudFormation. Alternatively, GraphBolt also has a tool to evaluate code in a GUI 😛 |
Here is some code I am using to bundle AppSync functions: https://github.com/taimos/cdk-serverless/blob/main/src/constructs/graphql.ts#L279 Beware that I name the js files .jar to avoid a missing feature in CDK assets I will address here: #26106 |
Built a custom AWS CDK construct to transpile and bundle Typescript to Javascript https://github.com/sudokar/cdk-appsync-typescript-resolver |
One feature I feel is missing is being able to provide a dynamic value to the resolver from the cdk file. |
Describe the feature
Based on this RFC, it would nice to explore what a CDK supported candidate would look like.
Use Case
I'd like the experimental L2 construct to support the JS runtime for AppSync resolvers
Proposed Solution
the
datasource.createResolver()
method should accept acode
argument, a specifiedruntime
, and make themappingTemplates
optional.From this:
To this:
Other Information
No response
Acknowledgements
CDK version used
2.50.0
Environment details (OS name and version, etc.)
macOS
The text was updated successfully, but these errors were encountered: