@aws-amplify/api-graphql 4.0.16-unstable.5e30055.0 → 4.0.16

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 (29) hide show
  1. package/dist/cjs/internals/APIClient.js +54 -4
  2. package/dist/cjs/internals/APIClient.js.map +1 -1
  3. package/dist/cjs/internals/clientUtils.js +29 -0
  4. package/dist/cjs/internals/clientUtils.js.map +1 -0
  5. package/dist/cjs/internals/generateModelsProperty.js +6 -0
  6. package/dist/cjs/internals/generateModelsProperty.js.map +1 -1
  7. package/dist/cjs/internals/operations/indexQuery.js +75 -0
  8. package/dist/cjs/internals/operations/indexQuery.js.map +1 -0
  9. package/dist/cjs/internals/server/generateModelsProperty.js +6 -0
  10. package/dist/cjs/internals/server/generateModelsProperty.js.map +1 -1
  11. package/dist/esm/internals/APIClient.d.ts +34 -29
  12. package/dist/esm/internals/APIClient.mjs +54 -4
  13. package/dist/esm/internals/APIClient.mjs.map +1 -1
  14. package/dist/esm/internals/clientUtils.d.ts +6 -0
  15. package/dist/esm/internals/clientUtils.mjs +26 -0
  16. package/dist/esm/internals/clientUtils.mjs.map +1 -0
  17. package/dist/esm/internals/generateModelsProperty.mjs +6 -0
  18. package/dist/esm/internals/generateModelsProperty.mjs.map +1 -1
  19. package/dist/esm/internals/operations/indexQuery.d.ts +9 -0
  20. package/dist/esm/internals/operations/indexQuery.mjs +73 -0
  21. package/dist/esm/internals/operations/indexQuery.mjs.map +1 -0
  22. package/dist/esm/internals/server/generateModelsProperty.mjs +6 -0
  23. package/dist/esm/internals/server/generateModelsProperty.mjs.map +1 -1
  24. package/package.json +6 -6
  25. package/src/internals/APIClient.ts +116 -51
  26. package/src/internals/clientUtils.ts +37 -0
  27. package/src/internals/generateModelsProperty.ts +17 -6
  28. package/src/internals/operations/indexQuery.ts +169 -0
  29. package/src/internals/server/generateModelsProperty.ts +16 -3
@@ -1 +1 @@
1
- {"version":3,"file":"generateModelsProperty.mjs","sources":["../../../../src/internals/server/generateModelsProperty.ts"],"sourcesContent":["import { graphQLOperationsInfo } from '../APIClient';\nimport { listFactory } from '../operations/list';\nimport { getFactory } from '../operations/get';\nexport function generateModelsProperty(client, params) {\n const models = {};\n const config = params.config;\n const useContext = params.amplify === null;\n if (!config) {\n throw new Error('generateModelsProperty cannot retrieve Amplify config');\n }\n if (!config.API?.GraphQL) {\n return {};\n }\n const modelIntrospection = config.API.GraphQL.modelIntrospection;\n if (!modelIntrospection) {\n return {};\n }\n const SSR_UNSUPORTED_OPS = [\n 'ONCREATE',\n 'ONUPDATE',\n 'ONDELETE',\n 'OBSERVE_QUERY',\n ];\n for (const model of Object.values(modelIntrospection.models)) {\n const { name } = model;\n models[name] = {};\n Object.entries(graphQLOperationsInfo).forEach(([key, { operationPrefix }]) => {\n const operation = key;\n // subscriptions are not supported in SSR\n if (SSR_UNSUPORTED_OPS.includes(operation))\n return;\n if (operation === 'LIST') {\n models[name][operationPrefix] = listFactory(client, modelIntrospection, model, useContext);\n }\n else {\n models[name][operationPrefix] = getFactory(client, modelIntrospection, model, operation, useContext);\n }\n });\n }\n return models;\n}\n"],"names":[],"mappings":";;;;AAGO,SAAS,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE;AACvD,IAAI,MAAM,MAAM,GAAG,EAAE,CAAC;AACtB,IAAI,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;AACjC,IAAI,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,KAAK,IAAI,CAAC;AAC/C,IAAI,IAAI,CAAC,MAAM,EAAE;AACjB,QAAQ,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;AACjF,KAAK;AACL,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE;AAC9B,QAAQ,OAAO,EAAE,CAAC;AAClB,KAAK;AACL,IAAI,MAAM,kBAAkB,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,kBAAkB,CAAC;AACrE,IAAI,IAAI,CAAC,kBAAkB,EAAE;AAC7B,QAAQ,OAAO,EAAE,CAAC;AAClB,KAAK;AACL,IAAI,MAAM,kBAAkB,GAAG;AAC/B,QAAQ,UAAU;AAClB,QAAQ,UAAU;AAClB,QAAQ,UAAU;AAClB,QAAQ,eAAe;AACvB,KAAK,CAAC;AACN,IAAI,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE;AAClE,QAAQ,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;AAC/B,QAAQ,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;AAC1B,QAAQ,MAAM,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,EAAE,eAAe,EAAE,CAAC,KAAK;AACtF,YAAY,MAAM,SAAS,GAAG,GAAG,CAAC;AAClC;AACA,YAAY,IAAI,kBAAkB,CAAC,QAAQ,CAAC,SAAS,CAAC;AACtD,gBAAgB,OAAO;AACvB,YAAY,IAAI,SAAS,KAAK,MAAM,EAAE;AACtC,gBAAgB,MAAM,CAAC,IAAI,CAAC,CAAC,eAAe,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,kBAAkB,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;AAC3G,aAAa;AACb,iBAAiB;AACjB,gBAAgB,MAAM,CAAC,IAAI,CAAC,CAAC,eAAe,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,kBAAkB,EAAE,KAAK,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC;AACrH,aAAa;AACb,SAAS,CAAC,CAAC;AACX,KAAK;AACL,IAAI,OAAO,MAAM,CAAC;AAClB;;;;"}
1
+ {"version":3,"file":"generateModelsProperty.mjs","sources":["../../../../src/internals/server/generateModelsProperty.ts"],"sourcesContent":["import { graphQLOperationsInfo } from '../APIClient';\nimport { listFactory } from '../operations/list';\nimport { indexQueryFactory } from '../operations/indexQuery';\nimport { getFactory } from '../operations/get';\nimport { getSecondaryIndexesFromSchemaModel } from '../clientUtils';\nexport function generateModelsProperty(client, params) {\n const models = {};\n const config = params.config;\n const useContext = params.amplify === null;\n if (!config) {\n throw new Error('generateModelsProperty cannot retrieve Amplify config');\n }\n if (!config.API?.GraphQL) {\n return {};\n }\n const modelIntrospection = config.API.GraphQL.modelIntrospection;\n if (!modelIntrospection) {\n return {};\n }\n const SSR_UNSUPORTED_OPS = [\n 'ONCREATE',\n 'ONUPDATE',\n 'ONDELETE',\n 'OBSERVE_QUERY',\n ];\n for (const model of Object.values(modelIntrospection.models)) {\n const { name } = model;\n models[name] = {};\n Object.entries(graphQLOperationsInfo).forEach(([key, { operationPrefix }]) => {\n const operation = key;\n // subscriptions are not supported in SSR\n if (SSR_UNSUPORTED_OPS.includes(operation))\n return;\n if (operation === 'LIST') {\n models[name][operationPrefix] = listFactory(client, modelIntrospection, model, useContext);\n }\n else {\n models[name][operationPrefix] = getFactory(client, modelIntrospection, model, operation, useContext);\n }\n });\n const secondaryIdxs = getSecondaryIndexesFromSchemaModel(model);\n for (const idx of secondaryIdxs) {\n models[name][idx.queryField] = indexQueryFactory(client, modelIntrospection, model, idx);\n }\n }\n return models;\n}\n"],"names":[],"mappings":";;;;;;AAKO,SAAS,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE;AACvD,IAAI,MAAM,MAAM,GAAG,EAAE,CAAC;AACtB,IAAI,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;AACjC,IAAI,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,KAAK,IAAI,CAAC;AAC/C,IAAI,IAAI,CAAC,MAAM,EAAE;AACjB,QAAQ,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;AACjF,KAAK;AACL,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE;AAC9B,QAAQ,OAAO,EAAE,CAAC;AAClB,KAAK;AACL,IAAI,MAAM,kBAAkB,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,kBAAkB,CAAC;AACrE,IAAI,IAAI,CAAC,kBAAkB,EAAE;AAC7B,QAAQ,OAAO,EAAE,CAAC;AAClB,KAAK;AACL,IAAI,MAAM,kBAAkB,GAAG;AAC/B,QAAQ,UAAU;AAClB,QAAQ,UAAU;AAClB,QAAQ,UAAU;AAClB,QAAQ,eAAe;AACvB,KAAK,CAAC;AACN,IAAI,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE;AAClE,QAAQ,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;AAC/B,QAAQ,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;AAC1B,QAAQ,MAAM,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,EAAE,eAAe,EAAE,CAAC,KAAK;AACtF,YAAY,MAAM,SAAS,GAAG,GAAG,CAAC;AAClC;AACA,YAAY,IAAI,kBAAkB,CAAC,QAAQ,CAAC,SAAS,CAAC;AACtD,gBAAgB,OAAO;AACvB,YAAY,IAAI,SAAS,KAAK,MAAM,EAAE;AACtC,gBAAgB,MAAM,CAAC,IAAI,CAAC,CAAC,eAAe,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,kBAAkB,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;AAC3G,aAAa;AACb,iBAAiB;AACjB,gBAAgB,MAAM,CAAC,IAAI,CAAC,CAAC,eAAe,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,kBAAkB,EAAE,KAAK,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC;AACrH,aAAa;AACb,SAAS,CAAC,CAAC;AACX,QAAQ,MAAM,aAAa,GAAG,kCAAkC,CAAC,KAAK,CAAC,CAAC;AACxE,QAAQ,KAAK,MAAM,GAAG,IAAI,aAAa,EAAE;AACzC,YAAY,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,iBAAiB,CAAC,MAAM,EAAE,kBAAkB,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;AACrG,SAAS;AACT,KAAK;AACL,IAAI,OAAO,MAAM,CAAC;AAClB;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/api-graphql",
3
- "version": "4.0.16-unstable.5e30055.0+5e30055",
3
+ "version": "4.0.16",
4
4
  "description": "Api-graphql category of aws-amplify",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.mjs",
@@ -71,7 +71,7 @@
71
71
  },
72
72
  "homepage": "https://aws-amplify.github.io/",
73
73
  "devDependencies": {
74
- "@aws-amplify/data-schema": "^0.12.11",
74
+ "@aws-amplify/data-schema": "^0.13.2",
75
75
  "@rollup/plugin-typescript": "11.1.5",
76
76
  "rollup": "^4.9.6",
77
77
  "typescript": "5.0.2"
@@ -84,9 +84,9 @@
84
84
  "server"
85
85
  ],
86
86
  "dependencies": {
87
- "@aws-amplify/api-rest": "4.0.16-unstable.5e30055.0+5e30055",
88
- "@aws-amplify/core": "6.0.16-unstable.5e30055.0+5e30055",
89
- "@aws-amplify/data-schema-types": "^0.6.10",
87
+ "@aws-amplify/api-rest": "4.0.16",
88
+ "@aws-amplify/core": "6.0.16",
89
+ "@aws-amplify/data-schema-types": "^0.7.1",
90
90
  "@aws-sdk/types": "3.387.0",
91
91
  "graphql": "15.8.0",
92
92
  "rxjs": "^7.8.1",
@@ -101,5 +101,5 @@
101
101
  "limit": "91.7 kB"
102
102
  }
103
103
  ],
104
- "gitHead": "5e30055971598cb4972a5c913af906e86c16cd3f"
104
+ "gitHead": "0e919f6e759cb94d03e119162b530b32d48f1357"
105
105
  }
@@ -24,6 +24,7 @@ import {
24
24
  } from '../types';
25
25
  import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
26
26
  import { CustomHeaders } from '@aws-amplify/data-schema-types';
27
+ import type { IndexMeta } from './operations/indexQuery';
27
28
 
28
29
  type LazyLoadOptions = {
29
30
  authMode?: GraphQLAuthMode;
@@ -51,7 +52,7 @@ export const flattenItems = (obj: Record<string, any>): Record<string, any> => {
51
52
  if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
52
53
  if (value.items !== undefined) {
53
54
  res[prop] = value.items.map((item: Record<string, any>) =>
54
- flattenItems(item)
55
+ flattenItems(item),
55
56
  );
56
57
  return;
57
58
  }
@@ -73,7 +74,7 @@ export function initializeModel(
73
74
  modelIntrospection: ModelIntrospectionSchema,
74
75
  authMode: GraphQLAuthMode | undefined,
75
76
  authToken: string | undefined,
76
- context = false
77
+ context = false,
77
78
  ): any[] {
78
79
  const introModel = modelIntrospection.models[modelName];
79
80
  const introModelFields = introModel.fields;
@@ -122,13 +123,13 @@ export function initializeModel(
122
123
  return (acc[curVal] = record[curVal]);
123
124
  }
124
125
  },
125
- {}
126
+ {},
126
127
  );
127
128
 
128
129
  if (context) {
129
130
  initializedRelationalFields[fieldName] = (
130
131
  contextSpec: AmplifyServer.ContextSpec,
131
- options?: LazyLoadOptions
132
+ options?: LazyLoadOptions,
132
133
  ) => {
133
134
  if (record[targetNames[0]]) {
134
135
  return (
@@ -142,14 +143,14 @@ export function initializeModel(
142
143
  {
143
144
  authMode: options?.authMode || authMode,
144
145
  authToken: options?.authToken || authToken,
145
- }
146
+ },
146
147
  );
147
148
  }
148
149
  return undefined;
149
150
  };
150
151
  } else {
151
152
  initializedRelationalFields[fieldName] = (
152
- options?: LazyLoadOptions
153
+ options?: LazyLoadOptions,
153
154
  ) => {
154
155
  if (record[targetNames[0]]) {
155
156
  return (client as V6Client<Record<string, any>>).models[
@@ -162,7 +163,7 @@ export function initializeModel(
162
163
  {
163
164
  authMode: options?.authMode || authMode,
164
165
  authToken: options?.authToken || authToken,
165
- }
166
+ },
166
167
  );
167
168
  }
168
169
  return undefined;
@@ -194,13 +195,13 @@ export function initializeModel(
194
195
  }
195
196
 
196
197
  return { [field]: { eq: record[parentSK[idx - 1]] } };
197
- }
198
+ },
198
199
  );
199
200
 
200
201
  if (context) {
201
202
  initializedRelationalFields[fieldName] = (
202
203
  contextSpec: AmplifyServer.ContextSpec,
203
- options?: LazyLoadOptions
204
+ options?: LazyLoadOptions,
204
205
  ) => {
205
206
  if (record[parentPk]) {
206
207
  return (
@@ -217,7 +218,7 @@ export function initializeModel(
217
218
  };
218
219
  } else {
219
220
  initializedRelationalFields[fieldName] = (
220
- options?: LazyLoadOptions
221
+ options?: LazyLoadOptions,
221
222
  ) => {
222
223
  if (record[parentPk]) {
223
224
  return (client as V6Client<Record<string, any>>).models[
@@ -244,13 +245,13 @@ export function initializeModel(
244
245
  }
245
246
 
246
247
  return { [field]: { eq: record[parentSK[idx - 1]] } };
247
- }
248
+ },
248
249
  );
249
250
 
250
251
  if (context) {
251
252
  initializedRelationalFields[fieldName] = (
252
253
  contextSpec: AmplifyServer.ContextSpec,
253
- options?: LazyLoadOptions
254
+ options?: LazyLoadOptions,
254
255
  ) => {
255
256
  if (record[parentPk]) {
256
257
  return (
@@ -267,7 +268,7 @@ export function initializeModel(
267
268
  };
268
269
  } else {
269
270
  initializedRelationalFields[fieldName] = (
270
- options?: LazyLoadOptions
271
+ options?: LazyLoadOptions,
271
272
  ) => {
272
273
  if (record[parentPk]) {
273
274
  return (client as V6Client<Record<string, any>>).models[
@@ -295,16 +296,17 @@ export function initializeModel(
295
296
  }
296
297
 
297
298
  export const graphQLOperationsInfo = {
298
- CREATE: { operationPrefix: 'create' as const, usePlural: false },
299
- READ: { operationPrefix: 'get' as const, usePlural: false },
300
- UPDATE: { operationPrefix: 'update' as const, usePlural: false },
301
- DELETE: { operationPrefix: 'delete' as const, usePlural: false },
302
- LIST: { operationPrefix: 'list' as const, usePlural: true },
303
- ONCREATE: { operationPrefix: 'onCreate' as const, usePlural: false },
304
- ONUPDATE: { operationPrefix: 'onUpdate' as const, usePlural: false },
305
- ONDELETE: { operationPrefix: 'onDelete' as const, usePlural: false },
306
- OBSERVE_QUERY: { operationPrefix: 'observeQuery' as const, usePlural: false },
307
- };
299
+ CREATE: { operationPrefix: 'create', usePlural: false },
300
+ READ: { operationPrefix: 'get', usePlural: false },
301
+ UPDATE: { operationPrefix: 'update', usePlural: false },
302
+ DELETE: { operationPrefix: 'delete', usePlural: false },
303
+ LIST: { operationPrefix: 'list', usePlural: true },
304
+ INDEX_QUERY: { operationPrefix: '', usePlural: false },
305
+ ONCREATE: { operationPrefix: 'onCreate', usePlural: false },
306
+ ONUPDATE: { operationPrefix: 'onUpdate', usePlural: false },
307
+ ONDELETE: { operationPrefix: 'onDelete', usePlural: false },
308
+ OBSERVE_QUERY: { operationPrefix: 'observeQuery', usePlural: false },
309
+ } as const;
308
310
  export type ModelOperation = keyof typeof graphQLOperationsInfo;
309
311
 
310
312
  type OperationPrefix =
@@ -322,7 +324,7 @@ function defaultSelectionSetForModel(modelDefinition: SchemaModel): string[] {
322
324
  ({ type, name }) =>
323
325
  (typeof type === 'string' ||
324
326
  (typeof type === 'object' && typeof type?.enum === 'string')) &&
325
- name
327
+ name,
326
328
  )
327
329
  .filter(Boolean);
328
330
 
@@ -357,11 +359,11 @@ const FIELD_IR = '';
357
359
  export function customSelectionSetToIR(
358
360
  modelDefinitions: SchemaModels,
359
361
  modelName: string,
360
- selectionSet: string[]
362
+ selectionSet: string[],
361
363
  ): Record<string, string | object> {
362
364
  const dotNotationToObject = (
363
365
  path: string,
364
- modelName: string
366
+ modelName: string,
365
367
  ): Record<string, any> => {
366
368
  const [fieldName, ...rest] = path.split('.');
367
369
 
@@ -373,7 +375,8 @@ export function customSelectionSetToIR(
373
375
  const nested = rest[0];
374
376
  const modelDefinition = modelDefinitions[modelName];
375
377
  const modelFields = modelDefinition.fields;
376
- const relatedModel = (modelFields[fieldName]?.type as ModelFieldType)?.model;
378
+ const relatedModel = (modelFields[fieldName]?.type as ModelFieldType)
379
+ ?.model;
377
380
 
378
381
  if (!relatedModel) {
379
382
  // TODO: may need to change this to support custom types
@@ -413,15 +416,15 @@ export function customSelectionSetToIR(
413
416
  (resultObj, path) =>
414
417
  deepMergeSelectionSetObjects(
415
418
  dotNotationToObject(path, modelName),
416
- resultObj
419
+ resultObj,
417
420
  ),
418
- {} as Record<string, any>
421
+ {} as Record<string, any>,
419
422
  );
420
423
  }
421
424
 
422
425
  const defaultSelectionSetIR = (relatedModelDefinition: SchemaModel) => {
423
426
  const defaultSelectionSet = defaultSelectionSetForModel(
424
- relatedModelDefinition
427
+ relatedModelDefinition,
425
428
  );
426
429
 
427
430
  const reduced = defaultSelectionSet.reduce(
@@ -429,7 +432,7 @@ const defaultSelectionSetIR = (relatedModelDefinition: SchemaModel) => {
429
432
  acc[curVal] = FIELD_IR;
430
433
  return acc;
431
434
  },
432
- {}
435
+ {},
433
436
  );
434
437
 
435
438
  return reduced;
@@ -451,7 +454,7 @@ const defaultSelectionSetIR = (relatedModelDefinition: SchemaModel) => {
451
454
  * `'id comments { items { post { id } } }'`
452
455
  */
453
456
  export function selectionSetIRToString(
454
- obj: Record<string, string | any>
457
+ obj: Record<string, string | any>,
455
458
  ): string {
456
459
  const res: string[] = [];
457
460
 
@@ -467,7 +470,7 @@ export function selectionSetIRToString(
467
470
  '{',
468
471
  selectionSetIRToString(value.items),
469
472
  '}',
470
- '}'
473
+ '}',
471
474
  );
472
475
  } else {
473
476
  res.push(fieldName, '{', selectionSetIRToString(value), '}');
@@ -488,7 +491,7 @@ export function selectionSetIRToString(
488
491
  */
489
492
  function deepMergeSelectionSetObjects<T extends Record<string, any>>(
490
493
  source: T,
491
- target: T
494
+ target: T,
492
495
  ) {
493
496
  const isObject = (obj: any) => obj && typeof obj === 'object';
494
497
 
@@ -509,7 +512,7 @@ function deepMergeSelectionSetObjects<T extends Record<string, any>>(
509
512
  export function generateSelectionSet(
510
513
  modelDefinitions: SchemaModels,
511
514
  modelName: string,
512
- selectionSet?: string[]
515
+ selectionSet?: string[],
513
516
  ) {
514
517
  const modelDefinition = modelDefinitions[modelName];
515
518
 
@@ -520,7 +523,7 @@ export function generateSelectionSet(
520
523
  const selSetIr = customSelectionSetToIR(
521
524
  modelDefinitions,
522
525
  modelName,
523
- selectionSet
526
+ selectionSet,
524
527
  );
525
528
  const selSetString = selectionSetIRToString(selSetIr);
526
529
 
@@ -531,7 +534,8 @@ export function generateGraphQLDocument(
531
534
  modelDefinitions: SchemaModels,
532
535
  modelName: string,
533
536
  modelOperation: ModelOperation,
534
- listArgs?: ListArgs
537
+ listArgs?: ListArgs | QueryArgs,
538
+ indexMeta?: IndexMeta,
535
539
  ): string {
536
540
  const modelDefinition = modelDefinitions[modelName];
537
541
 
@@ -549,7 +553,33 @@ export function generateGraphQLDocument(
549
553
  const { operationPrefix, usePlural } = graphQLOperationsInfo[modelOperation];
550
554
 
551
555
  const { selectionSet } = listArgs || {};
552
- const graphQLFieldName = `${operationPrefix}${usePlural ? pluralName : name}`;
556
+
557
+ let graphQLFieldName;
558
+ let indexQueryArgs: Record<string, string>;
559
+
560
+ if (operationPrefix) {
561
+ graphQLFieldName = `${operationPrefix}${usePlural ? pluralName : name}`;
562
+ } else if (indexMeta) {
563
+ const { queryField, pk, sk = [] } = indexMeta;
564
+ graphQLFieldName = queryField;
565
+
566
+ const skQueryArgs = sk.reduce((acc: Record<string, any>, fieldName) => {
567
+ const fieldType = fields[fieldName].type;
568
+ acc[fieldName] = `Model${fieldType}KeyConditionInput`;
569
+ return acc;
570
+ }, {});
571
+
572
+ indexQueryArgs = {
573
+ [pk]: `${fields[pk].type}!`,
574
+ ...skQueryArgs,
575
+ };
576
+ } else {
577
+ throw new Error(
578
+ 'Error generating GraphQL Document - invalid operation name',
579
+ );
580
+ }
581
+
582
+ console.log('indexQueryArgs', indexQueryArgs!);
553
583
 
554
584
  let graphQLOperationType: 'mutation' | 'query' | 'subscription' | undefined;
555
585
  let graphQLSelectionSet: string | undefined;
@@ -558,7 +588,7 @@ export function generateGraphQLDocument(
558
588
  const selectionSetFields = generateSelectionSet(
559
589
  modelDefinitions,
560
590
  modelName,
561
- selectionSet
591
+ selectionSet as ListArgs['selectionSet'],
562
592
  );
563
593
 
564
594
  switch (modelOperation) {
@@ -582,7 +612,7 @@ export function generateGraphQLDocument(
582
612
 
583
613
  return acc;
584
614
  },
585
- {}
615
+ {},
586
616
  )
587
617
  : {
588
618
  [primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}!`,
@@ -598,6 +628,17 @@ export function generateGraphQLDocument(
598
628
  graphQLOperationType ?? (graphQLOperationType = 'query');
599
629
  graphQLSelectionSet ??
600
630
  (graphQLSelectionSet = `items { ${selectionSetFields} } nextToken __typename`);
631
+ case 'INDEX_QUERY':
632
+ graphQLArguments ??
633
+ (graphQLArguments = {
634
+ ...indexQueryArgs!,
635
+ filter: `Model${name}FilterInput`,
636
+ limit: 'Int',
637
+ nextToken: 'String',
638
+ });
639
+ graphQLOperationType ?? (graphQLOperationType = 'query');
640
+ graphQLSelectionSet ??
641
+ (graphQLSelectionSet = `items { ${selectionSetFields} } nextToken __typename`);
601
642
  case 'ONCREATE':
602
643
  case 'ONUPDATE':
603
644
  case 'ONDELETE':
@@ -611,20 +652,20 @@ export function generateGraphQLDocument(
611
652
  case 'OBSERVE_QUERY':
612
653
  default:
613
654
  throw new Error(
614
- 'Internal error: Attempted to generate graphql document for observeQuery. Please report this error.'
655
+ 'Internal error: Attempted to generate graphql document for observeQuery. Please report this error.',
615
656
  );
616
657
  }
617
658
 
618
659
  const graphQLDocument = `${graphQLOperationType}${
619
660
  graphQLArguments
620
661
  ? `(${Object.entries(graphQLArguments).map(
621
- ([fieldName, type]) => `\$${fieldName}: ${type}`
662
+ ([fieldName, type]) => `\$${fieldName}: ${type}`,
622
663
  )})`
623
664
  : ''
624
665
  } { ${graphQLFieldName}${
625
666
  graphQLArguments
626
667
  ? `(${Object.keys(graphQLArguments).map(
627
- fieldName => `${fieldName}: \$${fieldName}`
668
+ fieldName => `${fieldName}: \$${fieldName}`,
628
669
  )})`
629
670
  : ''
630
671
  } { ${graphQLSelectionSet} } }`;
@@ -636,7 +677,8 @@ export function buildGraphQLVariables(
636
677
  modelDefinition: SchemaModel,
637
678
  operation: ModelOperation,
638
679
  arg: QueryArgs | undefined,
639
- modelIntrospection: ModelIntrospectionSchema
680
+ modelIntrospection: ModelIntrospectionSchema,
681
+ indexMeta?: IndexMeta,
640
682
  ): object {
641
683
  const {
642
684
  fields,
@@ -664,12 +706,16 @@ export function buildGraphQLVariables(
664
706
  input: arg
665
707
  ? Object.fromEntries(
666
708
  Object.entries(
667
- normalizeMutationInput(arg, modelDefinition, modelIntrospection)
709
+ normalizeMutationInput(
710
+ arg,
711
+ modelDefinition,
712
+ modelIntrospection,
713
+ ),
668
714
  ).filter(([fieldName]) => {
669
715
  const { isReadOnly } = fields[fieldName];
670
716
 
671
717
  return !isReadOnly;
672
- })
718
+ }),
673
719
  )
674
720
  : {},
675
721
  };
@@ -685,7 +731,7 @@ export function buildGraphQLVariables(
685
731
 
686
732
  return acc;
687
733
  },
688
- {}
734
+ {},
689
735
  )
690
736
  : { [primaryKeyFieldName]: arg[primaryKeyFieldName] };
691
737
  }
@@ -695,6 +741,25 @@ export function buildGraphQLVariables(
695
741
  }
696
742
  break;
697
743
  case 'LIST':
744
+ if (arg?.filter) {
745
+ variables.filter = arg.filter;
746
+ }
747
+ if (arg?.nextToken) {
748
+ variables.nextToken = arg.nextToken;
749
+ }
750
+ if (arg?.limit) {
751
+ variables.limit = arg.limit;
752
+ }
753
+ break;
754
+ case 'INDEX_QUERY':
755
+ const { pk, sk = [] } = indexMeta!;
756
+
757
+ variables[pk] = arg![pk];
758
+
759
+ for (const skField of sk) {
760
+ variables[skField] = arg![skField];
761
+ }
762
+
698
763
  if (arg?.filter) {
699
764
  variables.filter = arg.filter;
700
765
  }
@@ -714,7 +779,7 @@ export function buildGraphQLVariables(
714
779
  break;
715
780
  case 'OBSERVE_QUERY':
716
781
  throw new Error(
717
- 'Internal error: Attempted to build variables for observeQuery. Please report this error.'
782
+ 'Internal error: Attempted to build variables for observeQuery. Please report this error.',
718
783
  );
719
784
  break;
720
785
  default:
@@ -742,7 +807,7 @@ export function buildGraphQLVariables(
742
807
  export function normalizeMutationInput(
743
808
  mutationInput: QueryArgs,
744
809
  model: SchemaModel,
745
- modelIntrospection: ModelIntrospectionSchema
810
+ modelIntrospection: ModelIntrospectionSchema,
746
811
  ): QueryArgs {
747
812
  const { fields } = model;
748
813
 
@@ -803,7 +868,7 @@ export function normalizeMutationInput(
803
868
  */
804
869
  export function authModeParams(
805
870
  client: ClientWithModels,
806
- options: AuthModeParams = {}
871
+ options: AuthModeParams = {},
807
872
  ): AuthModeParams {
808
873
  return {
809
874
  authMode: options.authMode || client[__authMode],
@@ -819,7 +884,7 @@ export function authModeParams(
819
884
  */
820
885
  export function getCustomHeaders(
821
886
  client: V6Client | ClientWithModels,
822
- requestHeaders?: CustomHeaders
887
+ requestHeaders?: CustomHeaders,
823
888
  ): CustomHeaders {
824
889
  let headers: CustomHeaders = client[__headers] || {};
825
890
 
@@ -0,0 +1,37 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type {
5
+ SchemaModel,
6
+ ModelAttribute,
7
+ SecondaryIndexAttribute,
8
+ } from '@aws-amplify/core/internals/utils';
9
+
10
+ const attributeIsSecondaryIndex = (
11
+ attr: ModelAttribute,
12
+ ): attr is SecondaryIndexAttribute => {
13
+ return (
14
+ attr.type === 'key' &&
15
+ // presence of `name` property distinguishes GSI from primary index
16
+ attr.properties?.name &&
17
+ attr.properties?.queryField &&
18
+ attr.properties?.fields.length > 0
19
+ );
20
+ };
21
+
22
+ export const getSecondaryIndexesFromSchemaModel = (model: SchemaModel) => {
23
+ const idxs = model.attributes
24
+ ?.filter(attributeIsSecondaryIndex)
25
+ .map((attr: SecondaryIndexAttribute) => {
26
+ const queryField: string = attr.properties.queryField;
27
+ const [pk, ...sk] = attr.properties.fields;
28
+
29
+ return {
30
+ queryField,
31
+ pk,
32
+ sk,
33
+ };
34
+ });
35
+
36
+ return idxs || [];
37
+ };
@@ -2,15 +2,15 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { ModelTypes } from '@aws-amplify/data-schema-types';
4
4
  import { graphQLOperationsInfo, ModelOperation } from './APIClient';
5
- import { ClientGenerationParams } from './types';
6
5
  import { V6Client, __authMode, __authToken } from '../types';
7
-
8
6
  import { listFactory } from './operations/list';
7
+ import { indexQueryFactory } from './operations/indexQuery';
9
8
  import { getFactory } from './operations/get';
10
9
  import { subscriptionFactory } from './operations/subscription';
11
10
  import { observeQueryFactory } from './operations/observeQuery';
12
11
  import { ModelIntrospectionSchema } from '@aws-amplify/core/internals/utils';
13
12
  import { GraphQLProviderConfig } from '@aws-amplify/core';
13
+ import { getSecondaryIndexesFromSchemaModel } from './clientUtils';
14
14
 
15
15
  export function generateModelsProperty<T extends Record<any, any> = never>(
16
16
  client: V6Client<Record<string, any>>,
@@ -40,14 +40,14 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
40
40
  models[name][operationPrefix] = listFactory(
41
41
  client,
42
42
  modelIntrospection,
43
- model
43
+ model,
44
44
  );
45
45
  } else if (SUBSCRIPTION_OPS.includes(operation)) {
46
46
  models[name][operationPrefix] = subscriptionFactory(
47
47
  client,
48
48
  modelIntrospection,
49
49
  model,
50
- operation
50
+ operation,
51
51
  );
52
52
  } else if (operation === 'OBSERVE_QUERY') {
53
53
  models[name][operationPrefix] = observeQueryFactory(models, model);
@@ -56,11 +56,22 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
56
56
  client,
57
57
  modelIntrospection,
58
58
  model,
59
- operation
59
+ operation,
60
60
  );
61
61
  }
62
- }
62
+ },
63
63
  );
64
+
65
+ const secondaryIdxs = getSecondaryIndexesFromSchemaModel(model);
66
+
67
+ for (const idx of secondaryIdxs) {
68
+ models[name][idx.queryField] = indexQueryFactory(
69
+ client,
70
+ modelIntrospection,
71
+ model,
72
+ idx,
73
+ );
74
+ }
64
75
  }
65
76
 
66
77
  return models;