Skip to content

Commit

Permalink
feat: migrate index and model transformers to CDK v2 (#860)
Browse files Browse the repository at this point in the history
* feat: migrate index and model transformers to CDK v2

* feat: migrate index and model transformers to CDK v2 - undo lint

* feat: migrate index and model transformers to CDK v2 - remove unused import

* feat: migrate index and model transformers to CDK v2 - bump version in e2e tests.

* feat: migrate index and model transformers to CDK v2 - pr feedback.
  • Loading branch information
sobolk authored Oct 10, 2022
1 parent b3cb9ae commit 886ab6c
Show file tree
Hide file tree
Showing 43 changed files with 332 additions and 251 deletions.
2 changes: 1 addition & 1 deletion packages/amplify-e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"clean-stale-test-buckets": "ts-node ./src/cleanup-stale-test-buckets.ts"
},
"dependencies": {
"@aws-amplify/graphql-transformer-core": "0.17.12",
"@aws-amplify/graphql-transformer-core": "1.0.0",
"amplify-app": "^4.2.39",
"amplify-category-api-e2e-core": "4.0.8",
"aws-amplify": "^4.2.8",
Expand Down
17 changes: 7 additions & 10 deletions packages/amplify-graphql-index-transformer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aws-amplify/graphql-index-transformer",
"version": "0.14.0",
"version": "1.0.0",
"description": "Amplify GraphQL index and key transformers",
"repository": {
"type": "git",
Expand All @@ -27,19 +27,16 @@
"test": "jest"
},
"dependencies": {
"@aws-amplify/graphql-model-transformer": "0.16.0",
"@aws-amplify/graphql-transformer-core": "0.17.12",
"@aws-amplify/graphql-transformer-interfaces": "1.14.7",
"@aws-cdk/aws-appsync": "~1.124.0",
"@aws-cdk/aws-dynamodb": "~1.124.0",
"@aws-cdk/core": "~1.124.0",
"@aws-amplify/graphql-model-transformer": "1.0.0",
"@aws-amplify/graphql-transformer-core": "1.0.0",
"@aws-amplify/graphql-transformer-interfaces": "2.0.0",
"aws-cdk-lib": "^2.41.0",
"@aws-cdk/aws-appsync-alpha": "^2.41.0-alpha.0",
"constructs": "^10.0.5",
"graphql": "^14.5.8",
"graphql-mapping-template": "4.20.5",
"graphql-transformer-common": "4.24.0"
},
"devDependencies": {
"@aws-cdk/assert": "~1.124.0"
},
"peerDependencies": {
"amplify-cli-core": "^3.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Template,
} from '@aws-amplify/graphql-transformer-core';
import { FeatureFlagProvider } from '@aws-amplify/graphql-transformer-interfaces';
import { expect as cdkExpect, haveResourceLike } from '@aws-cdk/assert';
import { Template as AssertionTemplate } from 'aws-cdk-lib/assertions';
import { DocumentNode, parse } from 'graphql';
import { IndexTransformer, PrimaryKeyTransformer } from '..';
import * as resolverUtils from '../resolvers';
Expand Down Expand Up @@ -338,8 +338,8 @@ test('@index with multiple sort keys adds a query field and GSI correctly', () =
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
AssertionTemplate.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [{ AttributeName: 'id', KeyType: 'HASH' }],
AttributeDefinitions: [
{ AttributeName: 'id', AttributeType: 'S' },
Expand All @@ -366,15 +366,13 @@ test('@index with multiple sort keys adds a query field and GSI correctly', () =
},
},
],
}),
);
});

cdkExpect(stack).to(
haveResourceLike('AWS::AppSync::Resolver', {
AssertionTemplate.fromJSON(stack)
.hasResourceProperties('AWS::AppSync::Resolver', {
FieldName: 'listByEmailKindDate',
TypeName: 'Query',
}),
);
});

expect(out.resolvers).toMatchSnapshot();

Expand Down Expand Up @@ -435,8 +433,8 @@ test('@index with a single sort key adds a query field and GSI correctly', () =>
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
AssertionTemplate.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [{ AttributeName: 'id', KeyType: 'HASH' }],
AttributeDefinitions: [
{ AttributeName: 'id', AttributeType: 'S' },
Expand All @@ -452,8 +450,7 @@ test('@index with a single sort key adds a query field and GSI correctly', () =>
],
},
],
}),
);
});

expect(out.resolvers).toMatchSnapshot();

Expand Down Expand Up @@ -510,8 +507,8 @@ test('@index with no sort key field adds a query field and GSI correctly', () =>
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
AssertionTemplate.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [{ AttributeName: 'id', KeyType: 'HASH' }],
AttributeDefinitions: [
{ AttributeName: 'id', AttributeType: 'S' },
Expand All @@ -523,8 +520,7 @@ test('@index with no sort key field adds a query field and GSI correctly', () =>
KeySchema: [{ AttributeName: 'email', KeyType: 'HASH' }],
},
],
}),
);
});

expect(out.resolvers).toMatchSnapshot();

Expand Down Expand Up @@ -613,8 +609,8 @@ test('creates a primary key and a secondary index', () => {
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
AssertionTemplate.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [
{ AttributeName: 'email', KeyType: 'HASH' },
{ AttributeName: 'createdAt', KeyType: 'RANGE' },
Expand All @@ -633,8 +629,7 @@ test('creates a primary key and a secondary index', () => {
],
},
],
}),
);
});

expect(out.resolvers).toMatchSnapshot();

Expand Down Expand Up @@ -785,8 +780,8 @@ test('@index adds an LSI with secondaryKeyAsGSI FF set to false', () => {
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
AssertionTemplate.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [
{ AttributeName: 'email', KeyType: 'HASH' },
{ AttributeName: 'createdAt', KeyType: 'RANGE' },
Expand All @@ -805,8 +800,7 @@ test('@index adds an LSI with secondaryKeyAsGSI FF set to false', () => {
],
},
],
}),
);
});

const queryType = schema.definitions.find((def: any) => def.name && def.name.value === 'Query') as any;
const queryIndexField = queryType.fields.find((f: any) => f.name && f.name.value === 'testsByEmailByUpdatedAt');
Expand All @@ -832,8 +826,8 @@ test('@index adds a GSI with secondaryKeyAsGSI FF set to true', () => {
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
AssertionTemplate.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [
{ AttributeName: 'email', KeyType: 'HASH' },
{ AttributeName: 'createdAt', KeyType: 'RANGE' },
Expand All @@ -852,8 +846,7 @@ test('@index adds a GSI with secondaryKeyAsGSI FF set to true', () => {
],
},
],
}),
);
});

const queryType = schema.definitions.find((def: any) => def.name && def.name.value === 'Query') as any;
const queryIndexField = queryType.fields.find((f: any) => f.name && f.name.value === 'testsByEmailByUpdatedAt');
Expand Down Expand Up @@ -1281,16 +1274,15 @@ describe('automatic name generation', () => {
if (sortKeyName) {
keySchema.push({ AttributeName: sortKeyName, KeyType: 'RANGE' });
}
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
AssertionTemplate.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
GlobalSecondaryIndexes: [
{
IndexName: indexName,
KeySchema: keySchema,
},
],
}),
);
});
};
const expectGeneratedQueryLike = (
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { GraphQLTransform, validateModelSchema } from '@aws-amplify/graphql-transformer-core';
import { FeatureFlagProvider } from '@aws-amplify/graphql-transformer-interfaces';

import { expect as cdkExpect, haveResourceLike } from '@aws-cdk/assert';
import { Template } from 'aws-cdk-lib/assertions';
import { Kind, parse } from 'graphql';
import { PrimaryKeyTransformer } from '..';

Expand Down Expand Up @@ -308,12 +308,11 @@ test('a primary key with no sort key is properly configured', () => {
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
Template.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [{ AttributeName: 'email', KeyType: 'HASH' }],
AttributeDefinitions: [{ AttributeName: 'email', AttributeType: 'S' }],
}),
);
});

expect(out.resolvers).toMatchSnapshot();

Expand Down Expand Up @@ -361,8 +360,8 @@ test('a primary key with a single sort key field is properly configured', () =>
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
Template.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [
{ AttributeName: 'email', KeyType: 'HASH' },
{ AttributeName: 'kind', KeyType: 'RANGE' },
Expand All @@ -371,8 +370,7 @@ test('a primary key with a single sort key field is properly configured', () =>
{ AttributeName: 'email', AttributeType: 'S' },
{ AttributeName: 'kind', AttributeType: 'N' },
],
}),
);
});

expect(out.resolvers).toMatchSnapshot();

Expand Down Expand Up @@ -410,8 +408,8 @@ test('a primary key with a composite sort key is properly configured', () => {
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
Template.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [
{ AttributeName: 'email', KeyType: 'HASH' },
{ AttributeName: 'kind#other', KeyType: 'RANGE' },
Expand All @@ -420,8 +418,7 @@ test('a primary key with a composite sort key is properly configured', () => {
{ AttributeName: 'email', AttributeType: 'S' },
{ AttributeName: 'kind#other', AttributeType: 'S' },
],
}),
);
});

expect(out.resolvers).toMatchSnapshot();

Expand Down Expand Up @@ -495,8 +492,8 @@ test('enums are supported in keys', () => {
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
Template.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [
{ AttributeName: 'status', KeyType: 'HASH' },
{ AttributeName: 'lastStatus', KeyType: 'RANGE' },
Expand All @@ -505,8 +502,7 @@ test('enums are supported in keys', () => {
{ AttributeName: 'status', AttributeType: 'S' },
{ AttributeName: 'lastStatus', AttributeType: 'S' },
],
}),
);
});

expect(out.resolvers).toMatchSnapshot();

Expand Down Expand Up @@ -541,12 +537,11 @@ test('user provided id fields are not removed', () => {
const stack = out.stacks.Test;

validateModelSchema(schema);
cdkExpect(stack).to(
haveResourceLike('AWS::DynamoDB::Table', {
Template.fromJSON(stack)
.hasResourceProperties('AWS::DynamoDB::Table', {
KeySchema: [{ AttributeName: 'email', KeyType: 'HASH' }],
AttributeDefinitions: [{ AttributeName: 'email', AttributeType: 'S' }],
}),
);
});

const createInput: any = schema.definitions.find((d: any) => {
return d.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && d.name.value === 'CreateTestInput';
Expand Down
22 changes: 13 additions & 9 deletions packages/amplify-graphql-index-transformer/src/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { generateApplyDefaultsToInputTemplate } from '@aws-amplify/graphql-model-transformer';
import { MappingTemplate, GraphQLTransform, AmplifyApiGraphQlResourceStackTemplate, SyncUtils, StackManager } from '@aws-amplify/graphql-transformer-core';
import { DataSourceProvider, StackManagerProvider, TransformerContextProvider, TransformerPluginProvider, TransformerResolverProvider } from '@aws-amplify/graphql-transformer-interfaces';
import { DynamoDbDataSource } from '@aws-cdk/aws-appsync';
import { Table } from '@aws-cdk/aws-dynamodb';
import * as cdk from '@aws-cdk/core';
import {
MappingTemplate, GraphQLTransform, AmplifyApiGraphQlResourceStackTemplate, SyncUtils, StackManager,
} from '@aws-amplify/graphql-transformer-core';
import {
DataSourceProvider, StackManagerProvider, TransformerContextProvider, TransformerPluginProvider, TransformerResolverProvider,
} from '@aws-amplify/graphql-transformer-interfaces';
import { DynamoDbDataSource } from '@aws-cdk/aws-appsync-alpha';
import { Table } from 'aws-cdk-lib/aws-dynamodb';
import * as cdk from 'aws-cdk-lib';
import { Kind, ObjectTypeDefinitionNode, TypeNode } from 'graphql';
import {
and,
Expand Down Expand Up @@ -645,8 +649,8 @@ function setSyncQueryFilterSnippet(deltaSyncTableTtl: number) {
compoundExpression([
set(ref('filterArgsMap'), ref('ctx.args.filter.get("and")')),
generateDeltaTableTTLCheck(
'isLastSyncInDeltaTTLWindow',
deltaSyncTableTtl,
'isLastSyncInDeltaTTLWindow',
deltaSyncTableTtl,
'ctx.args.lastSync'
),
ifElse(
Expand Down Expand Up @@ -685,8 +689,8 @@ function setSyncQueryFilterSnippet(deltaSyncTableTtl: number) {
}

const generateDeltaTableTTLCheck = (
deltaTTLCheckRefName: string,
deltaTTLInMinutes: number,
deltaTTLCheckRefName: string,
deltaTTLInMinutes: number,
lastSyncRefName: string
): Expression => {
const deltaTTLInMilliSeconds = deltaTTLInMinutes * 60 * 1000;
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-graphql-maps-to-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"devDependencies": {
"@aws-amplify/graphql-model-transformer": "0.16.0",
"@aws-amplify/graphql-index-transformer": "0.14.0",
"@aws-amplify/graphql-relational-transformer": "0.12.0",
"@aws-amplify/graphql-searchable-transformer": "0.16.0",
"graphql": "^14.5.8",
Expand Down
Loading

0 comments on commit 886ab6c

Please sign in to comment.