@aws-amplify/graphql-model-transformer 0.13.4-pkg-npm-install.0 → 0.13.5-beta.1

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.
@@ -1,9 +1,9 @@
1
1
  import { TransformerTransformSchemaStepContextProvider } from '@aws-amplify/graphql-transformer-interfaces';
2
2
  import { DocumentNode, InputObjectTypeDefinitionNode, ObjectTypeDefinitionNode } from 'graphql';
3
3
  import { ModelResourceIDs, toPascalCase } from 'graphql-transformer-common';
4
- import { ModelDirectiveConfiguration } from '../graphql-model-transformer';
5
4
  import { InputFieldWrapper, InputObjectDefinitionWrapper, ObjectDefinitionWrapper } from '@aws-amplify/graphql-transformer-core';
6
5
  import { makeConditionFilterInput } from './common';
6
+ import { ModelDirectiveConfiguration } from '../directive';
7
7
 
8
8
  /**
9
9
  * Generate input used for update mutation
@@ -21,7 +21,7 @@ export const makeUpdateInputField = (
21
21
  // sync related things
22
22
  const objectWrapped = new ObjectDefinitionWrapper(obj);
23
23
  const typeName = objectWrapped.name;
24
- const name = toPascalCase([`Update`, typeName, 'Input']);
24
+ const name = toPascalCase(['Update', typeName, 'Input']);
25
25
  const hasIdField = objectWrapped.hasField('id');
26
26
  const fieldsToRemove = objectWrapped
27
27
  .fields!.filter(field => {
@@ -30,9 +30,7 @@ export const makeUpdateInputField = (
30
30
  }
31
31
  return false;
32
32
  })
33
- .map(field => {
34
- return field.name;
35
- });
33
+ .map(field => field.name);
36
34
 
37
35
  const objectTypeDefinition: ObjectTypeDefinitionNode = {
38
36
  ...obj,
@@ -55,14 +53,14 @@ export const makeUpdateInputField = (
55
53
  }
56
54
 
57
55
  // Make createdAt and updatedAt field Optionals if present
58
- for (let timeStampFieldName of Object.values(modelDirectiveConfig?.timestamps || {})) {
56
+ Object.values(modelDirectiveConfig?.timestamps || {}).forEach(timeStampFieldName => {
59
57
  if (input.hasField(timeStampFieldName!)) {
60
58
  const timestampField = input.getField(timeStampFieldName!);
61
59
  if (['String', 'AWSDateTime'].includes(timestampField.getTypeName())) {
62
60
  timestampField.makeNullable();
63
61
  }
64
62
  }
65
- }
63
+ });
66
64
 
67
65
  if (isSyncEnabled) {
68
66
  input.addField(InputFieldWrapper.create('_version', 'Int', true));
@@ -114,9 +112,7 @@ export const makeCreateInputField = (
114
112
  }
115
113
  return false;
116
114
  })
117
- .map(field => {
118
- return field.name;
119
- });
115
+ .map(field => field.name);
120
116
 
121
117
  const objectTypeDefinition: ObjectTypeDefinitionNode = {
122
118
  ...obj,
@@ -135,14 +131,14 @@ export const makeCreateInputField = (
135
131
  }
136
132
  }
137
133
  // Make createdAt and updatedAt field Optionals if present
138
- for (let timeStampFieldName of Object.values(modelDirectiveConfig?.timestamps || {})) {
134
+ Object.values(modelDirectiveConfig?.timestamps || {}).forEach(timeStampFieldName => {
139
135
  if (input.hasField(timeStampFieldName!)) {
140
136
  const timestampField = input.getField(timeStampFieldName!);
141
137
  if (['String', 'AWSDateTime'].includes(timestampField.getTypeName())) {
142
138
  timestampField.makeNullable();
143
139
  }
144
140
  }
145
- }
141
+ });
146
142
 
147
143
  if (isSyncEnabled) {
148
144
  input.addField(InputFieldWrapper.create('_version', 'Int', true));
@@ -151,6 +147,9 @@ export const makeCreateInputField = (
151
147
  return input.serialize();
152
148
  };
153
149
 
150
+ /**
151
+ * makeMutationConditionInput
152
+ */
154
153
  export const makeMutationConditionInput = (
155
154
  ctx: TransformerTransformSchemaStepContextProvider,
156
155
  name: string,
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { ModelTransformer, ModelDirectiveConfiguration, SubscriptionLevel } from './graphql-model-transformer';
1
+ export { ModelDirectiveConfiguration, SubscriptionLevel } from './directive';
2
+ export { ModelTransformer } from './graphql-model-transformer';
2
3
  export { OPERATION_KEY } from './definitions';
3
4
  export * from './graphql-types';
4
5
  export * from './resolvers';
@@ -19,7 +19,7 @@ import {
19
19
  printBlock,
20
20
  } from 'graphql-mapping-template';
21
21
  import { setArgs } from 'graphql-transformer-common';
22
- import { ModelDirectiveConfiguration } from '../graphql-model-transformer';
22
+ import { ModelDirectiveConfiguration } from '../directive';
23
23
  import { generateConditionSlot } from './common';
24
24
 
25
25
  /**
@@ -64,7 +64,7 @@ export const generateUpdateRequestTemplate = (modelName: string, isSyncEnabled:
64
64
  set(ref('keyFields'), list(keyFields)),
65
65
  ),
66
66
 
67
- forEach(ref('entry'), ref(`util.map.copyAndRemoveAllKeys($mergedValues, $keyFields).entrySet()`), [
67
+ forEach(ref('entry'), ref('util.map.copyAndRemoveAllKeys($mergedValues, $keyFields).entrySet()'), [
68
68
  ifElse(
69
69
  raw(
70
70
  '!$util.isNull($ctx.stash.metadata.dynamodbNameOverrideMap) && $ctx.stash.metadata.dynamodbNameOverrideMap.containsKey("$entry.key")',
@@ -75,8 +75,8 @@ export const generateUpdateRequestTemplate = (modelName: string, isSyncEnabled:
75
75
  ifElse(
76
76
  ref('util.isNull($entry.value)'),
77
77
  compoundExpression([
78
- set(ref('discard'), ref(`expRemove.add("#$entryKeyAttributeName")`)),
79
- qref(`$expNames.put("#$entryKeyAttributeName", "$entry.key")`),
78
+ set(ref('discard'), ref('expRemove.add("#$entryKeyAttributeName")')),
79
+ qref('$expNames.put("#$entryKeyAttributeName", "$entry.key")'),
80
80
  ]),
81
81
  compoundExpression([
82
82
  qref('$expSet.put("#$entryKeyAttributeName", ":$entryKeyAttributeName")'),
@@ -152,7 +152,7 @@ export const generateUpdateRequestTemplate = (modelName: string, isSyncEnabled:
152
152
  * Generates VTL template in create mutation
153
153
  * @param modelName Name of the model
154
154
  */
155
- export const generateCreateRequestTemplate = (modelName: string): string => {
155
+ export const generateCreateRequestTemplate = (modelName: string, modelIndexFields: string[]): string => {
156
156
  const statements: Expression[] = [
157
157
  setArgs,
158
158
  // Generate conditions
@@ -163,6 +163,17 @@ export const generateCreateRequestTemplate = (modelName: string): string => {
163
163
  comment('set the typename'),
164
164
  qref(methodCall(ref('mergedValues.put'), str('__typename'), str(modelName))),
165
165
 
166
+ ...(modelIndexFields.length ? [
167
+ set(ref('nullIndexFields'), list([])),
168
+ set(ref('indexFields'), list(modelIndexFields.map(it => str(it)))),
169
+
170
+ forEach(ref('entry'), ref('util.map.copyAndRetainAllKeys($mergedValues, $indexFields).entrySet()'), [
171
+ iff(raw('$util.isNull($entry.value)'), qref(methodCall(ref('nullIndexFields.add'), ref('entry.key')))),
172
+ ]),
173
+
174
+ set(ref('mergedValues'), ref('util.map.copyAndRemoveAllKeys($mergedValues, $nullIndexFields)')),
175
+ ] : []),
176
+
166
177
  // Set PutObject
167
178
  set(
168
179
  ref('PutObject'),
@@ -207,12 +218,11 @@ export const generateCreateRequestTemplate = (modelName: string): string => {
207
218
 
208
219
  /**
209
220
  * Generate mapping template that sets default values for create mutation
210
- * @param name modelName
211
221
  * @param modelConfig directive configuration
212
222
  */
213
- export const generateCreateInitSlotTemplate = (name: string, modelConfig: ModelDirectiveConfiguration): string => {
223
+ export const generateCreateInitSlotTemplate = (modelConfig: ModelDirectiveConfiguration): string => {
214
224
  const statements: Expression[] = [
215
- // initalize defaultVaules
225
+ // initialize defaultValues
216
226
  qref(
217
227
  methodCall(
218
228
  ref('ctx.stash.put'),
@@ -285,12 +295,11 @@ export const generateDeleteRequestTemplate = (isSyncEnabled: boolean): string =>
285
295
 
286
296
  /**
287
297
  * Generate VTL template that sets the default values for Update mutation
288
- * @param modelName Name of the model
289
298
  * @param modelConfig model directive configuration
290
299
  */
291
- export const generateUpdateInitSlotTemplate = (modelName: string, modelConfig: ModelDirectiveConfiguration): string => {
300
+ export const generateUpdateInitSlotTemplate = (modelConfig: ModelDirectiveConfiguration): string => {
292
301
  const statements: Expression[] = [
293
- // initalize defaultVaules
302
+ // initialize defaultValues
294
303
  qref(
295
304
  methodCall(
296
305
  ref('ctx.stash.put'),
@@ -316,14 +325,15 @@ export const generateUpdateInitSlotTemplate = (modelName: string, modelConfig: M
316
325
  return printBlock('Initialization default values')(compoundExpression(statements));
317
326
  };
318
327
 
319
- export function generateApplyDefaultsToInputTemplate(target: string): Expression {
320
- return compoundExpression([
321
- set(ref(target), methodCall(ref('util.defaultIfNull'), ref('ctx.stash.defaultValues'), obj({}))),
322
- qref(methodCall(ref(`${target}.putAll`), methodCall(ref('util.defaultIfNull'), ref('ctx.args.input'), obj({})))),
323
- ]);
324
- }
328
+ /**
329
+ * generateApplyDefaultsToInputTemplate
330
+ */
331
+ export const generateApplyDefaultsToInputTemplate = (target: string): Expression => compoundExpression([
332
+ set(ref(target), methodCall(ref('util.defaultIfNull'), ref('ctx.stash.defaultValues'), obj({}))),
333
+ qref(methodCall(ref(`${target}.putAll`), methodCall(ref('util.defaultIfNull'), ref('ctx.args.input'), obj({})))),
334
+ ]);
325
335
 
326
- function generateKeyConditionTemplate(attributeExistsValue: boolean): Expression[] {
336
+ const generateKeyConditionTemplate = (attributeExistsValue: boolean): Expression[] => {
327
337
  const statements: Expression[] = [
328
338
  comment('Begin - key condition'),
329
339
  ifElse(
@@ -351,4 +361,4 @@ function generateKeyConditionTemplate(attributeExistsValue: boolean): Expression
351
361
  ];
352
362
 
353
363
  return statements;
354
- }
364
+ };