@aws-amplify/api-graphql 4.0.1-api-v6-models.3eb7748.0 → 4.0.1-api-v6-models.9493d4d.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/api-graphql",
3
- "version": "4.0.1-api-v6-models.3eb7748.0+3eb7748",
3
+ "version": "4.0.1-api-v6-models.9493d4d.0+9493d4d",
4
4
  "description": "Api-graphql category of aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -52,9 +52,9 @@
52
52
  ],
53
53
  "dependencies": {
54
54
  "@aws-amplify/amplify-api-next-types-alpha": "^0.2.0",
55
- "@aws-amplify/api-rest": "4.0.1-api-v6-models.3eb7748.0+3eb7748",
56
- "@aws-amplify/auth": "6.0.1-api-v6-models.3eb7748.0+3eb7748",
57
- "@aws-amplify/core": "6.0.1-api-v6-models.3eb7748.0+3eb7748",
55
+ "@aws-amplify/api-rest": "4.0.1-api-v6-models.9493d4d.0+9493d4d",
56
+ "@aws-amplify/auth": "6.0.1-api-v6-models.9493d4d.0+9493d4d",
57
+ "@aws-amplify/core": "6.0.1-api-v6-models.9493d4d.0+9493d4d",
58
58
  "@aws-sdk/types": "3.387.0",
59
59
  "graphql": "15.8.0",
60
60
  "rxjs": "^7.8.1",
@@ -126,5 +126,5 @@
126
126
  "<rootDir>/__tests__"
127
127
  ]
128
128
  },
129
- "gitHead": "3eb7748aee2fcdea705c93904d1f30ff596649fa"
129
+ "gitHead": "9493d4d9a9fb9bfa104c0cf56e5a1d3154daa088"
130
130
  }
@@ -164,7 +164,7 @@ type OperationPrefix =
164
164
  (typeof graphQLOperationsInfo)[ModelOperation]['operationPrefix'];
165
165
 
166
166
  const graphQLDocumentsCache = new Map<string, Map<ModelOperation, string>>();
167
- const SELECTION_SET_ALL_NESTED = '*';
167
+ const SELECTION_SET_WILDCARD = '*';
168
168
 
169
169
  function defaultSelectionSetForModel(modelDefinition: any): string[] {
170
170
  // fields that are explicitly part of the graphql schema; not
@@ -208,78 +208,70 @@ export function customSelectionSetToIR(
208
208
  selectionSet: string[]
209
209
  ): Record<string, string | object> {
210
210
  const modelDefinition = modelIntrospection[modelName];
211
- const { fields } = modelDefinition;
212
-
213
- const intermediateSelectionSet: Record<string, string | object> = {};
211
+ const { fields: modelFields } = modelDefinition;
214
212
 
215
- for (const f of selectionSet) {
216
- const nested = f.includes('.');
213
+ return selectionSet.reduce((resultObj, path) => {
214
+ const [fieldName, nested, ...rest] = path.split('.');
217
215
 
218
216
  if (nested) {
219
- const [modelFieldName, selectedField] = f.split('.');
220
-
221
- const relatedModel = fields[modelFieldName]?.type?.model;
217
+ const relatedModel = modelFields[fieldName]?.type?.model;
222
218
 
223
219
  if (!relatedModel) {
224
220
  // TODO: may need to change this to support custom types
225
- throw Error(`${modelFieldName} is not a model field`);
221
+ throw Error(`${fieldName} is not a model field`);
226
222
  }
227
223
 
228
- if (selectedField === SELECTION_SET_ALL_NESTED) {
229
- const relatedModelDefinition = modelIntrospection[relatedModel];
230
-
231
- const defaultSelectionSet = defaultSelectionSetForModel(
232
- relatedModelDefinition
233
- );
224
+ const relatedModelDefinition = modelIntrospection[relatedModel];
234
225
 
235
- const reduced = defaultSelectionSet.reduce((acc, curVal) => {
236
- acc[curVal] = FIELD_IR;
237
- return acc;
238
- }, {});
226
+ const selectionSet =
227
+ nested === SELECTION_SET_WILDCARD
228
+ ? defaultSelectionSetIR(relatedModelDefinition)
229
+ : // if we have a path like 'field.anotherField' recursively build up selection set IR
230
+ customSelectionSetToIR(modelIntrospection, relatedModel, [
231
+ [nested, ...rest].join('.'),
232
+ ]);
239
233
 
240
- if (fields[modelFieldName]?.isArray) {
241
- intermediateSelectionSet[modelFieldName] = {
242
- items: reduced,
243
- };
244
- } else {
245
- intermediateSelectionSet[modelFieldName] = reduced;
246
- }
247
- } else {
248
- const getNestedSelSet = customSelectionSetToIR(
249
- modelIntrospection,
250
- relatedModel,
251
- [selectedField]
252
- );
234
+ if (modelFields[fieldName]?.isArray) {
235
+ const existing = resultObj[fieldName] || {
236
+ items: {},
237
+ };
238
+ const merged = { ...existing.items, ...selectionSet };
253
239
 
254
- if (fields[modelFieldName]?.isArray) {
255
- const existing = (intermediateSelectionSet as any)[
256
- modelFieldName
257
- ] || { items: {} };
258
- const merged = { ...existing.items, ...getNestedSelSet };
240
+ resultObj[fieldName] = { items: merged };
241
+ return resultObj;
242
+ }
259
243
 
260
- intermediateSelectionSet[modelFieldName] = { items: merged };
261
- } else {
262
- const existingItems =
263
- (intermediateSelectionSet as any)[modelFieldName] || {};
264
- const merged = { ...existingItems, ...getNestedSelSet };
244
+ const existingItems = resultObj[fieldName] || {};
245
+ const merged = { ...existingItems, ...selectionSet };
265
246
 
266
- intermediateSelectionSet[modelFieldName] = merged;
267
- }
268
- }
269
- } else {
270
- const exists = Boolean(fields[f]);
247
+ resultObj[fieldName] = merged;
248
+ return resultObj;
249
+ }
271
250
 
272
- if (!exists) {
273
- throw Error(`${f} is not a field of model ${modelName}`);
274
- }
251
+ const exists = Boolean(modelFields[fieldName]);
275
252
 
276
- intermediateSelectionSet[f] = FIELD_IR;
253
+ if (!exists) {
254
+ throw Error(`${fieldName} is not a field of model ${modelName}`);
277
255
  }
278
- }
279
256
 
280
- return intermediateSelectionSet;
257
+ resultObj[fieldName] = FIELD_IR;
258
+ return resultObj;
259
+ }, {});
281
260
  }
282
261
 
262
+ const defaultSelectionSetIR = relatedModelDefinition => {
263
+ const defaultSelectionSet = defaultSelectionSetForModel(
264
+ relatedModelDefinition
265
+ );
266
+
267
+ const reduced = defaultSelectionSet.reduce((acc, curVal) => {
268
+ acc[curVal] = FIELD_IR;
269
+ return acc;
270
+ }, {});
271
+
272
+ return reduced;
273
+ };
274
+
283
275
  /**
284
276
  * Stringifies selection set IR
285
277
  * * @example