-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(appsync): js resolver support #23551
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import * as s3_assets from '@aws-cdk/aws-s3-assets'; | ||
| import * as cdk from '@aws-cdk/core'; | ||
| import { Construct } from 'constructs'; | ||
|
|
||
| /** | ||
| * Result of binding `Code` into a `Function`. | ||
| */ | ||
| export interface CodeConfig { | ||
| /** | ||
| * The location of the code in S3 (mutually exclusive with `inlineCode`. | ||
| * @default - code is not an s3 location | ||
| */ | ||
| readonly s3Location?: string; | ||
|
|
||
| /** | ||
| * Inline code (mutually exclusive with `s3Location`). | ||
| * @default - code is not inline code | ||
| */ | ||
| readonly inlineCode?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Represents source code for an AppSync Function or Resolver. | ||
| */ | ||
| export abstract class Code { | ||
| /** | ||
| * Loads the function code from a local disk path. | ||
| * | ||
| * @param path The path to the source code file. | ||
| */ | ||
| public static fromAsset(path: string, options?: s3_assets.AssetOptions): AssetCode { | ||
| return new AssetCode(path, options); | ||
| } | ||
|
|
||
| /** | ||
| * Inline code for AppSync function | ||
| * @returns `InlineCode` with inline code. | ||
| * @param code The actual handler code (limited to 4KiB) | ||
| */ | ||
| public static fromInline(code: string): InlineCode { | ||
| return new InlineCode(code); | ||
| } | ||
|
|
||
| /** | ||
| * Bind source code to an AppSync Function or resolver. | ||
| */ | ||
| public abstract bind(scope: Construct): CodeConfig; | ||
| } | ||
|
|
||
| /** | ||
| * Represents a local file with source code used for an AppSync Function or Resolver. | ||
| */ | ||
| export class AssetCode extends Code { | ||
| private asset?: s3_assets.Asset; | ||
|
|
||
| /** | ||
| * @param path The path to the asset file. | ||
| */ | ||
| constructor(public readonly path: string, private readonly options: s3_assets.AssetOptions = { }) { | ||
| super(); | ||
| } | ||
|
|
||
| public bind(scope: Construct): CodeConfig { | ||
| // If the same AssetCode is used multiple times, retain only the first instantiation. | ||
| if (!this.asset) { | ||
| this.asset = new s3_assets.Asset(scope, 'Code', { | ||
| path: this.path, | ||
| ...this.options, | ||
| }); | ||
| } else if (cdk.Stack.of(this.asset) !== cdk.Stack.of(scope)) { | ||
| throw new Error(`Asset is already associated with another stack '${cdk.Stack.of(this.asset).stackName}'. ` + | ||
| 'Create a new Code instance for every stack.'); | ||
| } | ||
|
|
||
| return { | ||
| s3Location: this.asset.s3ObjectUrl, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * AppSync function code from an inline string. | ||
| */ | ||
| export class InlineCode extends Code { | ||
| constructor(private code: string) { | ||
| super(); | ||
|
|
||
| if (code.length === 0) { | ||
| throw new Error('AppSync Inline code cannot be empty'); | ||
| } | ||
| } | ||
|
|
||
| public bind(_scope: Construct): CodeConfig { | ||
| return { | ||
| inlineCode: this.code, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { CfnFunctionConfiguration } from './appsync.generated'; | ||
|
|
||
| /** | ||
| * Appsync supported runtimes. Only JavaScript as of now | ||
| */ | ||
| export enum FunctionRuntimeFamily { | ||
| /** | ||
| * AppSync JavaScript runtime | ||
| */ | ||
| JS = 'APPSYNC_JS', | ||
| } | ||
|
|
||
| /** | ||
| * Utility class for specifying specific appsync runtime versions | ||
| */ | ||
| export class FunctionRuntime { | ||
| /** | ||
| * APPSYNC_JS v1.0.0 runtime | ||
| */ | ||
| public static readonly JS_1_0_0 = new FunctionRuntime(FunctionRuntimeFamily.JS, '1.0.0'); | ||
|
|
||
| /** | ||
| * The name of the runtime | ||
| */ | ||
| public readonly name: string; | ||
|
|
||
| /** | ||
| * The runtime version | ||
| */ | ||
| public readonly version: string; | ||
|
|
||
| public constructor(family: FunctionRuntimeFamily, version: string) { | ||
| this.name = family; | ||
| this.version = version; | ||
| } | ||
|
|
||
| /** | ||
| * Convert to Cfn runtime configuration property format | ||
| */ | ||
| public toProperties(): CfnFunctionConfiguration.AppSyncRuntimeProperty { | ||
MrArnoldPalmer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return { | ||
| name: this.name, | ||
| runtimeVersion: this.version, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // The before step | ||
| export function request(...args) { | ||
| console.log(args); | ||
| return {} | ||
| } | ||
|
|
||
| // The after step | ||
| export function response(ctx) { | ||
| return ctx.prev.result | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { util } from '@aws-appsync/utils' | ||
|
|
||
| export function request(ctx) { | ||
| const id = util.autoId() | ||
| const name = ctx.args.name; | ||
|
|
||
| ctx.args.input = { | ||
| id, | ||
| name, | ||
| } | ||
|
|
||
| return JSON.stringify({ | ||
MrArnoldPalmer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| version: '2018-05-29', | ||
| operation: 'PutItem', | ||
| key: { id: util.dynamodb.toDynamoDB(ctx.args.input.id) }, | ||
| attributeValues: util.dynamodb.toMapValues(ctx.args.input), | ||
| }) | ||
| } | ||
|
|
||
| export function response(ctx) { | ||
| return JSON.stringify(ctx.result) | ||
MrArnoldPalmer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
10 changes: 10 additions & 0 deletions
10
packages/@aws-cdk/aws-appsync/test/appsync.js-resolver.graphql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| type Test { | ||
| id: String! | ||
| name: String! | ||
| } | ||
| type Query { | ||
| getTests: [Test]! | ||
| } | ||
| type Mutation { | ||
| addTest(name: String!): Test | ||
| } |
19 changes: 19 additions & 0 deletions
19
...eg.js-resolver.js.snapshot/JsResolverIntegTestDefaultTestDeployAssert57AD8D20.assets.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "version": "22.0.0", | ||
| "files": { | ||
| "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { | ||
| "source": { | ||
| "path": "JsResolverIntegTestDefaultTestDeployAssert57AD8D20.template.json", | ||
| "packaging": "file" | ||
| }, | ||
| "destinations": { | ||
| "current_account-current_region": { | ||
| "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", | ||
| "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", | ||
| "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "dockerImages": {} | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.