@aws-amplify/graphql-model-transformer 0.9.3-beta.0 → 0.9.5-apiext5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -3
- package/lib/definitions.d.ts +1 -0
- package/lib/definitions.d.ts.map +1 -1
- package/lib/definitions.js +2 -1
- package/lib/definitions.js.map +1 -1
- package/lib/graphql-model-transformer.d.ts +2 -1
- package/lib/graphql-model-transformer.d.ts.map +1 -1
- package/lib/graphql-model-transformer.js +137 -85
- package/lib/graphql-model-transformer.js.map +1 -1
- package/lib/graphql-types/common.d.ts +7 -3
- package/lib/graphql-types/common.d.ts.map +1 -1
- package/lib/graphql-types/common.js +86 -22
- package/lib/graphql-types/common.js.map +1 -1
- package/lib/graphql-types/mutation.js +14 -14
- package/lib/graphql-types/mutation.js.map +1 -1
- package/lib/graphql-types/query.js +6 -6
- package/lib/graphql-types/query.js.map +1 -1
- package/lib/resolvers/common.d.ts +1 -1
- package/lib/resolvers/common.d.ts.map +1 -1
- package/lib/resolvers/common.js +20 -21
- package/lib/resolvers/common.js.map +1 -1
- package/lib/resolvers/mutation.js +128 -128
- package/lib/resolvers/mutation.js.map +1 -1
- package/lib/resolvers/query.js +69 -69
- package/lib/resolvers/query.js.map +1 -1
- package/lib/resolvers/subscriptions.js +4 -4
- package/lib/resolvers/subscriptions.js.map +1 -1
- package/package.json +10 -7
- package/src/__tests__/__snapshots__/model-transformer-override.test.ts.snap +3009 -0
- package/src/__tests__/__snapshots__/model-transformer.test.ts.snap +3 -3
- package/src/__tests__/model-transformer-override.test.ts +41 -0
- package/src/__tests__/model-transformer.test.ts +132 -37
- package/src/__tests__/overrides/build/override.js +8 -0
- package/src/definitions.ts +2 -0
- package/src/graphql-model-transformer.ts +81 -9
- package/src/graphql-types/common.ts +98 -2
- package/src/graphql-types/mutation.ts +1 -1
- package/src/graphql-types/query.ts +1 -1
- package/src/resolvers/common.ts +1 -3
- package/tsconfig.tsbuildinfo +1 -1
- package/lib/wrappers/object-definition-wrapper.d.ts +0 -77
- package/lib/wrappers/object-definition-wrapper.d.ts.map +0 -1
- package/lib/wrappers/object-definition-wrapper.js +0 -354
- package/lib/wrappers/object-definition-wrapper.js.map +0 -1
- package/src/wrappers/object-definition-wrapper.ts +0 -447
|
@@ -13303,7 +13303,7 @@ $util.toJson($UpdateItem)
|
|
|
13303
13303
|
## [End] Mutation Update resolver. **"
|
|
13304
13304
|
`;
|
|
13305
13305
|
|
|
13306
|
-
exports[`ModelTransformer: should support timestamp parameters when generating
|
|
13306
|
+
exports[`ModelTransformer: should support timestamp parameters when generating resolvers and output schema 1`] = `
|
|
13307
13307
|
"
|
|
13308
13308
|
type Post {
|
|
13309
13309
|
id: ID!
|
|
@@ -13457,7 +13457,7 @@ type Subscription {
|
|
|
13457
13457
|
"
|
|
13458
13458
|
`;
|
|
13459
13459
|
|
|
13460
|
-
exports[`ModelTransformer: should support timestamp parameters when generating
|
|
13460
|
+
exports[`ModelTransformer: should support timestamp parameters when generating resolvers and output schema 2`] = `
|
|
13461
13461
|
"## [Start] Create Request template. **
|
|
13462
13462
|
## Set the default values to put request **
|
|
13463
13463
|
#set( $mergedValues = $util.defaultIfNull($ctx.stash.defaultValues, {}) )
|
|
@@ -13525,7 +13525,7 @@ $util.toJson($PutObject)
|
|
|
13525
13525
|
## [End] Create Request template. **"
|
|
13526
13526
|
`;
|
|
13527
13527
|
|
|
13528
|
-
exports[`ModelTransformer: should support timestamp parameters when generating
|
|
13528
|
+
exports[`ModelTransformer: should support timestamp parameters when generating resolvers and output schema 3`] = `
|
|
13529
13529
|
"## [Start] Mutation Update resolver. **
|
|
13530
13530
|
## Set the default values to put request **
|
|
13531
13531
|
#set( $mergedValues = $util.defaultIfNull($ctx.stash.defaultValues, {}) )
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
|
|
2
|
+
import { GraphQLTransform } from '@aws-amplify/graphql-transformer-core';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
const featureFlags = {
|
|
6
|
+
getBoolean: jest.fn(),
|
|
7
|
+
getNumber: jest.fn(),
|
|
8
|
+
getObject: jest.fn(),
|
|
9
|
+
getString: jest.fn(),
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
describe('ModelTransformer: ', () => {
|
|
13
|
+
it('should override model objects when given override config', () => {
|
|
14
|
+
const validSchema = `
|
|
15
|
+
type Post @model {
|
|
16
|
+
id: ID!
|
|
17
|
+
comments: [Comment]
|
|
18
|
+
}
|
|
19
|
+
type Comment @model{
|
|
20
|
+
id: String!
|
|
21
|
+
text: String!
|
|
22
|
+
}
|
|
23
|
+
`;
|
|
24
|
+
const transformer = new GraphQLTransform({
|
|
25
|
+
transformers: [new ModelTransformer()],
|
|
26
|
+
overrideConfig: {
|
|
27
|
+
overrideDir: path.join(__dirname, 'overrides'),
|
|
28
|
+
overrideFlag: true,
|
|
29
|
+
resourceName: 'myResource',
|
|
30
|
+
},
|
|
31
|
+
featureFlags,
|
|
32
|
+
});
|
|
33
|
+
const out = transformer.transform(validSchema);
|
|
34
|
+
expect(out).toBeDefined();
|
|
35
|
+
const postStack = out.stacks.Post;
|
|
36
|
+
const commentStack = out.stacks.Comment;
|
|
37
|
+
|
|
38
|
+
expect(postStack).toMatchSnapshot();
|
|
39
|
+
expect(commentStack).toMatchSnapshot();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -411,7 +411,7 @@ describe('ModelTransformer: ', () => {
|
|
|
411
411
|
expect(defaultIdField).toBeDefined();
|
|
412
412
|
expect(getBaseType(defaultIdField.type)).toEqual('Int');
|
|
413
413
|
// It should not add default value for ctx.arg.id as id is of type Int
|
|
414
|
-
expect(result.
|
|
414
|
+
expect(result.resolvers['Mutation.createPost.req.vtl']).toMatchSnapshot();
|
|
415
415
|
});
|
|
416
416
|
|
|
417
417
|
it('should generate only create mutation', () => {
|
|
@@ -670,7 +670,7 @@ describe('ModelTransformer: ', () => {
|
|
|
670
670
|
validateModelSchema(schema);
|
|
671
671
|
});
|
|
672
672
|
|
|
673
|
-
it('should support timestamp parameters when generating
|
|
673
|
+
it('should support timestamp parameters when generating resolvers and output schema', () => {
|
|
674
674
|
const validSchema = `
|
|
675
675
|
type Post @model(timestamps: { createdAt: "createdOn", updatedAt: "updatedOn"}) {
|
|
676
676
|
id: ID!
|
|
@@ -688,8 +688,8 @@ describe('ModelTransformer: ', () => {
|
|
|
688
688
|
const schema = parse(result.schema);
|
|
689
689
|
validateModelSchema(schema);
|
|
690
690
|
|
|
691
|
-
expect(result.
|
|
692
|
-
expect(result.
|
|
691
|
+
expect(result.resolvers['Mutation.createPost.req.vtl']).toMatchSnapshot();
|
|
692
|
+
expect(result.resolvers['Mutation.updatePost.req.vtl']).toMatchSnapshot();
|
|
693
693
|
});
|
|
694
694
|
|
|
695
695
|
it('should not to auto generate createdAt and updatedAt when the type in schema is not AWSDateTime', () => {
|
|
@@ -712,8 +712,8 @@ describe('ModelTransformer: ', () => {
|
|
|
712
712
|
const schema = parse(result.schema);
|
|
713
713
|
validateModelSchema(schema);
|
|
714
714
|
|
|
715
|
-
expect(result.
|
|
716
|
-
expect(result.
|
|
715
|
+
expect(result.resolvers['Mutation.createPost.req.vtl']).toMatchSnapshot();
|
|
716
|
+
expect(result.resolvers['Mutation.updatePost.req.vtl']).toMatchSnapshot();
|
|
717
717
|
});
|
|
718
718
|
|
|
719
719
|
it('should have timestamps as nullable fields when the type makes it non-nullable', () => {
|
|
@@ -737,8 +737,8 @@ describe('ModelTransformer: ', () => {
|
|
|
737
737
|
const schema = parse(result.schema);
|
|
738
738
|
validateModelSchema(schema);
|
|
739
739
|
|
|
740
|
-
expect(result.
|
|
741
|
-
expect(result.
|
|
740
|
+
expect(result.resolvers['Mutation.createPost.req.vtl']).toMatchSnapshot();
|
|
741
|
+
expect(result.resolvers['Mutation.updatePost.req.vtl']).toMatchSnapshot();
|
|
742
742
|
});
|
|
743
743
|
|
|
744
744
|
it('should not to include createdAt and updatedAt field when timestamps is set to null', () => {
|
|
@@ -759,8 +759,8 @@ describe('ModelTransformer: ', () => {
|
|
|
759
759
|
const schema = parse(result.schema);
|
|
760
760
|
validateModelSchema(schema);
|
|
761
761
|
|
|
762
|
-
expect(result.
|
|
763
|
-
expect(result.
|
|
762
|
+
expect(result.resolvers['Mutation.createPost.req.vtl']).toMatchSnapshot();
|
|
763
|
+
expect(result.resolvers['Mutation.updatePost.req.vtl']).toMatchSnapshot();
|
|
764
764
|
});
|
|
765
765
|
|
|
766
766
|
it('should filter known input types from create and update input fields', () => {
|
|
@@ -897,10 +897,8 @@ describe('ModelTransformer: ', () => {
|
|
|
897
897
|
const transformer = new GraphQLTransform({
|
|
898
898
|
transformers: [new ModelTransformer()],
|
|
899
899
|
featureFlags,
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
project: config,
|
|
903
|
-
},
|
|
900
|
+
resolverConfig: {
|
|
901
|
+
project: config,
|
|
904
902
|
},
|
|
905
903
|
});
|
|
906
904
|
const out = transformer.transform(validSchema);
|
|
@@ -908,7 +906,7 @@ describe('ModelTransformer: ', () => {
|
|
|
908
906
|
|
|
909
907
|
const definition = out.schema;
|
|
910
908
|
expect(definition).toBeDefined();
|
|
911
|
-
expect(out.
|
|
909
|
+
expect(out.resolvers).toMatchSnapshot();
|
|
912
910
|
|
|
913
911
|
validateModelSchema(parse(definition));
|
|
914
912
|
});
|
|
@@ -934,10 +932,8 @@ describe('ModelTransformer: ', () => {
|
|
|
934
932
|
const transformer = new GraphQLTransform({
|
|
935
933
|
transformers: [new ModelTransformer()],
|
|
936
934
|
featureFlags,
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
project: config,
|
|
940
|
-
},
|
|
935
|
+
resolverConfig: {
|
|
936
|
+
project: config,
|
|
941
937
|
},
|
|
942
938
|
});
|
|
943
939
|
const out = transformer.transform(validSchema);
|
|
@@ -945,7 +941,7 @@ describe('ModelTransformer: ', () => {
|
|
|
945
941
|
|
|
946
942
|
const definition = out.schema;
|
|
947
943
|
expect(definition).toBeDefined();
|
|
948
|
-
expect(out.
|
|
944
|
+
expect(out.resolvers).toMatchSnapshot();
|
|
949
945
|
|
|
950
946
|
validateModelSchema(parse(definition));
|
|
951
947
|
});
|
|
@@ -968,10 +964,8 @@ describe('ModelTransformer: ', () => {
|
|
|
968
964
|
const transformer = new GraphQLTransform({
|
|
969
965
|
transformers: [new ModelTransformer()],
|
|
970
966
|
featureFlags,
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
project: config,
|
|
974
|
-
},
|
|
967
|
+
resolverConfig: {
|
|
968
|
+
project: config,
|
|
975
969
|
},
|
|
976
970
|
});
|
|
977
971
|
const out = transformer.transform(validSchema);
|
|
@@ -979,7 +973,7 @@ describe('ModelTransformer: ', () => {
|
|
|
979
973
|
|
|
980
974
|
const definition = out.schema;
|
|
981
975
|
expect(definition).toBeDefined();
|
|
982
|
-
expect(out.
|
|
976
|
+
expect(out.resolvers).toMatchSnapshot();
|
|
983
977
|
|
|
984
978
|
validateModelSchema(parse(definition));
|
|
985
979
|
});
|
|
@@ -1000,10 +994,8 @@ describe('ModelTransformer: ', () => {
|
|
|
1000
994
|
const transformer = new GraphQLTransform({
|
|
1001
995
|
transformers: [new ModelTransformer()],
|
|
1002
996
|
featureFlags,
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
project: config,
|
|
1006
|
-
},
|
|
997
|
+
resolverConfig: {
|
|
998
|
+
project: config,
|
|
1007
999
|
},
|
|
1008
1000
|
});
|
|
1009
1001
|
const out = transformer.transform(validSchema);
|
|
@@ -1096,12 +1088,11 @@ describe('ModelTransformer: ', () => {
|
|
|
1096
1088
|
}`;
|
|
1097
1089
|
|
|
1098
1090
|
const transformer = new GraphQLTransform({
|
|
1099
|
-
transformConfig: {
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
},
|
|
1091
|
+
transformConfig: {},
|
|
1092
|
+
resolverConfig: {
|
|
1093
|
+
project: {
|
|
1094
|
+
ConflictDetection: 'VERSION',
|
|
1095
|
+
ConflictHandler: ConflictHandlerType.AUTOMERGE,
|
|
1105
1096
|
},
|
|
1106
1097
|
},
|
|
1107
1098
|
sandboxModeEnabled: true,
|
|
@@ -1115,8 +1106,8 @@ describe('ModelTransformer: ', () => {
|
|
|
1115
1106
|
const queryObject = getObjectType(schema, 'Query');
|
|
1116
1107
|
expectFields(queryObject!, ['syncTodos']);
|
|
1117
1108
|
// sync resolvers
|
|
1118
|
-
expect(out.
|
|
1119
|
-
expect(out.
|
|
1109
|
+
expect(out.resolvers['Query.syncTodos.req.vtl']).toMatchSnapshot();
|
|
1110
|
+
expect(out.resolvers['Query.syncTodos.res.vtl']).toMatchSnapshot();
|
|
1120
1111
|
// ds table
|
|
1121
1112
|
cdkExpect(out.rootStack).to(
|
|
1122
1113
|
haveResource('AWS::DynamoDB::Table', {
|
|
@@ -1166,4 +1157,108 @@ describe('ModelTransformer: ', () => {
|
|
|
1166
1157
|
}),
|
|
1167
1158
|
);
|
|
1168
1159
|
});
|
|
1160
|
+
|
|
1161
|
+
it('should add the model parameters at the root sack', () => {
|
|
1162
|
+
const modelParams = {
|
|
1163
|
+
DynamoDBModelTableReadIOPS: expect.objectContaining({
|
|
1164
|
+
Type: 'Number',
|
|
1165
|
+
Default: 5,
|
|
1166
|
+
Description: 'The number of read IOPS the table should support.',
|
|
1167
|
+
}),
|
|
1168
|
+
DynamoDBModelTableWriteIOPS: expect.objectContaining({
|
|
1169
|
+
Type: 'Number',
|
|
1170
|
+
Default: 5,
|
|
1171
|
+
Description: 'The number of write IOPS the table should support.',
|
|
1172
|
+
}),
|
|
1173
|
+
DynamoDBBillingMode: expect.objectContaining({
|
|
1174
|
+
Type: 'String',
|
|
1175
|
+
Default: 'PAY_PER_REQUEST',
|
|
1176
|
+
AllowedValues: ['PAY_PER_REQUEST', 'PROVISIONED'],
|
|
1177
|
+
Description: 'Configure @model types to create DynamoDB tables with PAY_PER_REQUEST or PROVISIONED billing modes.',
|
|
1178
|
+
}),
|
|
1179
|
+
DynamoDBEnablePointInTimeRecovery: expect.objectContaining({
|
|
1180
|
+
Type: 'String',
|
|
1181
|
+
Default: 'false',
|
|
1182
|
+
AllowedValues: ['true', 'false'],
|
|
1183
|
+
Description: 'Whether to enable Point in Time Recovery on the table.',
|
|
1184
|
+
}),
|
|
1185
|
+
DynamoDBEnableServerSideEncryption: expect.objectContaining({
|
|
1186
|
+
Type: 'String',
|
|
1187
|
+
Default: 'true',
|
|
1188
|
+
AllowedValues: ['true', 'false'],
|
|
1189
|
+
Description: 'Enable server side encryption powered by KMS.',
|
|
1190
|
+
}),
|
|
1191
|
+
};
|
|
1192
|
+
const validSchema = `type Todo @model {
|
|
1193
|
+
name: String
|
|
1194
|
+
}`;
|
|
1195
|
+
const transformer = new GraphQLTransform({
|
|
1196
|
+
sandboxModeEnabled: true,
|
|
1197
|
+
transformers: [new ModelTransformer()],
|
|
1198
|
+
});
|
|
1199
|
+
const out = transformer.transform(validSchema);
|
|
1200
|
+
|
|
1201
|
+
const rootStack = out.rootStack;
|
|
1202
|
+
expect(rootStack).toBeDefined();
|
|
1203
|
+
expect(rootStack.Parameters).toMatchObject(modelParams);
|
|
1204
|
+
|
|
1205
|
+
const todoStack = out.stacks['Todo'];
|
|
1206
|
+
expect(todoStack).toBeDefined();
|
|
1207
|
+
expect(todoStack.Parameters).toMatchObject(modelParams);
|
|
1208
|
+
});
|
|
1209
|
+
|
|
1210
|
+
it('global auth enabled should add apiKey if not default mode of auth', () => {
|
|
1211
|
+
const validSchema = `
|
|
1212
|
+
type Post @model {
|
|
1213
|
+
id: ID!
|
|
1214
|
+
title: String!
|
|
1215
|
+
tags: [Tag]
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
type Tag {
|
|
1219
|
+
id: ID
|
|
1220
|
+
tags: [Tag]
|
|
1221
|
+
}`;
|
|
1222
|
+
const transformer = new GraphQLTransform({
|
|
1223
|
+
authConfig: {
|
|
1224
|
+
defaultAuthentication: {
|
|
1225
|
+
authenticationType: 'AMAZON_COGNITO_USER_POOLS',
|
|
1226
|
+
},
|
|
1227
|
+
additionalAuthenticationProviders: [
|
|
1228
|
+
{
|
|
1229
|
+
authenticationType: 'API_KEY',
|
|
1230
|
+
},
|
|
1231
|
+
],
|
|
1232
|
+
},
|
|
1233
|
+
sandboxModeEnabled: true,
|
|
1234
|
+
transformers: [new ModelTransformer()],
|
|
1235
|
+
});
|
|
1236
|
+
const out = transformer.transform(validSchema);
|
|
1237
|
+
expect(out).toBeDefined();
|
|
1238
|
+
|
|
1239
|
+
const schema = parse(out.schema);
|
|
1240
|
+
validateModelSchema(schema);
|
|
1241
|
+
|
|
1242
|
+
const postType = getObjectType(schema, 'Post')!;
|
|
1243
|
+
expect(postType).toBeDefined();
|
|
1244
|
+
expect(postType.directives).toBeDefined();
|
|
1245
|
+
expect(postType.directives!.some(dir => dir.name.value === 'aws_api_key')).toEqual(true);
|
|
1246
|
+
|
|
1247
|
+
const tagType = getObjectType(schema, 'Tag')!;
|
|
1248
|
+
expect(tagType).toBeDefined();
|
|
1249
|
+
expect(tagType.directives).toBeDefined();
|
|
1250
|
+
expect(tagType.directives!.some(dir => dir.name.value === 'aws_api_key')).toEqual(true);
|
|
1251
|
+
|
|
1252
|
+
// check operations
|
|
1253
|
+
const queryType = getObjectType(schema, 'Query')!;
|
|
1254
|
+
expect(queryType).toBeDefined();
|
|
1255
|
+
const mutationType = getObjectType(schema, 'Mutation')!;
|
|
1256
|
+
expect(mutationType).toBeDefined();
|
|
1257
|
+
const subscriptionType = getObjectType(schema, 'Subscription')!;
|
|
1258
|
+
expect(subscriptionType).toBeDefined();
|
|
1259
|
+
|
|
1260
|
+
for (const field of [...queryType.fields!, ...mutationType.fields!, ...subscriptionType.fields!]) {
|
|
1261
|
+
expect(field.directives!.some(dir => dir.name.value === 'aws_api_key')).toEqual(true);
|
|
1262
|
+
}
|
|
1263
|
+
});
|
|
1169
1264
|
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
function override(resource) {
|
|
2
|
+
resource.api.GraphQLAPI.xrayEnabled = true;
|
|
3
|
+
resource.models['Post'].modelDDBTable.billingMode = 'PROVISIONED';
|
|
4
|
+
resource.models['Comment'].modelDDBTable.billingMode = 'PROVISIONED';
|
|
5
|
+
// override resolver
|
|
6
|
+
resource.models['Post'].resolvers['subscriptionOnUpdatePostResolver'].requestMappingTemplate = 'mockTemplate';
|
|
7
|
+
}
|
|
8
|
+
exports.override = override;
|
package/src/definitions.ts
CHANGED
|
@@ -14,3 +14,5 @@ export const BOOLEAN_FUNCTIONS = new Set<string>(['attributeExists', 'attributeT
|
|
|
14
14
|
export const ATTRIBUTE_TYPES = ['binary', 'binarySet', 'bool', 'list', 'map', 'number', 'numberSet', 'string', 'stringSet', '_null'];
|
|
15
15
|
|
|
16
16
|
export const OPERATION_KEY = '__operation';
|
|
17
|
+
|
|
18
|
+
export const API_KEY_DIRECTIVE = 'aws_api_key';
|
|
@@ -5,6 +5,10 @@ import {
|
|
|
5
5
|
SyncConfig,
|
|
6
6
|
SyncUtils,
|
|
7
7
|
TransformerModelBase,
|
|
8
|
+
TransformerNestedStack,
|
|
9
|
+
FieldWrapper,
|
|
10
|
+
InputObjectDefinitionWrapper,
|
|
11
|
+
ObjectDefinitionWrapper,
|
|
8
12
|
} from '@aws-amplify/graphql-transformer-core';
|
|
9
13
|
import {
|
|
10
14
|
AppSyncDataSourceType,
|
|
@@ -20,6 +24,7 @@ import {
|
|
|
20
24
|
TransformerSchemaVisitStepContextProvider,
|
|
21
25
|
TransformerTransformSchemaStepContextProvider,
|
|
22
26
|
TransformerValidationStepContextProvider,
|
|
27
|
+
TransformerBeforeStepContextProvider,
|
|
23
28
|
} from '@aws-amplify/graphql-transformer-interfaces';
|
|
24
29
|
import { AttributeType, CfnTable, ITable, StreamViewType, Table, TableEncryption } from '@aws-cdk/aws-dynamodb';
|
|
25
30
|
import * as iam from '@aws-cdk/aws-iam';
|
|
@@ -51,8 +56,10 @@ import {
|
|
|
51
56
|
toPascalCase,
|
|
52
57
|
} from 'graphql-transformer-common';
|
|
53
58
|
import {
|
|
59
|
+
addDirectivesToOperation,
|
|
54
60
|
addModelConditionInputs,
|
|
55
61
|
createEnumModelFilters,
|
|
62
|
+
extendTypeWithDirectives,
|
|
56
63
|
makeCreateInputField,
|
|
57
64
|
makeDeleteInputField,
|
|
58
65
|
makeListQueryFilterInput,
|
|
@@ -60,6 +67,7 @@ import {
|
|
|
60
67
|
makeModelSortDirectionEnumObject,
|
|
61
68
|
makeMutationConditionInput,
|
|
62
69
|
makeUpdateInputField,
|
|
70
|
+
propagateApiKeyToNestedTypes,
|
|
63
71
|
} from './graphql-types';
|
|
64
72
|
import {
|
|
65
73
|
generateAuthExpressionForSandboxMode,
|
|
@@ -79,9 +87,9 @@ import {
|
|
|
79
87
|
generateListRequestTemplate,
|
|
80
88
|
generateSyncRequestTemplate,
|
|
81
89
|
} from './resolvers/query';
|
|
82
|
-
import { FieldWrapper, InputObjectDefinitionWrapper, ObjectDefinitionWrapper } from './wrappers/object-definition-wrapper';
|
|
83
90
|
import { CfnRole } from '@aws-cdk/aws-iam';
|
|
84
91
|
import md5 from 'md5';
|
|
92
|
+
import { API_KEY_DIRECTIVE } from './definitions';
|
|
85
93
|
|
|
86
94
|
export type Nullable<T> = T | null;
|
|
87
95
|
export type OptionalAndNullable<T> = Partial<T>;
|
|
@@ -167,6 +175,37 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
167
175
|
this.options = this.getOptions(options);
|
|
168
176
|
}
|
|
169
177
|
|
|
178
|
+
before = (ctx: TransformerBeforeStepContextProvider) => {
|
|
179
|
+
// add model related-parameters to the root stack
|
|
180
|
+
ctx.stackManager.addParameter(ResourceConstants.PARAMETERS.DynamoDBModelTableReadIOPS, {
|
|
181
|
+
description: 'The number of read IOPS the table should support.',
|
|
182
|
+
type: 'Number',
|
|
183
|
+
default: 5,
|
|
184
|
+
});
|
|
185
|
+
ctx.stackManager.addParameter(ResourceConstants.PARAMETERS.DynamoDBModelTableWriteIOPS, {
|
|
186
|
+
description: 'The number of write IOPS the table should support.',
|
|
187
|
+
type: 'Number',
|
|
188
|
+
default: 5,
|
|
189
|
+
});
|
|
190
|
+
ctx.stackManager.addParameter(ResourceConstants.PARAMETERS.DynamoDBBillingMode, {
|
|
191
|
+
description: 'Configure @model types to create DynamoDB tables with PAY_PER_REQUEST or PROVISIONED billing modes.',
|
|
192
|
+
default: 'PAY_PER_REQUEST',
|
|
193
|
+
allowedValues: ['PAY_PER_REQUEST', 'PROVISIONED'],
|
|
194
|
+
});
|
|
195
|
+
ctx.stackManager.addParameter(ResourceConstants.PARAMETERS.DynamoDBEnablePointInTimeRecovery, {
|
|
196
|
+
description: 'Whether to enable Point in Time Recovery on the table.',
|
|
197
|
+
type: 'String',
|
|
198
|
+
default: 'false',
|
|
199
|
+
allowedValues: ['true', 'false'],
|
|
200
|
+
});
|
|
201
|
+
ctx.stackManager.addParameter(ResourceConstants.PARAMETERS.DynamoDBEnableServerSideEncryption, {
|
|
202
|
+
description: 'Enable server side encryption powered by KMS.',
|
|
203
|
+
type: 'String',
|
|
204
|
+
default: 'true',
|
|
205
|
+
allowedValues: ['true', 'false'],
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
|
|
170
209
|
object = (definition: ObjectTypeDefinitionNode, directive: DirectiveNode, ctx: TransformerSchemaVisitStepContextProvider): void => {
|
|
171
210
|
const isTypeNameReserved =
|
|
172
211
|
definition.name.value === ctx.output.getQueryTypeName() ||
|
|
@@ -227,6 +266,7 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
227
266
|
this.ensureModelSortDirectionEnum(ctx);
|
|
228
267
|
for (const type of this.typesWithModelDirective) {
|
|
229
268
|
const def = ctx.output.getObject(type)!;
|
|
269
|
+
const hasAuth = def.directives!.some(dir => dir.name.value === 'auth');
|
|
230
270
|
|
|
231
271
|
// add Non Model type inputs
|
|
232
272
|
this.createNonModelInputs(ctx, def);
|
|
@@ -246,6 +286,24 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
246
286
|
if (ctx.isProjectUsingDataStore()) {
|
|
247
287
|
this.addModelSyncFields(ctx, type);
|
|
248
288
|
}
|
|
289
|
+
// global auth check
|
|
290
|
+
if (!hasAuth && ctx.sandboxModeEnabled && ctx.authConfig.defaultAuthentication.authenticationType !== 'API_KEY') {
|
|
291
|
+
const apiKeyDirArray = [makeDirective(API_KEY_DIRECTIVE, [])];
|
|
292
|
+
extendTypeWithDirectives(ctx, def.name.value, apiKeyDirArray);
|
|
293
|
+
propagateApiKeyToNestedTypes(ctx as TransformerContextProvider, def, new Set<string>());
|
|
294
|
+
for (let operationField of queryFields) {
|
|
295
|
+
const operationName = operationField.name.value;
|
|
296
|
+
addDirectivesToOperation(ctx, ctx.output.getQueryTypeName()!, operationName, apiKeyDirArray);
|
|
297
|
+
}
|
|
298
|
+
for (let operationField of mutationFields) {
|
|
299
|
+
const operationName = operationField.name.value;
|
|
300
|
+
addDirectivesToOperation(ctx, ctx.output.getMutationTypeName()!, operationName, apiKeyDirArray);
|
|
301
|
+
}
|
|
302
|
+
for (let operationField of subscriptionsFields) {
|
|
303
|
+
const operationName = operationField.name.value;
|
|
304
|
+
addDirectivesToOperation(ctx, ctx.output.getSubscriptionTypeName()!, operationName, apiKeyDirArray);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
249
307
|
}
|
|
250
308
|
};
|
|
251
309
|
|
|
@@ -274,10 +332,12 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
274
332
|
default:
|
|
275
333
|
throw new Error('Unknown query field type');
|
|
276
334
|
}
|
|
335
|
+
// TODO: add mechanism to add an auth like rule to all non auth @models
|
|
336
|
+
// this way we can just depend on auth to add the check
|
|
277
337
|
resolver.addToSlot(
|
|
278
338
|
'postAuth',
|
|
279
339
|
MappingTemplate.s3MappingTemplateFromString(
|
|
280
|
-
generateAuthExpressionForSandboxMode(context),
|
|
340
|
+
generateAuthExpressionForSandboxMode(context.sandboxModeEnabled),
|
|
281
341
|
`${query.typeName}.${query.fieldName}.{slotName}.{slotIndex}.req.vtl`,
|
|
282
342
|
),
|
|
283
343
|
);
|
|
@@ -304,7 +364,7 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
304
364
|
resolver.addToSlot(
|
|
305
365
|
'postAuth',
|
|
306
366
|
MappingTemplate.s3MappingTemplateFromString(
|
|
307
|
-
generateAuthExpressionForSandboxMode(context),
|
|
367
|
+
generateAuthExpressionForSandboxMode(context.sandboxModeEnabled),
|
|
308
368
|
`${mutation.typeName}.${mutation.fieldName}.{slotName}.{slotIndex}.req.vtl`,
|
|
309
369
|
),
|
|
310
370
|
);
|
|
@@ -352,7 +412,7 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
352
412
|
resolver.addToSlot(
|
|
353
413
|
'postAuth',
|
|
354
414
|
MappingTemplate.s3MappingTemplateFromString(
|
|
355
|
-
generateAuthExpressionForSandboxMode(context),
|
|
415
|
+
generateAuthExpressionForSandboxMode(context.sandboxModeEnabled),
|
|
356
416
|
`${subscription.typeName}.${subscription.fieldName}.{slotName}.{slotIndex}.req.vtl`,
|
|
357
417
|
),
|
|
358
418
|
);
|
|
@@ -1085,30 +1145,42 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
1085
1145
|
description: 'The number of read IOPS the table should support.',
|
|
1086
1146
|
type: 'Number',
|
|
1087
1147
|
default: 5,
|
|
1088
|
-
})
|
|
1148
|
+
});
|
|
1089
1149
|
const writeIops = new cdk.CfnParameter(stack, ResourceConstants.PARAMETERS.DynamoDBModelTableWriteIOPS, {
|
|
1090
1150
|
description: 'The number of write IOPS the table should support.',
|
|
1091
1151
|
type: 'Number',
|
|
1092
1152
|
default: 5,
|
|
1093
|
-
})
|
|
1153
|
+
});
|
|
1094
1154
|
const billingMode = new cdk.CfnParameter(stack, ResourceConstants.PARAMETERS.DynamoDBBillingMode, {
|
|
1095
1155
|
description: 'Configure @model types to create DynamoDB tables with PAY_PER_REQUEST or PROVISIONED billing modes.',
|
|
1096
1156
|
type: 'String',
|
|
1097
1157
|
default: 'PAY_PER_REQUEST',
|
|
1098
1158
|
allowedValues: ['PAY_PER_REQUEST', 'PROVISIONED'],
|
|
1099
|
-
})
|
|
1159
|
+
});
|
|
1100
1160
|
const pointInTimeRecovery = new cdk.CfnParameter(stack, ResourceConstants.PARAMETERS.DynamoDBEnablePointInTimeRecovery, {
|
|
1101
1161
|
description: 'Whether to enable Point in Time Recovery on the table.',
|
|
1102
1162
|
type: 'String',
|
|
1103
1163
|
default: 'false',
|
|
1104
1164
|
allowedValues: ['true', 'false'],
|
|
1105
|
-
})
|
|
1165
|
+
});
|
|
1106
1166
|
const enableSSE = new cdk.CfnParameter(stack, ResourceConstants.PARAMETERS.DynamoDBEnableServerSideEncryption, {
|
|
1107
1167
|
description: 'Enable server side encryption powered by KMS.',
|
|
1108
1168
|
type: 'String',
|
|
1109
1169
|
default: 'true',
|
|
1110
1170
|
allowedValues: ['true', 'false'],
|
|
1111
|
-
})
|
|
1171
|
+
});
|
|
1172
|
+
// add the connection between the root and nested stack so the values can be passed down
|
|
1173
|
+
(stack as TransformerNestedStack).setParameter(readIops.node.id, cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBModelTableReadIOPS));
|
|
1174
|
+
(stack as TransformerNestedStack).setParameter(writeIops.node.id, cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBModelTableWriteIOPS));
|
|
1175
|
+
(stack as TransformerNestedStack).setParameter(billingMode.node.id, cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBBillingMode));
|
|
1176
|
+
(stack as TransformerNestedStack).setParameter(
|
|
1177
|
+
pointInTimeRecovery.node.id,
|
|
1178
|
+
cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBEnablePointInTimeRecovery),
|
|
1179
|
+
);
|
|
1180
|
+
(stack as TransformerNestedStack).setParameter(
|
|
1181
|
+
enableSSE.node.id,
|
|
1182
|
+
cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBEnableServerSideEncryption),
|
|
1183
|
+
);
|
|
1112
1184
|
|
|
1113
1185
|
// Add conditions.
|
|
1114
1186
|
// eslint-disable-next-line no-new
|