@aws-amplify/api-graphql 4.0.29-unstable.2a7b3ac.0 → 4.0.29-unstable.f37faeb.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.
@@ -318,10 +318,10 @@ export type ModelOperation = keyof typeof graphQLOperationsInfo;
318
318
 
319
319
  const SELECTION_SET_WILDCARD = '*';
320
320
 
321
- export function defaultSelectionSetForNonModelWithIR(
321
+ export const getDefaultSelectionSetForNonModelWithIR = (
322
322
  nonModelDefinition: SchemaNonModel,
323
323
  modelIntrospection: ModelIntrospectionSchema,
324
- ): Record<string, unknown> {
324
+ ): Record<string, unknown> => {
325
325
  const { fields } = nonModelDefinition;
326
326
  const mappedFields = Object.values(fields)
327
327
  .map(({ type, name }) => {
@@ -332,7 +332,7 @@ export function defaultSelectionSetForNonModelWithIR(
332
332
  if (typeof (type as NonModelFieldType).nonModel === 'string') {
333
333
  return [
334
334
  name,
335
- defaultSelectionSetForNonModelWithIR(
335
+ getDefaultSelectionSetForNonModelWithIR(
336
336
  modelIntrospection.nonModels[(type as NonModelFieldType).nonModel],
337
337
  modelIntrospection,
338
338
  ),
@@ -352,7 +352,47 @@ export function defaultSelectionSetForNonModelWithIR(
352
352
  );
353
353
 
354
354
  return Object.fromEntries(mappedFields);
355
- }
355
+ };
356
+
357
+ const getDefaultSelectionSetForModelWithIR = (
358
+ modelDefinition: SchemaModel,
359
+ modelIntrospection: ModelIntrospectionSchema,
360
+ ): Record<string, unknown> => {
361
+ const { fields } = modelDefinition;
362
+ const mappedFields = Object.values(fields)
363
+ .map(({ type, name }) => {
364
+ if (
365
+ typeof (type as { enum: string }).enum === 'string' ||
366
+ typeof type === 'string'
367
+ ) {
368
+ return [name, FIELD_IR];
369
+ }
370
+
371
+ if (typeof (type as NonModelFieldType).nonModel === 'string') {
372
+ return [
373
+ name,
374
+ getDefaultSelectionSetForNonModelWithIR(
375
+ modelIntrospection.nonModels[(type as NonModelFieldType).nonModel],
376
+ modelIntrospection,
377
+ ),
378
+ ];
379
+ }
380
+
381
+ return undefined;
382
+ })
383
+ .filter(
384
+ (
385
+ pair: (string | Record<string, unknown>)[] | undefined,
386
+ ): pair is (string | Record<string, unknown>)[] => pair !== undefined,
387
+ );
388
+
389
+ const ownerFields = resolveOwnerFields(modelDefinition).map(field => [
390
+ field,
391
+ FIELD_IR,
392
+ ]);
393
+
394
+ return Object.fromEntries(mappedFields.concat(ownerFields));
395
+ };
356
396
 
357
397
  function defaultSelectionSetForModel(modelDefinition: SchemaModel): string[] {
358
398
  // fields that are explicitly part of the graphql schema; not
@@ -445,7 +485,7 @@ export function customSelectionSetToIR(
445
485
 
446
486
  if (nested === SELECTION_SET_WILDCARD) {
447
487
  result = {
448
- [fieldName]: defaultSelectionSetForNonModelWithIR(
488
+ [fieldName]: getDefaultSelectionSetForNonModelWithIR(
449
489
  relatedNonModelDefinition,
450
490
  modelIntrospection,
451
491
  ),
@@ -471,8 +511,9 @@ export function customSelectionSetToIR(
471
511
  modelIntrospection.models[relatedModel];
472
512
 
473
513
  result = {
474
- [fieldName]: modelsDefaultSelectionSetIR(
514
+ [fieldName]: getDefaultSelectionSetForModelWithIR(
475
515
  nestedRelatedModelDefinition,
516
+ modelIntrospection,
476
517
  ),
477
518
  };
478
519
  } else {
@@ -529,23 +570,6 @@ export function customSelectionSetToIR(
529
570
  );
530
571
  }
531
572
 
532
- const modelsDefaultSelectionSetIR = (relatedModelDefinition: SchemaModel) => {
533
- const defaultSelectionSet = defaultSelectionSetForModel(
534
- relatedModelDefinition,
535
- );
536
-
537
- const reduced = defaultSelectionSet.reduce(
538
- (acc: Record<string, any>, curVal) => {
539
- acc[curVal] = FIELD_IR;
540
-
541
- return acc;
542
- },
543
- {},
544
- );
545
-
546
- return reduced;
547
- };
548
-
549
573
  /**
550
574
  * Stringifies selection set IR
551
575
  * * @example
@@ -697,6 +721,45 @@ export function generateGraphQLDocument(
697
721
  selectionSet as ListArgs['selectionSet'],
698
722
  );
699
723
 
724
+ // default PK args for get and list operations
725
+ // modified below for CPK
726
+ const getPkArgs = {
727
+ [primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}!`,
728
+ };
729
+ const listPkArgs = {};
730
+
731
+ const generateSkArgs = (op: 'get' | 'list') => {
732
+ return sortKeyFieldNames.reduce(
733
+ (acc: Record<string, any>, fieldName: string) => {
734
+ const fieldType = fields[fieldName].type;
735
+
736
+ if (op === 'get') {
737
+ acc[fieldName] = `${fieldType}!`;
738
+ } else if (op === 'list') {
739
+ acc[fieldName] = `Model${fieldType}KeyConditionInput`;
740
+ }
741
+
742
+ return acc;
743
+ },
744
+ {},
745
+ );
746
+ };
747
+
748
+ if (isCustomPrimaryKey) {
749
+ Object.assign(getPkArgs, generateSkArgs('get'));
750
+
751
+ Object.assign(
752
+ listPkArgs,
753
+ {
754
+ // PK is only included in list query field args in the generated GQL
755
+ // when explicitly specifying PK with .identifier(['fieldName']) or @primaryKey in the schema definition
756
+ [primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}`, // PK is always a nullable arg for list (no `!` after the type)
757
+ sortDirection: 'ModelSortDirection',
758
+ },
759
+ generateSkArgs('list'),
760
+ );
761
+ }
762
+
700
763
  switch (modelOperation) {
701
764
  case 'CREATE':
702
765
  case 'UPDATE':
@@ -712,36 +775,15 @@ export function generateGraphQLDocument(
712
775
  // TODO(Eslint): this this case clause correct without the break statement?
713
776
  // eslint-disable-next-line no-fallthrough
714
777
  case 'READ':
715
- graphQLArguments ??
716
- (graphQLArguments = isCustomPrimaryKey
717
- ? [primaryKeyFieldName, ...sortKeyFieldNames].reduce(
718
- (acc: Record<string, any>, fieldName) => {
719
- acc[fieldName] = `${fields[fieldName].type}!`;
720
-
721
- return acc;
722
- },
723
- {},
724
- )
725
- : {
726
- [primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}!`,
727
- });
778
+ graphQLArguments ?? (graphQLArguments = getPkArgs);
728
779
  graphQLSelectionSet ?? (graphQLSelectionSet = selectionSetFields);
729
780
  // TODO(Eslint): this this case clause correct without the break statement?
730
781
  // eslint-disable-next-line no-fallthrough
731
782
  case 'LIST':
732
783
  graphQLArguments ??
733
784
  (graphQLArguments = {
785
+ ...listPkArgs,
734
786
  filter: `Model${name}FilterInput`,
735
- ...(sortKeyFieldNames.length > 0
736
- ? [primaryKeyFieldName, ...sortKeyFieldNames].reduce(
737
- (acc: Record<string, any>, fieldName) => {
738
- acc[fieldName] = `${fields[fieldName].type}`;
739
-
740
- return acc;
741
- },
742
- { sortDirection: 'ModelSortDirection' },
743
- )
744
- : []),
745
787
  limit: 'Int',
746
788
  nextToken: 'String',
747
789
  });
@@ -9,10 +9,10 @@ import { map } from 'rxjs';
9
9
 
10
10
  import {
11
11
  authModeParams,
12
- defaultSelectionSetForNonModelWithIR,
13
12
  flattenItems,
14
13
  generateSelectionSet,
15
14
  getCustomHeaders,
15
+ getDefaultSelectionSetForNonModelWithIR,
16
16
  initializeModel,
17
17
  selectionSetIRToString,
18
18
  } from '../APIClient';
@@ -290,7 +290,7 @@ function operationSelectionSet(
290
290
  const nonModel = modelIntrospection.nonModels[operation.type.nonModel];
291
291
 
292
292
  return `{${selectionSetIRToString(
293
- defaultSelectionSetForNonModelWithIR(nonModel, modelIntrospection),
293
+ getDefaultSelectionSetForNonModelWithIR(nonModel, modelIntrospection),
294
294
  )}}`;
295
295
  } else if (hasStringField(operation.type, 'model')) {
296
296
  return `{${generateSelectionSet(modelIntrospection, operation.type.model)}}`;