Skip to content

Commit

Permalink
fix: datastore logical id (#8761)
Browse files Browse the repository at this point in the history
  • Loading branch information
SwaySway authored Nov 10, 2021
1 parent f1c1ee2 commit e86cbb9
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13656,3 +13656,51 @@ $util.qr($update.put(\\"expression\\", \\"$expression\\"))
$util.toJson($UpdateItem)
## [End] Mutation Update resolver. **"
`;

exports[`ModelTransformer: the datastore table should be configured 1`] = `
"## [Start] Sync Request template. **
#if( !$util.isNullOrEmpty($ctx.stash.authFilter) )
#set( $filter = $ctx.stash.authFilter )
#if( !$util.isNullOrEmpty($ctx.args.filter) )
#set( $filter = {
\\"and\\": [$filter, $ctx.args.filter]
} )
#end
#else
#if( !$util.isNullOrEmpty($ctx.args.filter) )
#set( $filter = $ctx.args.filter )
#end
#end
#if( !$util.isNullOrEmpty($filter) )
#set( $filterExpression = $util.parseJson($util.transform.toDynamoDBFilterExpression($filter)) )
#if( !$util.isNullOrBlank($filterExpression.expression) )
#if( $filterEpression.expressionValues.size() == 0 )
$util.qr($filterEpression.remove(\\"expressionValues\\"))
#end
#set( $filter = $filterExpression )
#end
#end
{
\\"version\\": \\"2018-05-29\\",
\\"operation\\": \\"Sync\\",
\\"filter\\": #if( $filter )
$util.toJson($filter)
#else
null
#end,
\\"limit\\": $util.defaultIfNull($ctx.args.limit, 100),
\\"lastSync\\": $util.toJson($util.defaultIfNull($ctx.args.lastSync, null)),
\\"nextToken\\": $util.toJson($util.defaultIfNull($ctx.args.nextToken, null))
}
## [End] Sync Request template. **"
`;

exports[`ModelTransformer: the datastore table should be configured 2`] = `
"## [Start] ResponseTemplate. **
#if( $ctx.error )
$util.error($ctx.error.message, $ctx.error.type, $ctx.result)
#else
$util.toJson($ctx.result)
#end
## [End] ResponseTemplate. **"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -1089,4 +1089,81 @@ describe('ModelTransformer: ', () => {

expectFieldsOnInputType(updateTodoInput!, ['name']);
});
it('the datastore table should be configured', () => {
const validSchema = `
type Todo @model {
name: String
}`;

const transformer = new GraphQLTransform({
transformConfig: {
ResolverConfig: {
project: {
ConflictDetection: 'VERSION',
ConflictHandler: ConflictHandlerType.AUTOMERGE,
},
},
},
sandboxModeEnabled: true,
transformers: [new ModelTransformer()],
});
const out = transformer.transform(validSchema);
expect(out).toBeDefined();
const schema = parse(out.schema);
validateModelSchema(schema);
// sync operation
const queryObject = getObjectType(schema, 'Query');
expectFields(queryObject!, ['syncTodos']);
// sync resolvers
expect(out.pipelineFunctions['Query.syncTodos.req.vtl']).toMatchSnapshot();
expect(out.pipelineFunctions['Query.syncTodos.res.vtl']).toMatchSnapshot();
// ds table
cdkExpect(out.rootStack).to(
haveResource('AWS::DynamoDB::Table', {
KeySchema: [
{
AttributeName: 'ds_pk',
KeyType: 'HASH',
},
{
AttributeName: 'ds_sk',
KeyType: 'RANGE',
},
],
AttributeDefinitions: [
{
AttributeName: 'ds_pk',
AttributeType: 'S',
},
{
AttributeName: 'ds_sk',
AttributeType: 'S',
},
],
BillingMode: 'PAY_PER_REQUEST',
StreamSpecification: {
StreamViewType: 'NEW_AND_OLD_IMAGES',
},
TableName: {
'Fn::Join': [
'',
[
'AmplifyDataStore-',
{
'Fn::GetAtt': ['GraphQLAPI', 'ApiId'],
},
'-',
{
Ref: 'env',
},
],
],
},
TimeToLiveSpecification: {
AttributeName: '_ttl',
Enabled: true,
},
}),
);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AttributeType, BillingMode, StreamViewType, Table, TableEncryption } from '@aws-cdk/aws-dynamodb';
import { AttributeType, BillingMode, StreamViewType, Table } from '@aws-cdk/aws-dynamodb';
import * as cdk from '@aws-cdk/core';
import * as iam from '@aws-cdk/aws-iam';
import { ResourceConstants, SyncResourceIDs } from 'graphql-transformer-common';
Expand All @@ -19,7 +19,7 @@ export function createSyncTable(context: TransformerContext) {
const stack = context.stackManager.getStackFor(SyncResourceIDs.syncTableName);
const tableName = context.resourceHelper.generateResourceName(SyncResourceIDs.syncTableName);
// eslint-disable-next-line no-new
new Table(stack, SyncResourceIDs.syncTableName, {
new Table(stack, SyncResourceIDs.syncDataSourceID, {
tableName,
partitionKey: {
name: SyncResourceIDs.syncPrimaryKey,
Expand All @@ -30,7 +30,6 @@ export function createSyncTable(context: TransformerContext) {
type: AttributeType.STRING,
},
stream: StreamViewType.NEW_AND_OLD_IMAGES,
encryption: TableEncryption.DEFAULT,
removalPolicy: cdk.RemovalPolicy.DESTROY,
billingMode: BillingMode.PAY_PER_REQUEST,
timeToLiveAttribute: '_ttl',
Expand Down

0 comments on commit e86cbb9

Please sign in to comment.