@aws-amplify/data-schema 1.2.2 → 1.2.3

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.
@@ -148,7 +148,6 @@ export function initializeModel(
148
148
  }
149
149
 
150
150
  switch (relationType) {
151
- case connectionType.HAS_ONE:
152
151
  case connectionType.BELONGS_TO: {
153
152
  const sortKeyValues = relatedModelSKFieldNames.reduce(
154
153
  // TODO(Eslint): is this implementation correct?
@@ -180,7 +179,7 @@ export function initializeModel(
180
179
  );
181
180
  }
182
181
 
183
- return undefined;
182
+ return { data: null };
184
183
  };
185
184
  } else {
186
185
  initializedRelationalFields[fieldName] = (
@@ -199,13 +198,29 @@ export function initializeModel(
199
198
  );
200
199
  }
201
200
 
202
- return undefined;
201
+ return { data: null };
203
202
  };
204
203
  }
205
204
 
206
205
  break;
207
206
  }
207
+ case connectionType.HAS_ONE:
208
208
  case connectionType.HAS_MANY: {
209
+ /**
210
+ * If the loader is a HAS_ONE, we just need to attempt to grab the first item
211
+ * from the result.
212
+ */
213
+ const mapResult =
214
+ relationType === connectionType.HAS_ONE
215
+ ? (result: Record<string, any>) => {
216
+ return {
217
+ data: result?.data.shift() || null,
218
+ errors: result.errors,
219
+ extensions: result.extensions,
220
+ };
221
+ }
222
+ : (result: Record<string, any>) => result;
223
+
209
224
  const parentPk = introModel.primaryKeyInfo.primaryKeyFieldName;
210
225
  const parentSK = introModel.primaryKeyInfo.sortKeyFieldNames;
211
226
 
@@ -238,16 +253,15 @@ export function initializeModel(
238
253
  options?: LazyLoadOptions,
239
254
  ) => {
240
255
  if (record[parentPk]) {
241
- return (client as any).models[relatedModelName].list(
242
- contextSpec,
243
- {
256
+ return (client as any).models[relatedModelName]
257
+ .list(contextSpec, {
244
258
  filter: { and: hasManyFilter },
245
259
  limit: options?.limit,
246
260
  nextToken: options?.nextToken,
247
261
  authMode: options?.authMode || authMode,
248
262
  authToken: options?.authToken || authToken,
249
- },
250
- );
263
+ })
264
+ .then(mapResult);
251
265
  }
252
266
 
253
267
  return [];
@@ -257,13 +271,15 @@ export function initializeModel(
257
271
  options?: LazyLoadOptions,
258
272
  ) => {
259
273
  if (record[parentPk]) {
260
- return (client as any).models[relatedModelName].list({
261
- filter: { and: hasManyFilter },
262
- limit: options?.limit,
263
- nextToken: options?.nextToken,
264
- authMode: options?.authMode || authMode,
265
- authToken: options?.authToken || authToken,
266
- });
274
+ return (client as any).models[relatedModelName]
275
+ .list({
276
+ filter: { and: hasManyFilter },
277
+ limit: options?.limit,
278
+ nextToken: options?.nextToken,
279
+ authMode: options?.authMode || authMode,
280
+ authToken: options?.authToken || authToken,
281
+ })
282
+ .then(mapResult);
267
283
  }
268
284
 
269
285
  return [];
@@ -289,16 +305,15 @@ export function initializeModel(
289
305
  options?: LazyLoadOptions,
290
306
  ) => {
291
307
  if (record[parentPk]) {
292
- return (client as any).models[relatedModelName].list(
293
- contextSpec,
294
- {
308
+ return (client as any).models[relatedModelName]
309
+ .list(contextSpec, {
295
310
  filter: { and: hasManyFilter },
296
311
  limit: options?.limit,
297
312
  nextToken: options?.nextToken,
298
313
  authMode: options?.authMode || authMode,
299
314
  authToken: options?.authToken || authToken,
300
- },
301
- );
315
+ })
316
+ .then(mapResult);
302
317
  }
303
318
 
304
319
  return [];
@@ -308,13 +323,15 @@ export function initializeModel(
308
323
  options?: LazyLoadOptions,
309
324
  ) => {
310
325
  if (record[parentPk]) {
311
- return (client as any).models[relatedModelName].list({
312
- filter: { and: hasManyFilter },
313
- limit: options?.limit,
314
- nextToken: options?.nextToken,
315
- authMode: options?.authMode || authMode,
316
- authToken: options?.authToken || authToken,
317
- });
326
+ return (client as any).models[relatedModelName]
327
+ .list({
328
+ filter: { and: hasManyFilter },
329
+ limit: options?.limit,
330
+ nextToken: options?.nextToken,
331
+ authMode: options?.authMode || authMode,
332
+ authToken: options?.authToken || authToken,
333
+ })
334
+ .then(mapResult);
318
335
  }
319
336
 
320
337
  return [];
@@ -121,7 +121,9 @@ async function _get(
121
121
  const [key] = Object.keys(data);
122
122
  const flattenedResult = flattenItems(data)[key];
123
123
 
124
- if (options?.selectionSet) {
124
+ if (flattenedResult === null) {
125
+ return { data: null, extensions };
126
+ } else if (options?.selectionSet) {
125
127
  return { data: flattenedResult, extensions };
126
128
  } else {
127
129
  // TODO: refactor to avoid destructuring here