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(appsync): support for read consistency during DynamoDB reads #20793

Merged
merged 27 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fd94542
add consistent read for dynamoDbGetItem, dynamoDbScanTable and dynamo…
YichenQian09 Jun 22, 2022
1d04a0a
change the indent
YichenQian09 Jun 23, 2022
bc2cd79
add tests for consistentRead
YichenQian09 Jul 1, 2022
a81b745
add reference for consistentRead in README.md
YichenQian09 Jul 1, 2022
cf44a76
pass the consistentRead as boolean in mapping-template
YichenQian09 Jul 1, 2022
ce16abc
chore: add unit test
pahud Jul 7, 2022
a3a1ccc
Merge pull request #1 from pahud/pahud/read-consistency
YichenQian09 Jul 8, 2022
905339b
added united tests for read consistency
YichenQian09 Jul 9, 2022
08fe755
modified united tests for read consistency
YichenQian09 Jul 9, 2022
f761b45
delete inexistent files in main branch
YichenQian09 Jul 11, 2022
5d100bf
Merge branch 'aws-main' into consistent-read-v2
YichenQian09 Jul 11, 2022
ef9a183
update markdown for DynamoDB read consistency
YichenQian09 Jul 13, 2022
364ea79
updated the readme.md
YichenQian09 Jul 13, 2022
76c14fe
Merge branch 'main' into read-consistency
kellertk Jul 14, 2022
502ac8d
updated readme.md
YichenQian09 Jul 14, 2022
46ae6f1
updated readme.md
YichenQian09 Jul 14, 2022
78d03c3
Merge branch 'read-consistency' of https://github.com/YichenQian09/aw…
YichenQian09 Jul 14, 2022
4d23918
Merge branch 'main' into read-consistency
YichenQian09 Jul 15, 2022
ff88f79
Merge branch 'main' into read-consistency
pahud Jul 15, 2022
8e25f62
update readme.md
YichenQian09 Jul 18, 2022
3867d2a
solved conflict
YichenQian09 Jul 18, 2022
f2fc776
updated the expiration timestamp for api-key
YichenQian09 Jul 19, 2022
25b0059
Merge branch 'main' into read-consistency
YichenQian09 Jul 19, 2022
ea91ca7
Merge branch 'main' into read-consistency
YichenQian09 Jul 19, 2022
5ebe3b2
Update packages/@aws-cdk/aws-appsync/README.md
YichenQian09 Jul 21, 2022
1e24775
Merge branch 'main' into read-consistency
YichenQian09 Jul 21, 2022
669d3dd
Merge branch 'main' into read-consistency
mergify[bot] Jul 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-appsync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ const demoTable = new dynamodb.Table(this, 'DemoTable', {
const demoDS = api.addDynamoDbDataSource('demoDataSource', demoTable);

// Resolver for the Query "getDemos" that scans the DynamoDb table and returns the entire list.
// Resolver Mapping Template Reference:
// https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html
demoDS.createResolver({
typeName: 'Query',
fieldName: 'getDemos',
Expand All @@ -94,8 +96,20 @@ demoDS.createResolver({
),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});

//To enable DynamoDB read consistency with the `MappingTemplate`:
demoDS.createResolver({
typeName: 'Query',
fieldName: 'getDemosConsistent',
requestMappingTemplate: appsync.MappingTemplate.dynamoDbScanTable(true),
//requestMappingTemplate: appsync.MappingTemplate.dynamoDbGetItem('id', 'id', true),
//requestMappingTemplate: appsync.MappingTemplate.dynamoDbQuery(KeyCondition.eq('order', 'order'), 'orderIndex', true),
YichenQian09 marked this conversation as resolved.
Show resolved Hide resolved
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultList(),
});
```



### Aurora Serverless

AppSync provides a data source for executing SQL commands against Amazon Aurora
Expand Down
12 changes: 6 additions & 6 deletions packages/@aws-cdk/aws-appsync/lib/mapping-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ export abstract class MappingTemplate {
/**
* Mapping template to scan a DynamoDB table to fetch all entries
*/
public static dynamoDbScanTable(): MappingTemplate {
return this.fromString('{"version" : "2017-02-28", "operation" : "Scan"}');
public static dynamoDbScanTable(consistentRead: boolean = false): MappingTemplate {
return this.fromString(`{"version" : "2017-02-28", "operation" : "Scan", "consistentRead": ${consistentRead}}`);
}

/**
* Mapping template to query a set of items from a DynamoDB table
*
* @param cond the key condition for the query
*/
public static dynamoDbQuery(cond: KeyCondition, indexName?: string): MappingTemplate {
return this.fromString(`{"version" : "2017-02-28", "operation" : "Query", ${indexName ? `"index" : "${indexName}", ` : ''}${cond.renderTemplate()}}`);
public static dynamoDbQuery(cond: KeyCondition, indexName?: string, consistentRead: boolean = false): MappingTemplate {
return this.fromString(`{"version" : "2017-02-28", "operation" : "Query", "consistentRead": ${consistentRead}, ${indexName ? `"index" : "${indexName}", ` : ''}${cond.renderTemplate()}}`);
}

/**
Expand All @@ -55,8 +55,8 @@ export abstract class MappingTemplate {
* @param keyName the name of the hash key field
* @param idArg the name of the Query argument
*/
public static dynamoDbGetItem(keyName: string, idArg: string): MappingTemplate {
return this.fromString(`{"version": "2017-02-28", "operation": "GetItem", "key": {"${keyName}": $util.dynamodb.toDynamoDBJson($ctx.args.${idArg})}}`);
public static dynamoDbGetItem(keyName: string, idArg: string, consistentRead: boolean = false): MappingTemplate {
return this.fromString(`{"version": "2017-02-28", "operation": "GetItem", "consistentRead": ${consistentRead}, "key": {"${keyName}": $util.dynamodb.toDynamoDBJson($ctx.args.${idArg})}}`);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-appsync/test/appsync-dynamodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Template } from '@aws-cdk/assertions';
import * as db from '@aws-cdk/aws-dynamodb';
import * as cdk from '@aws-cdk/core';
import * as appsync from '../lib';
import { KeyCondition } from '../lib';

function joined(str: string): string {
return str.replace(/\s+/g, '');
Expand Down Expand Up @@ -80,6 +81,24 @@ describe('DynamoDb Data Source configuration', () => {
});

describe('DynamoDB Mapping Templates', () => {
test('read consistency option for dynamoDbScanTable should render correctly', () => {
const template = appsync.MappingTemplate.dynamoDbScanTable(true);
const rendered = joined(template.renderTemplate());
expect(rendered).toStrictEqual('{\"version\":\"2017-02-28\",\"operation\":\"Scan\",\"consistentRead\":true}');
});

test('read consistency option for dynamoDbGetItem should render correctly', () => {
const template = appsync.MappingTemplate.dynamoDbGetItem('id', 'id', true);
const rendered = joined(template.renderTemplate());
expect(rendered).toStrictEqual('{\"version\":\"2017-02-28\",\"operation\":\"GetItem\",\"consistentRead\":true,\"key\":{\"id\":$util.dynamodb.toDynamoDBJson($ctx.args.id)}}');
});

test('read consistency option for dynamoDbQuery should render correctly', () => {
const template = appsync.MappingTemplate.dynamoDbQuery(KeyCondition.eq('order', 'order'), 'orderIndex', true);
const rendered = joined(template.renderTemplate());
expect(rendered).toStrictEqual('{\"version\":\"2017-02-28\",\"operation\":\"Query\",\"consistentRead\":true,\"index\":\"orderIndex\",\"query\":{\"expression\":\"#order=:order\",\"expressionNames\":{\"#order\":\"order\"},\"expressionValues\":{\":order\":$util.dynamodb.toDynamoDBJson($ctx.args.order)}}}');
});

test('PutItem projecting all', () => {
const template = appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').is('id'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "20.0.0",
"files": {
"edb3fd1d912b400f4857c41a3ff80bcc32d180595affcd6c017f72afd5959482": {
"7eaa4499d04a6696121796bb6e27fab5b4211517a6e0523260984e33c134f84f": {
"source": {
"path": "aws-appsync-integ.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "edb3fd1d912b400f4857c41a3ff80bcc32d180595affcd6c017f72afd5959482.json",
"objectKey": "7eaa4499d04a6696121796bb6e27fab5b4211517a6e0523260984e33c134f84f.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"TypeName": "Query",
"DataSourceName": "testDataSource",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\"}",
"RequestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\", \"consistentRead\": false}",
"ResponseMappingTemplate": "$util.toJson($ctx.result.items)"
},
"DependsOn": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appsync.CfnGraphQLApi",
"fqn": "@aws-cdk/core.CfnResource",
"version": "0.0.0"
}
},
Expand All @@ -51,7 +51,7 @@
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appsync.CfnGraphQLSchema",
"fqn": "@aws-cdk/core.CfnResource",
"version": "0.0.0"
}
},
Expand All @@ -70,7 +70,7 @@
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appsync.CfnApiKey",
"fqn": "@aws-cdk/core.CfnResource",
"version": "0.0.0"
}
},
Expand Down Expand Up @@ -206,7 +206,7 @@
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appsync.CfnDataSource",
"fqn": "@aws-cdk/core.CfnResource",
"version": "0.0.0"
}
},
Expand All @@ -230,19 +230,19 @@
"typeName": "Query",
"dataSourceName": "testDataSource",
"kind": "UNIT",
"requestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\"}",
"requestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\", \"consistentRead\": false}",
"responseMappingTemplate": "$util.toJson($ctx.result.items)"
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appsync.CfnResolver",
"fqn": "@aws-cdk/core.CfnResource",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appsync.Resolver",
"version": "0.0.0"
"fqn": "constructs.Construct",
"version": "10.1.33"
}
},
"MutationaddTestResolver": {
Expand Down Expand Up @@ -270,25 +270,25 @@
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appsync.CfnResolver",
"fqn": "@aws-cdk/core.CfnResource",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appsync.Resolver",
"version": "0.0.0"
"fqn": "constructs.Construct",
"version": "10.1.33"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appsync.DynamoDbDataSource",
"version": "0.0.0"
"fqn": "constructs.Construct",
"version": "10.1.33"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appsync.GraphqlApi",
"fqn": "@aws-cdk/core.Resource",
"version": "0.0.0"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
}
}
},
"4d9e950f40fbc075b913a37030dfd9c31de11a2230d2c5c7e45b8b9730faaa3f": {
"c06c582f69e71eae955ab50693deb45f0836b7837ef3184a737f87c4bf7952a4": {
"source": {
"path": "aws-appsync-integ.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "4d9e950f40fbc075b913a37030dfd9c31de11a2230d2c5c7e45b8b9730faaa3f.json",
"objectKey": "c06c582f69e71eae955ab50693deb45f0836b7837ef3184a737f87c4bf7952a4.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
"TypeName": "Query",
"DataSourceName": "testDataSource",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\": \"2017-02-28\", \"operation\": \"GetItem\", \"key\": {\"id\": $util.dynamodb.toDynamoDBJson($ctx.args.id)}}",
"RequestMappingTemplate": "{\"version\": \"2017-02-28\", \"operation\": \"GetItem\", \"consistentRead\": false, \"key\": {\"id\": $util.dynamodb.toDynamoDBJson($ctx.args.id)}}",
"ResponseMappingTemplate": "$util.toJson($ctx.result)"
},
"DependsOn": [
Expand All @@ -187,7 +187,7 @@
"TypeName": "Query",
"DataSourceName": "testDataSource",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\"}",
"RequestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\", \"consistentRead\": false}",
"ResponseMappingTemplate": "$util.toJson($ctx.result.items)"
},
"DependsOn": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.0.9"
"version": "10.1.33"
}
},
"aws-appsync-integ": {
Expand Down Expand Up @@ -273,7 +273,7 @@
"typeName": "Query",
"dataSourceName": "testDataSource",
"kind": "UNIT",
"requestMappingTemplate": "{\"version\": \"2017-02-28\", \"operation\": \"GetItem\", \"key\": {\"id\": $util.dynamodb.toDynamoDBJson($ctx.args.id)}}",
"requestMappingTemplate": "{\"version\": \"2017-02-28\", \"operation\": \"GetItem\", \"consistentRead\": false, \"key\": {\"id\": $util.dynamodb.toDynamoDBJson($ctx.args.id)}}",
"responseMappingTemplate": "$util.toJson($ctx.result)"
}
},
Expand Down Expand Up @@ -308,7 +308,7 @@
"typeName": "Query",
"dataSourceName": "testDataSource",
"kind": "UNIT",
"requestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\"}",
"requestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\", \"consistentRead\": false}",
"responseMappingTemplate": "$util.toJson($ctx.result.items)"
}
},
Expand Down Expand Up @@ -714,13 +714,13 @@
},
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.0.9"
"version": "10.1.33"
}
}
},
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.0.9"
"version": "10.1.33"
}
},
"testFail": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"17.0.0"}
{"version":"20.0.0"}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "17.0.0",
"version": "20.0.0",
"files": {
"13205a5206cf49ce1c9674807fa5b2c3315de95159c5c1c969664271edd96507": {
"56d1a7b725a78aa83ffacc3ba1d3ae4b4c2fa61c3caff19dafd5f77c1e0333c3": {
"source": {
"path": "code-first-schema.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "13205a5206cf49ce1c9674807fa5b2c3315de95159c5c1c969664271edd96507.json",
"objectKey": "56d1a7b725a78aa83ffacc3ba1d3ae4b4c2fa61c3caff19dafd5f77c1e0333c3.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"TypeName": "Query",
"DataSourceName": "planets",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\"}",
"RequestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\", \"consistentRead\": false}",
"ResponseMappingTemplate": "$util.toJson($ctx.result.items)"
},
"DependsOn": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "17.0.0",
"version": "20.0.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.0.9"
"version": "10.1.33"
}
},
"code-first-schema": {
Expand Down Expand Up @@ -236,7 +236,7 @@
"typeName": "Query",
"dataSourceName": "planets",
"kind": "UNIT",
"requestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\"}",
"requestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\", \"consistentRead\": false}",
"responseMappingTemplate": "$util.toJson($ctx.result.items)"
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "17.0.0",
"version": "20.0.0",
"files": {
"7b4937a061e73c53a8d1ae8eeb6c778db65cfc02df7a34947b816bcd1332f9c9": {
"410a11ff94f45720614bec412ef8a8f4aa68e8f98e181bbd4b98fb47cbb76284": {
"source": {
"path": "aws-appsync-integ.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "7b4937a061e73c53a8d1ae8eeb6c778db65cfc02df7a34947b816bcd1332f9c9.json",
"objectKey": "410a11ff94f45720614bec412ef8a8f4aa68e8f98e181bbd4b98fb47cbb76284.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Loading