@aws-amplify/graphql-model-transformer 0.9.4-beta.0 → 0.10.0-apiext3.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.
Files changed (34) hide show
  1. package/CHANGELOG.md +6 -8
  2. package/lib/graphql-model-transformer.d.ts.map +1 -1
  3. package/lib/graphql-model-transformer.js +85 -86
  4. package/lib/graphql-model-transformer.js.map +1 -1
  5. package/lib/graphql-types/common.d.ts +1 -1
  6. package/lib/graphql-types/common.js +29 -29
  7. package/lib/graphql-types/common.js.map +1 -1
  8. package/lib/graphql-types/mutation.js +14 -14
  9. package/lib/graphql-types/mutation.js.map +1 -1
  10. package/lib/graphql-types/query.js +6 -6
  11. package/lib/graphql-types/query.js.map +1 -1
  12. package/lib/resolvers/common.js +19 -19
  13. package/lib/resolvers/common.js.map +1 -1
  14. package/lib/resolvers/mutation.js +128 -128
  15. package/lib/resolvers/mutation.js.map +1 -1
  16. package/lib/resolvers/query.js +69 -69
  17. package/lib/resolvers/query.js.map +1 -1
  18. package/lib/resolvers/subscriptions.js +4 -4
  19. package/lib/resolvers/subscriptions.js.map +1 -1
  20. package/package.json +9 -6
  21. package/src/__tests__/__snapshots__/model-transformer-override.test.ts.snap +3009 -0
  22. package/src/__tests__/model-transformer-override.test.ts +41 -0
  23. package/src/__tests__/model-transformer.test.ts +13 -22
  24. package/src/__tests__/overrides/build/override.js +8 -0
  25. package/src/graphql-model-transformer.ts +3 -1
  26. package/src/graphql-types/common.ts +1 -1
  27. package/src/graphql-types/mutation.ts +1 -1
  28. package/src/graphql-types/query.ts +1 -1
  29. package/tsconfig.tsbuildinfo +1 -1
  30. package/lib/wrappers/object-definition-wrapper.d.ts +0 -77
  31. package/lib/wrappers/object-definition-wrapper.d.ts.map +0 -1
  32. package/lib/wrappers/object-definition-wrapper.js +0 -354
  33. package/lib/wrappers/object-definition-wrapper.js.map +0 -1
  34. package/src/wrappers/object-definition-wrapper.ts +0 -447
@@ -1,447 +0,0 @@
1
- import { DirectiveWrapper } from '@aws-amplify/graphql-transformer-core';
2
- import {
3
- DirectiveNode,
4
- DocumentNode,
5
- EnumTypeDefinitionNode,
6
- FieldDefinitionNode,
7
- InputObjectTypeDefinitionNode,
8
- InputValueDefinitionNode,
9
- isListType,
10
- Kind,
11
- ListTypeNode,
12
- Location,
13
- NamedTypeNode,
14
- NonNullTypeNode,
15
- ObjectTypeDefinitionNode,
16
- StringValueNode,
17
- TypeNode,
18
- } from 'graphql';
19
- import {
20
- DEFAULT_SCALARS,
21
- getBaseType,
22
- isEnum,
23
- isScalar,
24
- ModelResourceIDs,
25
- unwrapNonNull,
26
- withNamedNodeNamed,
27
- } from 'graphql-transformer-common';
28
-
29
- export class GenericFieldWrapper {
30
- protected type: TypeNode;
31
- public readonly directives: DirectiveWrapper[];
32
- public loc?: Location;
33
- public name: string;
34
- constructor(field: FieldDefinitionNode | InputValueDefinitionNode) {
35
- this.type = field.type;
36
- this.name = field.name.value;
37
- this.loc = field.loc;
38
- this.directives = (field.directives || []).map(d => new DirectiveWrapper(d));
39
- }
40
- isList = (): boolean => {
41
- return this.isListType(this.type);
42
- };
43
-
44
- isNonNullable = (): boolean => {
45
- return this.type.kind === 'NonNullType';
46
- };
47
-
48
- makeNullable = (): boolean => {
49
- if (this.isNonNullable()) {
50
- this.type = (this.type as NonNullTypeNode).type;
51
- return true;
52
- }
53
- return false;
54
- };
55
-
56
- makeNonNullable = (): boolean => {
57
- if (!this.isNonNullable()) {
58
- this.type = { kind: 'NonNullType', type: this.type } as NonNullTypeNode;
59
- return true;
60
- }
61
- return false;
62
- };
63
-
64
- wrapListType = (): GenericFieldWrapper => {
65
- if (!this.isList()) {
66
- this.type = {
67
- kind: 'ListType',
68
- type: this.type,
69
- };
70
- }
71
- return this;
72
- };
73
-
74
- unWrapListType = (): boolean => {
75
- if (!this.isList()) {
76
- this.type = (this.type as ListTypeNode).type;
77
- return true;
78
- }
79
- return false;
80
- };
81
-
82
- private isListType = (type: TypeNode): boolean => {
83
- if (type.kind === Kind.NON_NULL_TYPE) {
84
- return isListType(type.type);
85
- } else {
86
- return type.kind === Kind.LIST_TYPE;
87
- }
88
- };
89
-
90
- public getBaseType = (): NamedTypeNode => {
91
- let node = this.type;
92
- while (node.kind === Kind.LIST_TYPE || node.kind === Kind.NON_NULL_TYPE) {
93
- node = node.type;
94
- }
95
- return node;
96
- };
97
- public getTypeName = (): string => {
98
- return this.getBaseType().name.value;
99
- };
100
-
101
- public isScalar = (): boolean => {
102
- const typeName = this.getTypeName();
103
- return Object.keys(DEFAULT_SCALARS).includes(typeName);
104
- };
105
- }
106
-
107
- // Todo: Create a wrapper for InputValueDefinination with default values
108
- // export class InputValueDefinitionWrapper extends GenericFieldWrapper {
109
- // private defaultValue;
110
- // constructor(private node: InputValueDefinitionNode) {
111
- // super(node);
112
- // this.defaultValue = node.defaultValue;
113
- // }
114
- // serialize = (): InputValueDefinitionNode => {
115
- // return {
116
- // ...this.node,
117
- // type: this.type,
118
- // name: { kind: 'Name', value: this.name },
119
- // defaultValue: this.defaultValue,
120
- // };
121
- // };
122
- // static create = (name: string, type: string, defaultValue?: string|number|boolean| isNullable: boolean = false, isList: boolean = false): InputValueDefinitionWrapper => {
123
-
124
- // };
125
- // }
126
-
127
- export class InputFieldWrapper extends GenericFieldWrapper {
128
- public readonly argumenets?: InputValueDefinitionNode[];
129
- public readonly description?: StringValueNode;
130
- public type: TypeNode;
131
- public readonly name: string;
132
- public readonly loc?: Location;
133
-
134
- constructor(protected field: InputValueDefinitionNode) {
135
- super(field);
136
- this.type = field.type;
137
- this.name = field.name.value;
138
- }
139
-
140
- public serialize = (): InputValueDefinitionNode => {
141
- return {
142
- ...this.field,
143
- kind: 'InputValueDefinition',
144
- name: { kind: 'Name', value: this.name },
145
- type: this.type,
146
- description: this.description,
147
- directives: this.directives?.map(d => d.serialize()),
148
- };
149
- };
150
-
151
- static fromField = (name: string, field: FieldDefinitionNode, document: DocumentNode): InputFieldWrapper => {
152
- const autoGeneratableFieldsWithType: Record<string, string[]> = {
153
- id: ['ID'],
154
- createdAt: ['AWSDateTime', 'String'],
155
- updatedAt: ['AWSDateTime', 'String'],
156
- };
157
-
158
- let type: TypeNode;
159
-
160
- if (
161
- Object.keys(autoGeneratableFieldsWithType).indexOf(name) !== -1 &&
162
- autoGeneratableFieldsWithType[name].indexOf(unwrapNonNull(field.type).name.value) !== -1
163
- ) {
164
- // ids are always optional. when provided the value is used.
165
- // when not provided the value is not used.
166
- type = unwrapNonNull(field.type);
167
- } else {
168
- type =
169
- isScalar(field.type) || isEnum(field.type, document)
170
- ? field.type
171
- : withNamedNodeNamed(field.type, ModelResourceIDs.NonModelInputObjectName(getBaseType(field.type)));
172
- }
173
-
174
- return new InputFieldWrapper({
175
- kind: 'InputValueDefinition',
176
- name: { kind: 'Name', value: name },
177
- type,
178
- });
179
- };
180
-
181
- static create = (name: string, type: string, isNullable = false, isList = false): InputFieldWrapper => {
182
- const field = new InputFieldWrapper({
183
- kind: 'InputValueDefinition',
184
- name: {
185
- kind: 'Name',
186
- value: name,
187
- },
188
- type: {
189
- kind: 'NamedType',
190
- name: {
191
- value: type,
192
- kind: 'Name',
193
- },
194
- },
195
- });
196
- if (!isNullable) {
197
- field.makeNonNullable();
198
- }
199
- if (isList) {
200
- field.wrapListType();
201
- }
202
- return field;
203
- };
204
- }
205
- export class FieldWrapper extends GenericFieldWrapper {
206
- public readonly argumenets?: InputValueDefinitionNode[];
207
- public readonly description?: StringValueNode;
208
- public readonly loc?: Location;
209
-
210
- // arguments to be added
211
- constructor(field: FieldDefinitionNode) {
212
- super(field);
213
- this.argumenets = [...(field.arguments || [])];
214
- this.description = field.description;
215
- this.loc = field.loc;
216
- }
217
-
218
- public serialize = (): FieldDefinitionNode => {
219
- return {
220
- kind: 'FieldDefinition',
221
- name: { kind: 'Name', value: this.name },
222
- type: this.type,
223
- arguments: this.argumenets,
224
- description: this.description,
225
- directives: this.directives?.map(d => d.serialize()),
226
- };
227
- };
228
-
229
- static create = (name: string, type: string, isNullable = false, isList = false): FieldWrapper => {
230
- const field = new FieldWrapper({
231
- kind: 'FieldDefinition',
232
- name: {
233
- kind: 'Name',
234
- value: name,
235
- },
236
- type: {
237
- kind: 'NamedType',
238
- name: {
239
- value: type,
240
- kind: 'Name',
241
- },
242
- },
243
- });
244
- if (!isNullable) {
245
- field.makeNonNullable();
246
- }
247
- if (isList) {
248
- field.wrapListType().makeNonNullable();
249
- }
250
- return field;
251
- };
252
- }
253
-
254
- export class ObjectDefinitionWrapper {
255
- public readonly directives?: DirectiveWrapper[];
256
- public readonly fields: FieldWrapper[];
257
- public readonly name: string;
258
- constructor(private node: ObjectTypeDefinitionNode) {
259
- this.directives = (node.directives || []).map(d => new DirectiveWrapper(d));
260
- this.fields = (node.fields || []).map(f => new FieldWrapper(f));
261
- this.name = node.name.value;
262
- }
263
-
264
- public serialize = (): ObjectTypeDefinitionNode => {
265
- return {
266
- ...this.node,
267
- name: {
268
- kind: 'Name',
269
- value: this.name,
270
- },
271
- fields: this.fields.map(f => f.serialize()),
272
- directives: this.directives?.map(d => d.serialize()),
273
- };
274
- };
275
-
276
- hasField = (name: string): boolean => {
277
- const field = this.fields.find(f => f.name === name);
278
- return field ? true : false;
279
- };
280
-
281
- getField = (name: string): FieldWrapper => {
282
- const field = this.fields.find(f => f.name === name);
283
- if (!field) {
284
- throw new Error(`Field ${name} missing in type ${this.name}`);
285
- }
286
- return field;
287
- };
288
-
289
- addField = (field: FieldWrapper): void => {
290
- if (this.hasField(field.name)) {
291
- throw new Error(`type ${this.name} has already a field with name ${field.name}`);
292
- }
293
- this.fields.push(field);
294
- };
295
-
296
- removeField = (field: FieldWrapper): void => {
297
- if (this.hasField(field.name)) {
298
- throw new Error(`type ${this.name} does not have the field with name ${field.name}`);
299
- }
300
- const index = this.fields.indexOf(field);
301
-
302
- this.fields.splice(index, 1);
303
- };
304
-
305
- static create = (name: string, fields: FieldDefinitionNode[] = [], directives: DirectiveNode[] = []): ObjectDefinitionWrapper => {
306
- return new ObjectDefinitionWrapper({
307
- kind: 'ObjectTypeDefinition',
308
- name: {
309
- kind: 'Name',
310
- value: name,
311
- },
312
- fields: fields,
313
- directives: directives,
314
- });
315
- };
316
- }
317
-
318
- export class InputObjectDefinitionWrapper {
319
- public readonly directives?: DirectiveWrapper[];
320
- public readonly fields: InputFieldWrapper[];
321
- public readonly name: string;
322
- constructor(private node: InputObjectTypeDefinitionNode) {
323
- this.directives = (node.directives || []).map(d => new DirectiveWrapper(d));
324
- this.fields = (node.fields || []).map(f => new InputFieldWrapper(f));
325
- this.name = node.name.value;
326
- }
327
-
328
- public serialize = (): InputObjectTypeDefinitionNode => {
329
- return {
330
- ...this.node,
331
- fields: this.fields.map(f => f.serialize()),
332
- directives: this.directives?.map(d => d.serialize()),
333
- };
334
- };
335
- hasField = (name: string): boolean => {
336
- const field = this.fields.find(f => f.name === name);
337
- return field ? true : false;
338
- };
339
-
340
- getField = (name: string): InputFieldWrapper => {
341
- const field = this.fields.find(f => f.name === name);
342
- if (!field) {
343
- throw new Error(`Field ${name} missing in type ${this.name}`);
344
- }
345
- return field;
346
- };
347
-
348
- addField = (field: InputFieldWrapper): void => {
349
- if (this.hasField(field.name)) {
350
- throw new Error(`type ${this.name} has already a field with name ${field.name}`);
351
- }
352
- this.fields.push(field);
353
- };
354
-
355
- removeField = (field: InputFieldWrapper): void => {
356
- if (!this.hasField(field.name)) {
357
- throw new Error(`type ${this.name} does not have the field with name ${field.name}`);
358
- }
359
- const index = this.fields.indexOf(field);
360
-
361
- this.fields.splice(index, 1);
362
- };
363
-
364
- static create = (
365
- name: string,
366
- fields: InputValueDefinitionNode[] = [],
367
- directives: DirectiveNode[] = [],
368
- ): InputObjectDefinitionWrapper => {
369
- const wrappedObj = new InputObjectDefinitionWrapper({
370
- kind: 'InputObjectTypeDefinition',
371
- name: {
372
- kind: 'Name',
373
- value: name,
374
- },
375
- fields: [],
376
- directives: directives,
377
- });
378
- for (let field of fields) {
379
- const fieldWrapper = new InputFieldWrapper(field);
380
- wrappedObj.addField(fieldWrapper);
381
- }
382
- return wrappedObj;
383
- };
384
-
385
- static fromObject = (name: string, def: ObjectTypeDefinitionNode, document: DocumentNode): InputObjectDefinitionWrapper => {
386
- const inputObj: InputObjectTypeDefinitionNode = {
387
- kind: 'InputObjectTypeDefinition',
388
- name: { kind: 'Name', value: name },
389
- fields: [],
390
- directives: [],
391
- };
392
-
393
- const wrappedInput = new InputObjectDefinitionWrapper(inputObj);
394
- for (let f of def.fields || []) {
395
- const wrappedField = InputFieldWrapper.fromField(f.name.value, f, document);
396
- wrappedInput.fields.push(wrappedField);
397
- }
398
- return wrappedInput;
399
- };
400
- }
401
-
402
- export class EnumWrapper {
403
- public readonly name: string;
404
- public values: string[];
405
- public directives: DirectiveWrapper[];
406
- constructor(private node: EnumTypeDefinitionNode) {
407
- this.name = node.name.value;
408
- this.values = node.values?.map(v => v.name.value) || [];
409
- this.directives = (node.directives || []).map(d => new DirectiveWrapper(d));
410
- }
411
-
412
- addValue = (value: string): void => {
413
- this.values.push(value);
414
- };
415
-
416
- serialize = (): EnumTypeDefinitionNode => {
417
- return {
418
- ...this.node,
419
- name: {
420
- kind: 'Name',
421
- value: this.name,
422
- },
423
- directives: this.directives.map(d => d.serialize()),
424
- values: this.values.map(value => ({
425
- kind: 'EnumValueDefinition',
426
- name: {
427
- kind: 'Name',
428
- value: value,
429
- },
430
- })),
431
- };
432
- };
433
- static create = (name: string, values: string[] = []): EnumWrapper => {
434
- const wrappedEnum = new EnumWrapper({
435
- kind: 'EnumTypeDefinition',
436
- name: {
437
- kind: 'Name',
438
- value: name,
439
- },
440
- values: [],
441
- });
442
- values.forEach(val => {
443
- wrappedEnum.addValue(val);
444
- });
445
- return wrappedEnum;
446
- };
447
- }