@aws-amplify/graphql-model-transformer 0.9.3-beta.0 → 0.10.0-apiext2.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 +12 -5
- 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 +129 -76
- package/lib/graphql-model-transformer.js.map +1 -1
- package/lib/graphql-types/common.d.ts +6 -2
- package/lib/graphql-types/common.d.ts.map +1 -1
- package/lib/graphql-types/common.js +67 -3
- package/lib/graphql-types/common.js.map +1 -1
- package/lib/graphql-types/mutation.js +3 -3
- package/lib/graphql-types/mutation.js.map +1 -1
- package/lib/graphql-types/query.js +1 -1
- 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/lib/wrappers/object-definition-wrapper.js +5 -5
- package/lib/wrappers/object-definition-wrapper.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 +78 -8
- package/src/graphql-types/common.ts +97 -1
- package/src/resolvers/common.ts +1 -3
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -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,7 @@ import {
|
|
|
5
5
|
SyncConfig,
|
|
6
6
|
SyncUtils,
|
|
7
7
|
TransformerModelBase,
|
|
8
|
+
TransformerNestedStack,
|
|
8
9
|
} from '@aws-amplify/graphql-transformer-core';
|
|
9
10
|
import {
|
|
10
11
|
AppSyncDataSourceType,
|
|
@@ -20,6 +21,7 @@ import {
|
|
|
20
21
|
TransformerSchemaVisitStepContextProvider,
|
|
21
22
|
TransformerTransformSchemaStepContextProvider,
|
|
22
23
|
TransformerValidationStepContextProvider,
|
|
24
|
+
TransformerBeforeStepContextProvider,
|
|
23
25
|
} from '@aws-amplify/graphql-transformer-interfaces';
|
|
24
26
|
import { AttributeType, CfnTable, ITable, StreamViewType, Table, TableEncryption } from '@aws-cdk/aws-dynamodb';
|
|
25
27
|
import * as iam from '@aws-cdk/aws-iam';
|
|
@@ -51,8 +53,10 @@ import {
|
|
|
51
53
|
toPascalCase,
|
|
52
54
|
} from 'graphql-transformer-common';
|
|
53
55
|
import {
|
|
56
|
+
addDirectivesToOperation,
|
|
54
57
|
addModelConditionInputs,
|
|
55
58
|
createEnumModelFilters,
|
|
59
|
+
extendTypeWithDirectives,
|
|
56
60
|
makeCreateInputField,
|
|
57
61
|
makeDeleteInputField,
|
|
58
62
|
makeListQueryFilterInput,
|
|
@@ -60,6 +64,7 @@ import {
|
|
|
60
64
|
makeModelSortDirectionEnumObject,
|
|
61
65
|
makeMutationConditionInput,
|
|
62
66
|
makeUpdateInputField,
|
|
67
|
+
propagateApiKeyToNestedTypes,
|
|
63
68
|
} from './graphql-types';
|
|
64
69
|
import {
|
|
65
70
|
generateAuthExpressionForSandboxMode,
|
|
@@ -82,6 +87,7 @@ import {
|
|
|
82
87
|
import { FieldWrapper, InputObjectDefinitionWrapper, ObjectDefinitionWrapper } from './wrappers/object-definition-wrapper';
|
|
83
88
|
import { CfnRole } from '@aws-cdk/aws-iam';
|
|
84
89
|
import md5 from 'md5';
|
|
90
|
+
import { API_KEY_DIRECTIVE } from './definitions';
|
|
85
91
|
|
|
86
92
|
export type Nullable<T> = T | null;
|
|
87
93
|
export type OptionalAndNullable<T> = Partial<T>;
|
|
@@ -167,6 +173,37 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
167
173
|
this.options = this.getOptions(options);
|
|
168
174
|
}
|
|
169
175
|
|
|
176
|
+
before = (ctx: TransformerBeforeStepContextProvider) => {
|
|
177
|
+
// add model related-parameters to the root stack
|
|
178
|
+
ctx.stackManager.addParameter(ResourceConstants.PARAMETERS.DynamoDBModelTableReadIOPS, {
|
|
179
|
+
description: 'The number of read IOPS the table should support.',
|
|
180
|
+
type: 'Number',
|
|
181
|
+
default: 5,
|
|
182
|
+
});
|
|
183
|
+
ctx.stackManager.addParameter(ResourceConstants.PARAMETERS.DynamoDBModelTableWriteIOPS, {
|
|
184
|
+
description: 'The number of write IOPS the table should support.',
|
|
185
|
+
type: 'Number',
|
|
186
|
+
default: 5,
|
|
187
|
+
});
|
|
188
|
+
ctx.stackManager.addParameter(ResourceConstants.PARAMETERS.DynamoDBBillingMode, {
|
|
189
|
+
description: 'Configure @model types to create DynamoDB tables with PAY_PER_REQUEST or PROVISIONED billing modes.',
|
|
190
|
+
default: 'PAY_PER_REQUEST',
|
|
191
|
+
allowedValues: ['PAY_PER_REQUEST', 'PROVISIONED'],
|
|
192
|
+
});
|
|
193
|
+
ctx.stackManager.addParameter(ResourceConstants.PARAMETERS.DynamoDBEnablePointInTimeRecovery, {
|
|
194
|
+
description: 'Whether to enable Point in Time Recovery on the table.',
|
|
195
|
+
type: 'String',
|
|
196
|
+
default: 'false',
|
|
197
|
+
allowedValues: ['true', 'false'],
|
|
198
|
+
});
|
|
199
|
+
ctx.stackManager.addParameter(ResourceConstants.PARAMETERS.DynamoDBEnableServerSideEncryption, {
|
|
200
|
+
description: 'Enable server side encryption powered by KMS.',
|
|
201
|
+
type: 'String',
|
|
202
|
+
default: 'true',
|
|
203
|
+
allowedValues: ['true', 'false'],
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
|
|
170
207
|
object = (definition: ObjectTypeDefinitionNode, directive: DirectiveNode, ctx: TransformerSchemaVisitStepContextProvider): void => {
|
|
171
208
|
const isTypeNameReserved =
|
|
172
209
|
definition.name.value === ctx.output.getQueryTypeName() ||
|
|
@@ -227,6 +264,7 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
227
264
|
this.ensureModelSortDirectionEnum(ctx);
|
|
228
265
|
for (const type of this.typesWithModelDirective) {
|
|
229
266
|
const def = ctx.output.getObject(type)!;
|
|
267
|
+
const hasAuth = def.directives!.some(dir => dir.name.value === 'auth');
|
|
230
268
|
|
|
231
269
|
// add Non Model type inputs
|
|
232
270
|
this.createNonModelInputs(ctx, def);
|
|
@@ -246,6 +284,24 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
246
284
|
if (ctx.isProjectUsingDataStore()) {
|
|
247
285
|
this.addModelSyncFields(ctx, type);
|
|
248
286
|
}
|
|
287
|
+
// global auth check
|
|
288
|
+
if (!hasAuth && ctx.sandboxModeEnabled && ctx.authConfig.defaultAuthentication.authenticationType !== 'API_KEY') {
|
|
289
|
+
const apiKeyDirArray = [makeDirective(API_KEY_DIRECTIVE, [])];
|
|
290
|
+
extendTypeWithDirectives(ctx, def.name.value, apiKeyDirArray);
|
|
291
|
+
propagateApiKeyToNestedTypes(ctx as TransformerContextProvider, def, new Set<string>());
|
|
292
|
+
for (let operationField of queryFields) {
|
|
293
|
+
const operationName = operationField.name.value;
|
|
294
|
+
addDirectivesToOperation(ctx, ctx.output.getQueryTypeName()!, operationName, apiKeyDirArray);
|
|
295
|
+
}
|
|
296
|
+
for (let operationField of mutationFields) {
|
|
297
|
+
const operationName = operationField.name.value;
|
|
298
|
+
addDirectivesToOperation(ctx, ctx.output.getMutationTypeName()!, operationName, apiKeyDirArray);
|
|
299
|
+
}
|
|
300
|
+
for (let operationField of subscriptionsFields) {
|
|
301
|
+
const operationName = operationField.name.value;
|
|
302
|
+
addDirectivesToOperation(ctx, ctx.output.getSubscriptionTypeName()!, operationName, apiKeyDirArray);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
249
305
|
}
|
|
250
306
|
};
|
|
251
307
|
|
|
@@ -274,10 +330,12 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
274
330
|
default:
|
|
275
331
|
throw new Error('Unknown query field type');
|
|
276
332
|
}
|
|
333
|
+
// TODO: add mechanism to add an auth like rule to all non auth @models
|
|
334
|
+
// this way we can just depend on auth to add the check
|
|
277
335
|
resolver.addToSlot(
|
|
278
336
|
'postAuth',
|
|
279
337
|
MappingTemplate.s3MappingTemplateFromString(
|
|
280
|
-
generateAuthExpressionForSandboxMode(context),
|
|
338
|
+
generateAuthExpressionForSandboxMode(context.sandboxModeEnabled),
|
|
281
339
|
`${query.typeName}.${query.fieldName}.{slotName}.{slotIndex}.req.vtl`,
|
|
282
340
|
),
|
|
283
341
|
);
|
|
@@ -304,7 +362,7 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
304
362
|
resolver.addToSlot(
|
|
305
363
|
'postAuth',
|
|
306
364
|
MappingTemplate.s3MappingTemplateFromString(
|
|
307
|
-
generateAuthExpressionForSandboxMode(context),
|
|
365
|
+
generateAuthExpressionForSandboxMode(context.sandboxModeEnabled),
|
|
308
366
|
`${mutation.typeName}.${mutation.fieldName}.{slotName}.{slotIndex}.req.vtl`,
|
|
309
367
|
),
|
|
310
368
|
);
|
|
@@ -352,7 +410,7 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
352
410
|
resolver.addToSlot(
|
|
353
411
|
'postAuth',
|
|
354
412
|
MappingTemplate.s3MappingTemplateFromString(
|
|
355
|
-
generateAuthExpressionForSandboxMode(context),
|
|
413
|
+
generateAuthExpressionForSandboxMode(context.sandboxModeEnabled),
|
|
356
414
|
`${subscription.typeName}.${subscription.fieldName}.{slotName}.{slotIndex}.req.vtl`,
|
|
357
415
|
),
|
|
358
416
|
);
|
|
@@ -1085,30 +1143,42 @@ export class ModelTransformer extends TransformerModelBase implements Transforme
|
|
|
1085
1143
|
description: 'The number of read IOPS the table should support.',
|
|
1086
1144
|
type: 'Number',
|
|
1087
1145
|
default: 5,
|
|
1088
|
-
})
|
|
1146
|
+
});
|
|
1089
1147
|
const writeIops = new cdk.CfnParameter(stack, ResourceConstants.PARAMETERS.DynamoDBModelTableWriteIOPS, {
|
|
1090
1148
|
description: 'The number of write IOPS the table should support.',
|
|
1091
1149
|
type: 'Number',
|
|
1092
1150
|
default: 5,
|
|
1093
|
-
})
|
|
1151
|
+
});
|
|
1094
1152
|
const billingMode = new cdk.CfnParameter(stack, ResourceConstants.PARAMETERS.DynamoDBBillingMode, {
|
|
1095
1153
|
description: 'Configure @model types to create DynamoDB tables with PAY_PER_REQUEST or PROVISIONED billing modes.',
|
|
1096
1154
|
type: 'String',
|
|
1097
1155
|
default: 'PAY_PER_REQUEST',
|
|
1098
1156
|
allowedValues: ['PAY_PER_REQUEST', 'PROVISIONED'],
|
|
1099
|
-
})
|
|
1157
|
+
});
|
|
1100
1158
|
const pointInTimeRecovery = new cdk.CfnParameter(stack, ResourceConstants.PARAMETERS.DynamoDBEnablePointInTimeRecovery, {
|
|
1101
1159
|
description: 'Whether to enable Point in Time Recovery on the table.',
|
|
1102
1160
|
type: 'String',
|
|
1103
1161
|
default: 'false',
|
|
1104
1162
|
allowedValues: ['true', 'false'],
|
|
1105
|
-
})
|
|
1163
|
+
});
|
|
1106
1164
|
const enableSSE = new cdk.CfnParameter(stack, ResourceConstants.PARAMETERS.DynamoDBEnableServerSideEncryption, {
|
|
1107
1165
|
description: 'Enable server side encryption powered by KMS.',
|
|
1108
1166
|
type: 'String',
|
|
1109
1167
|
default: 'true',
|
|
1110
1168
|
allowedValues: ['true', 'false'],
|
|
1111
|
-
})
|
|
1169
|
+
});
|
|
1170
|
+
// add the connection between the root and nested stack so the values can be passed down
|
|
1171
|
+
(stack as TransformerNestedStack).setParameter(readIops.node.id, cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBModelTableReadIOPS));
|
|
1172
|
+
(stack as TransformerNestedStack).setParameter(writeIops.node.id, cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBModelTableWriteIOPS));
|
|
1173
|
+
(stack as TransformerNestedStack).setParameter(billingMode.node.id, cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBBillingMode));
|
|
1174
|
+
(stack as TransformerNestedStack).setParameter(
|
|
1175
|
+
pointInTimeRecovery.node.id,
|
|
1176
|
+
cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBEnablePointInTimeRecovery),
|
|
1177
|
+
);
|
|
1178
|
+
(stack as TransformerNestedStack).setParameter(
|
|
1179
|
+
enableSSE.node.id,
|
|
1180
|
+
cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBEnableServerSideEncryption),
|
|
1181
|
+
);
|
|
1112
1182
|
|
|
1113
1183
|
// Add conditions.
|
|
1114
1184
|
// eslint-disable-next-line no-new
|
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
import { TransformerTransformSchemaStepContextProvider } from '@aws-amplify/graphql-transformer-interfaces';
|
|
1
|
+
import { TransformerContextProvider, TransformerTransformSchemaStepContextProvider } from '@aws-amplify/graphql-transformer-interfaces';
|
|
2
2
|
import {
|
|
3
|
+
DirectiveNode,
|
|
3
4
|
EnumTypeDefinitionNode,
|
|
4
5
|
FieldDefinitionNode,
|
|
5
6
|
InputObjectTypeDefinitionNode,
|
|
6
7
|
Kind,
|
|
8
|
+
NamedTypeNode,
|
|
7
9
|
ObjectTypeDefinitionNode,
|
|
8
10
|
TypeDefinitionNode,
|
|
9
11
|
} from 'graphql';
|
|
10
12
|
import {
|
|
13
|
+
blankObjectExtension,
|
|
11
14
|
DEFAULT_SCALARS,
|
|
15
|
+
extendFieldWithDirectives,
|
|
16
|
+
extensionWithDirectives,
|
|
17
|
+
getBaseType,
|
|
12
18
|
makeArgument,
|
|
13
19
|
makeDirective,
|
|
14
20
|
makeField,
|
|
@@ -29,6 +35,7 @@ import {
|
|
|
29
35
|
SIZE_CONDITIONS,
|
|
30
36
|
STRING_CONDITIONS,
|
|
31
37
|
STRING_FUNCTIONS,
|
|
38
|
+
API_KEY_DIRECTIVE,
|
|
32
39
|
} from '../definitions';
|
|
33
40
|
import {
|
|
34
41
|
EnumWrapper,
|
|
@@ -245,7 +252,96 @@ export function makeEnumFilterInput(fieldWrapper: FieldWrapper): InputObjectType
|
|
|
245
252
|
return input.serialize();
|
|
246
253
|
}
|
|
247
254
|
|
|
255
|
+
export const addDirectivesToField = (
|
|
256
|
+
ctx: TransformerTransformSchemaStepContextProvider,
|
|
257
|
+
typeName: string,
|
|
258
|
+
fieldName: string,
|
|
259
|
+
directives: Array<DirectiveNode>,
|
|
260
|
+
) => {
|
|
261
|
+
const type = ctx.output.getType(typeName) as ObjectTypeDefinitionNode;
|
|
262
|
+
if (type) {
|
|
263
|
+
const field = type.fields?.find(f => f.name.value === fieldName);
|
|
264
|
+
if (field) {
|
|
265
|
+
const newFields = [...type.fields!.filter(f => f.name.value !== field.name.value), extendFieldWithDirectives(field, directives)];
|
|
266
|
+
|
|
267
|
+
const newType = {
|
|
268
|
+
...type,
|
|
269
|
+
fields: newFields,
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
ctx.output.putType(newType);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
export const addDirectivesToOperation = (
|
|
278
|
+
ctx: TransformerTransformSchemaStepContextProvider,
|
|
279
|
+
typeName: string,
|
|
280
|
+
operationName: string,
|
|
281
|
+
directives: Array<DirectiveNode>,
|
|
282
|
+
) => {
|
|
283
|
+
// add directives to the given operation
|
|
284
|
+
addDirectivesToField(ctx, typeName, operationName, directives);
|
|
285
|
+
|
|
286
|
+
// add the directives to the result type of the operation
|
|
287
|
+
const type = ctx.output.getType(typeName) as ObjectTypeDefinitionNode;
|
|
288
|
+
if (type) {
|
|
289
|
+
const field = type.fields!.find(f => f.name.value === operationName);
|
|
290
|
+
|
|
291
|
+
if (field) {
|
|
292
|
+
const returnFieldType = field.type as NamedTypeNode;
|
|
293
|
+
|
|
294
|
+
if (returnFieldType.name) {
|
|
295
|
+
const returnTypeName = returnFieldType.name.value;
|
|
296
|
+
|
|
297
|
+
extendTypeWithDirectives(ctx, returnTypeName, directives);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
export const extendTypeWithDirectives = (
|
|
304
|
+
ctx: TransformerTransformSchemaStepContextProvider,
|
|
305
|
+
typeName: string,
|
|
306
|
+
directives: Array<DirectiveNode>,
|
|
307
|
+
): void => {
|
|
308
|
+
let objectTypeExtension = blankObjectExtension(typeName);
|
|
309
|
+
objectTypeExtension = extensionWithDirectives(objectTypeExtension, directives);
|
|
310
|
+
ctx.output.addObjectExtension(objectTypeExtension);
|
|
311
|
+
};
|
|
312
|
+
|
|
248
313
|
export function makeModelSortDirectionEnumObject(): EnumTypeDefinitionNode {
|
|
249
314
|
const name = 'ModelSortDirection';
|
|
250
315
|
return EnumWrapper.create(name, ['ASC', 'DESC']).serialize();
|
|
251
316
|
}
|
|
317
|
+
// the smaller version of it's @auth equivalent since we only support
|
|
318
|
+
// apikey as the only global auth rule
|
|
319
|
+
export const propagateApiKeyToNestedTypes = (
|
|
320
|
+
ctx: TransformerContextProvider,
|
|
321
|
+
def: ObjectTypeDefinitionNode,
|
|
322
|
+
seenNonModelTypes: Set<string>,
|
|
323
|
+
) => {
|
|
324
|
+
const nonModelTypePredicate = (fieldType: TypeDefinitionNode): TypeDefinitionNode | undefined => {
|
|
325
|
+
if (fieldType) {
|
|
326
|
+
if (fieldType.kind !== 'ObjectTypeDefinition') {
|
|
327
|
+
return undefined;
|
|
328
|
+
}
|
|
329
|
+
const typeModel = fieldType.directives!.find(dir => dir.name.value === 'model');
|
|
330
|
+
return typeModel !== undefined ? undefined : fieldType;
|
|
331
|
+
}
|
|
332
|
+
return fieldType;
|
|
333
|
+
};
|
|
334
|
+
const nonModelFieldTypes = def
|
|
335
|
+
.fields!.map(f => ctx.output.getType(getBaseType(f.type)) as TypeDefinitionNode)
|
|
336
|
+
.filter(nonModelTypePredicate);
|
|
337
|
+
for (const nonModelFieldType of nonModelFieldTypes) {
|
|
338
|
+
const nonModelName = nonModelFieldType.name.value;
|
|
339
|
+
const hasSeenType = seenNonModelTypes.has(nonModelName);
|
|
340
|
+
const hasApiKey = nonModelFieldType.directives?.some(dir => dir.name.value === API_KEY_DIRECTIVE) ?? false;
|
|
341
|
+
if (!hasSeenType && !hasApiKey) {
|
|
342
|
+
seenNonModelTypes.add(nonModelName);
|
|
343
|
+
extendTypeWithDirectives(ctx, nonModelName, [makeDirective(API_KEY_DIRECTIVE, [])]);
|
|
344
|
+
propagateApiKeyToNestedTypes(ctx, nonModelFieldType as ObjectTypeDefinitionNode, seenNonModelTypes);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
};
|