@aws-amplify/api-graphql 4.0.22 → 4.0.23

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 (106) hide show
  1. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js +40 -31
  2. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
  3. package/dist/cjs/internals/APIClient.js +34 -19
  4. package/dist/cjs/internals/APIClient.js.map +1 -1
  5. package/dist/cjs/internals/InternalGraphQLAPI.js +10 -9
  6. package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -1
  7. package/dist/cjs/internals/generateClient.js +4 -1
  8. package/dist/cjs/internals/generateClient.js.map +1 -1
  9. package/dist/cjs/internals/generateCustomOperationsProperty.js +6 -1
  10. package/dist/cjs/internals/generateCustomOperationsProperty.js.map +1 -1
  11. package/dist/cjs/internals/operations/custom.js +90 -7
  12. package/dist/cjs/internals/operations/custom.js.map +1 -1
  13. package/dist/cjs/internals/operations/list.js +1 -1
  14. package/dist/cjs/internals/operations/list.js.map +1 -1
  15. package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js +1 -1
  16. package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js.map +1 -1
  17. package/dist/cjs/internals/server/generateModelsProperty.js +1 -1
  18. package/dist/cjs/internals/server/generateModelsProperty.js.map +1 -1
  19. package/dist/cjs/internals/v6.js +0 -2
  20. package/dist/cjs/internals/v6.js.map +1 -1
  21. package/dist/cjs/utils/errors/repackageAuthError.js +1 -1
  22. package/dist/cjs/utils/errors/repackageAuthError.js.map +1 -1
  23. package/dist/cjs/utils/resolveOwnerFields.js +3 -0
  24. package/dist/cjs/utils/resolveOwnerFields.js.map +1 -1
  25. package/dist/esm/GraphQLAPI.d.ts +2 -2
  26. package/dist/esm/GraphQLAPI.mjs.map +1 -1
  27. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +8 -8
  28. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs +40 -31
  29. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs.map +1 -1
  30. package/dist/esm/internals/APIClient.d.ts +5 -6
  31. package/dist/esm/internals/APIClient.mjs +34 -19
  32. package/dist/esm/internals/APIClient.mjs.map +1 -1
  33. package/dist/esm/internals/InternalGraphQLAPI.d.ts +1 -1
  34. package/dist/esm/internals/InternalGraphQLAPI.mjs +10 -9
  35. package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -1
  36. package/dist/esm/internals/generateClient.mjs +5 -2
  37. package/dist/esm/internals/generateClient.mjs.map +1 -1
  38. package/dist/esm/internals/generateCustomOperationsProperty.d.ts +4 -3
  39. package/dist/esm/internals/generateCustomOperationsProperty.mjs +5 -1
  40. package/dist/esm/internals/generateCustomOperationsProperty.mjs.map +1 -1
  41. package/dist/esm/internals/operations/custom.d.ts +6 -3
  42. package/dist/esm/internals/operations/custom.mjs +90 -7
  43. package/dist/esm/internals/operations/custom.mjs.map +1 -1
  44. package/dist/esm/internals/operations/get.d.ts +1 -1
  45. package/dist/esm/internals/operations/get.mjs.map +1 -1
  46. package/dist/esm/internals/operations/indexQuery.d.ts +3 -3
  47. package/dist/esm/internals/operations/indexQuery.mjs.map +1 -1
  48. package/dist/esm/internals/operations/list.d.ts +1 -1
  49. package/dist/esm/internals/operations/list.mjs +1 -1
  50. package/dist/esm/internals/operations/list.mjs.map +1 -1
  51. package/dist/esm/internals/operations/subscription.d.ts +1 -1
  52. package/dist/esm/internals/operations/subscription.mjs.map +1 -1
  53. package/dist/esm/internals/server/generateClientWithAmplifyInstance.d.ts +1 -1
  54. package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs +1 -1
  55. package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs.map +1 -1
  56. package/dist/esm/internals/server/generateModelsProperty.d.ts +3 -3
  57. package/dist/esm/internals/server/generateModelsProperty.mjs +1 -1
  58. package/dist/esm/internals/server/generateModelsProperty.mjs.map +1 -1
  59. package/dist/esm/internals/types.d.ts +2 -2
  60. package/dist/esm/internals/utils/clientProperties/generateModelsProperty.d.ts +2 -2
  61. package/dist/esm/internals/v6.d.ts +1 -1
  62. package/dist/esm/internals/v6.mjs +0 -2
  63. package/dist/esm/internals/v6.mjs.map +1 -1
  64. package/dist/esm/types/index.d.ts +34 -35
  65. package/dist/esm/utils/ConnectionStateMonitor.d.ts +2 -2
  66. package/dist/esm/utils/ConnectionStateMonitor.mjs.map +1 -1
  67. package/dist/esm/utils/ReachabilityMonitor/index.native.mjs.map +1 -1
  68. package/dist/esm/utils/errors/repackageAuthError.d.ts +2 -2
  69. package/dist/esm/utils/errors/repackageAuthError.mjs +1 -1
  70. package/dist/esm/utils/errors/repackageAuthError.mjs.map +1 -1
  71. package/dist/esm/utils/findIndexByFields.d.ts +1 -1
  72. package/dist/esm/utils/resolveOwnerFields.mjs +3 -0
  73. package/dist/esm/utils/resolveOwnerFields.mjs.map +1 -1
  74. package/package.json +8 -7
  75. package/src/GraphQLAPI.ts +4 -3
  76. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +84 -64
  77. package/src/internals/APIClient.ts +83 -58
  78. package/src/internals/InternalGraphQLAPI.ts +26 -30
  79. package/src/internals/clientUtils.ts +1 -1
  80. package/src/internals/generateClient.ts +23 -10
  81. package/src/internals/generateCustomOperationsProperty.ts +31 -8
  82. package/src/internals/operations/custom.ts +159 -32
  83. package/src/internals/operations/get.ts +9 -8
  84. package/src/internals/operations/indexQuery.ts +12 -11
  85. package/src/internals/operations/list.ts +11 -15
  86. package/src/internals/operations/observeQuery.ts +3 -1
  87. package/src/internals/operations/subscription.ts +12 -10
  88. package/src/internals/server/generateClientWithAmplifyInstance.ts +7 -7
  89. package/src/internals/server/generateModelsProperty.ts +12 -6
  90. package/src/internals/types.ts +2 -2
  91. package/src/internals/utils/clientProperties/generateEnumsProperty.ts +7 -6
  92. package/src/internals/utils/clientProperties/generateModelsProperty.ts +8 -7
  93. package/src/internals/v6.ts +6 -4
  94. package/src/server/generateClient.ts +4 -1
  95. package/src/types/PubSub.ts +1 -0
  96. package/src/types/index.ts +51 -47
  97. package/src/utils/ConnectionStateMonitor.ts +8 -3
  98. package/src/utils/ReachabilityMonitor/index.native.ts +1 -1
  99. package/src/utils/ReachabilityMonitor/index.ts +1 -0
  100. package/src/utils/ReconnectionMonitor.ts +1 -0
  101. package/src/utils/errors/repackageAuthError.ts +6 -7
  102. package/src/utils/findIndexByFields.ts +1 -1
  103. package/src/utils/resolveConfig.ts +1 -0
  104. package/src/utils/resolveLibraryOptions.ts +1 -0
  105. package/src/utils/resolveOwnerFields.ts +16 -7
  106. package/src/utils/resolvePKFields.ts +1 -0
@@ -2,31 +2,52 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
4
4
  import {
5
- initializeModel,
6
- flattenItems,
5
+ CustomOperation,
6
+ ModelIntrospectionSchema,
7
+ } from '@aws-amplify/core/internals/utils';
8
+ import { map } from 'rxjs';
9
+
10
+ import {
7
11
  authModeParams,
8
- getCustomHeaders,
12
+ defaultSelectionSetForNonModelWithIR,
13
+ flattenItems,
9
14
  generateSelectionSet,
15
+ getCustomHeaders,
16
+ initializeModel,
10
17
  selectionSetIRToString,
11
- defaultSelectionSetForNonModelWithIR,
12
18
  } from '../APIClient';
13
19
  import {
14
20
  AuthModeParams,
15
21
  ClientWithModels,
16
- GraphQLOptionsV6,
17
22
  GraphQLResult,
23
+ GraphqlSubscriptionResult,
18
24
  ListArgs,
19
25
  QueryArgs,
20
26
  V6Client,
21
27
  V6ClientSSRRequest,
22
28
  } from '../../types';
23
- import {
24
- ModelIntrospectionSchema,
25
- SchemaModel,
26
- SchemaNonModel,
27
- CustomOperation,
28
- CustomOperationArgument,
29
- } from '@aws-amplify/core/internals/utils';
29
+
30
+ type CustomOperationOptions = AuthModeParams & ListArgs;
31
+
32
+ // these are the 4 possible sets of arguments custom operations methods can receive
33
+ type OpArgs =
34
+ // SSR Request Client w Args defined
35
+ | [AmplifyServer.ContextSpec, QueryArgs, CustomOperationOptions]
36
+ // SSR Request Client without Args defined
37
+ | [AmplifyServer.ContextSpec, CustomOperationOptions]
38
+ // Client or SSR Cookies Client w Args defined
39
+ | [QueryArgs, CustomOperationOptions]
40
+ // Client or SSR Cookies Client without Args defined
41
+ | [CustomOperationOptions];
42
+
43
+ /**
44
+ * Type guard for checking whether a Custom Operation argument is a contextSpec object
45
+ */
46
+ const argIsContextSpec = (
47
+ arg: OpArgs[number],
48
+ ): arg is AmplifyServer.ContextSpec => {
49
+ return typeof (arg as AmplifyServer.ContextSpec)?.token?.value === 'symbol';
50
+ };
30
51
 
31
52
  /**
32
53
  * Builds an operation function, embedded with all client and context data, that
@@ -87,27 +108,50 @@ import {
87
108
  export function customOpFactory(
88
109
  client: ClientWithModels,
89
110
  modelIntrospection: ModelIntrospectionSchema,
90
- operationType: 'query' | 'mutation',
111
+ operationType: 'query' | 'mutation' | 'subscription',
91
112
  operation: CustomOperation,
92
113
  useContext: boolean,
93
114
  ) {
94
- const opWithContext = async (
95
- contextSpec: AmplifyServer.ContextSpec & GraphQLOptionsV6<unknown, string>,
96
- arg?: QueryArgs,
97
- options?: AuthModeParams & ListArgs,
98
- ) => {
99
- return _op(
100
- client,
101
- modelIntrospection,
102
- operationType,
103
- operation,
104
- arg,
105
- options,
106
- contextSpec,
107
- );
108
- };
115
+ // .arguments() are defined for the custom operation in the schema builder
116
+ // and are present in the model introspection schema
117
+ const argsDefined = operation.arguments !== undefined;
118
+
119
+ const op = (...args: OpArgs) => {
120
+ // options is always the last argument
121
+ const options = args[args.length - 1] as CustomOperationOptions;
122
+
123
+ let contextSpec: AmplifyServer.ContextSpec | undefined;
124
+ let arg: QueryArgs | undefined;
125
+
126
+ if (useContext) {
127
+ if (argIsContextSpec(args[0])) {
128
+ contextSpec = args[0];
129
+ } else {
130
+ throw new Error(
131
+ `Invalid first argument passed to ${operation.name}. Expected contextSpec`,
132
+ );
133
+ }
134
+ }
135
+
136
+ if (argsDefined) {
137
+ if (useContext) {
138
+ arg = args[1] as QueryArgs;
139
+ } else {
140
+ arg = args[0] as QueryArgs;
141
+ }
142
+ }
143
+
144
+ if (operationType === 'subscription') {
145
+ return _opSubscription(
146
+ // subscriptions are only enabled on the clientside
147
+ client as V6Client<Record<string, any>>,
148
+ modelIntrospection,
149
+ operation,
150
+ arg,
151
+ options,
152
+ );
153
+ }
109
154
 
110
- const op = async (arg?: QueryArgs, options?: AuthModeParams & ListArgs) => {
111
155
  return _op(
112
156
  client,
113
157
  modelIntrospection,
@@ -115,10 +159,11 @@ export function customOpFactory(
115
159
  operation,
116
160
  arg,
117
161
  options,
162
+ contextSpec,
118
163
  );
119
164
  };
120
165
 
121
- return useContext ? opWithContext : op;
166
+ return op;
122
167
  }
123
168
 
124
169
  /**
@@ -162,15 +207,20 @@ function hasStringField<Field extends string>(
162
207
  * @returns "outer" arguments string
163
208
  */
164
209
  function outerArguments(operation: CustomOperation): string {
210
+ if (operation.arguments === undefined) {
211
+ return '';
212
+ }
165
213
  const args = Object.entries(operation.arguments)
166
214
  .map(([k, v]) => {
167
215
  const baseType = v.type + (v.isRequired ? '!' : '');
168
216
  const finalType = v.isArray
169
217
  ? `[${baseType}]${v.isArrayNullable ? '' : '!'}`
170
218
  : baseType;
219
+
171
220
  return `$${k}: ${finalType}`;
172
221
  })
173
222
  .join(', ');
223
+
174
224
  return args.length > 0 ? `(${args})` : '';
175
225
  }
176
226
 
@@ -194,9 +244,13 @@ function outerArguments(operation: CustomOperation): string {
194
244
  * @returns "outer" arguments string
195
245
  */
196
246
  function innerArguments(operation: CustomOperation): string {
197
- const args = Object.entries(operation.arguments)
198
- .map(([k, v]) => `${k}: $${k}`)
247
+ if (operation.arguments === undefined) {
248
+ return '';
249
+ }
250
+ const args = Object.keys(operation.arguments)
251
+ .map(k => `${k}: $${k}`)
199
252
  .join(', ');
253
+
200
254
  return args.length > 0 ? `(${args})` : '';
201
255
  }
202
256
 
@@ -234,6 +288,7 @@ function operationSelectionSet(
234
288
  return '';
235
289
  } else if (hasStringField(operation.type, 'nonModel')) {
236
290
  const nonModel = modelIntrospection.nonModels[operation.type.nonModel];
291
+
237
292
  return `{${selectionSetIRToString(
238
293
  defaultSelectionSetForNonModelWithIR(nonModel, modelIntrospection),
239
294
  )}}`;
@@ -257,6 +312,9 @@ function operationVariables(
257
312
  args: QueryArgs = {},
258
313
  ): Record<string, unknown> {
259
314
  const variables = {} as Record<string, unknown>;
315
+ if (operation.arguments === undefined) {
316
+ return variables;
317
+ }
260
318
  for (const argDef of Object.values(operation.arguments)) {
261
319
  if (typeof args[argDef.name] !== 'undefined') {
262
320
  variables[argDef.name] = args[argDef.name];
@@ -266,6 +324,7 @@ function operationVariables(
266
324
  throw new Error(`${operation.name} requires arguments '${argDef.name}'`);
267
325
  }
268
326
  }
327
+
269
328
  return variables;
270
329
  }
271
330
 
@@ -363,3 +422,71 @@ async function _op(
363
422
  }
364
423
  }
365
424
  }
425
+
426
+ /**
427
+ * Executes an operation from the given model intro schema against a client, returning
428
+ * a fully instantiated model when relevant.
429
+ *
430
+ * @param client The client to operate `graphql()` calls through.
431
+ * @param modelIntrospection The model intro schema to construct requests from.
432
+ * @param operation The specific operation name, args, return type details.
433
+ * @param args The arguments to provide to the operation as variables.
434
+ * @param options Request options like headers, etc.
435
+ * @returns Result from the graphql request, model-instantiated when relevant.
436
+ */
437
+ function _opSubscription(
438
+ client: V6Client<Record<string, any>>,
439
+ modelIntrospection: ModelIntrospectionSchema,
440
+ operation: CustomOperation,
441
+ args?: QueryArgs,
442
+ options?: AuthModeParams & ListArgs,
443
+ ) {
444
+ const operationType = 'subscription';
445
+ const { name: operationName } = operation;
446
+ const auth = authModeParams(client, options);
447
+ const headers = getCustomHeaders(client, options?.headers);
448
+ const outerArgsString = outerArguments(operation);
449
+ const innerArgsString = innerArguments(operation);
450
+ const selectionSet = operationSelectionSet(modelIntrospection, operation);
451
+
452
+ const returnTypeModelName = hasStringField(operation.type, 'model')
453
+ ? operation.type.model
454
+ : undefined;
455
+
456
+ const query = `
457
+ ${operationType.toLocaleLowerCase()}${outerArgsString} {
458
+ ${operationName}${innerArgsString} ${selectionSet}
459
+ }
460
+ `;
461
+
462
+ const variables = operationVariables(operation, args);
463
+
464
+ const observable = client.graphql(
465
+ {
466
+ ...auth,
467
+ query,
468
+ variables,
469
+ },
470
+ headers,
471
+ ) as GraphqlSubscriptionResult<object>;
472
+
473
+ return observable.pipe(
474
+ map(value => {
475
+ const [key] = Object.keys(value.data);
476
+ const data = (value.data as any)[key];
477
+
478
+ const [initialized] = returnTypeModelName
479
+ ? initializeModel(
480
+ client as V6Client<Record<string, any>>,
481
+ returnTypeModelName,
482
+ [data],
483
+ modelIntrospection,
484
+ auth.authMode,
485
+ auth.authToken,
486
+ )
487
+ : [data];
488
+
489
+ return initialized;
490
+ }),
491
+ );
492
+ }
@@ -2,13 +2,18 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
4
4
  import {
5
- initializeModel,
6
- generateGraphQLDocument,
5
+ ModelIntrospectionSchema,
6
+ SchemaModel,
7
+ } from '@aws-amplify/core/internals/utils';
8
+
9
+ import {
10
+ ModelOperation,
11
+ authModeParams,
7
12
  buildGraphQLVariables,
8
13
  flattenItems,
9
- authModeParams,
10
- ModelOperation,
14
+ generateGraphQLDocument,
11
15
  getCustomHeaders,
16
+ initializeModel,
12
17
  } from '../APIClient';
13
18
  import {
14
19
  AuthModeParams,
@@ -20,10 +25,6 @@ import {
20
25
  V6Client,
21
26
  V6ClientSSRRequest,
22
27
  } from '../../types';
23
- import {
24
- ModelIntrospectionSchema,
25
- SchemaModel,
26
- } from '@aws-amplify/core/internals/utils';
27
28
 
28
29
  export function getFactory(
29
30
  client: ClientWithModels,
@@ -2,31 +2,32 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
4
4
  import {
5
- initializeModel,
6
- generateGraphQLDocument,
5
+ ModelIntrospectionSchema,
6
+ SchemaModel,
7
+ } from '@aws-amplify/core/internals/utils';
8
+
9
+ import {
10
+ authModeParams,
7
11
  buildGraphQLVariables,
8
12
  flattenItems,
9
- authModeParams,
13
+ generateGraphQLDocument,
10
14
  getCustomHeaders,
15
+ initializeModel,
11
16
  } from '../APIClient';
12
17
  import {
13
18
  AuthModeParams,
14
19
  ClientWithModels,
15
- ListArgs,
16
- V6ClientSSRRequest,
17
20
  GraphQLResult,
21
+ ListArgs,
18
22
  QueryArgs,
23
+ V6ClientSSRRequest,
19
24
  } from '../../types';
20
- import {
21
- ModelIntrospectionSchema,
22
- SchemaModel,
23
- } from '@aws-amplify/core/internals/utils';
24
25
 
25
- export type IndexMeta = {
26
+ export interface IndexMeta {
26
27
  queryField: string;
27
28
  pk: string;
28
29
  sk?: string[];
29
- };
30
+ }
30
31
 
31
32
  export function indexQueryFactory(
32
33
  client: ClientWithModels,
@@ -2,25 +2,26 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
4
4
  import {
5
- initializeModel,
6
- generateGraphQLDocument,
5
+ ModelIntrospectionSchema,
6
+ SchemaModel,
7
+ } from '@aws-amplify/core/internals/utils';
8
+
9
+ import {
10
+ authModeParams,
7
11
  buildGraphQLVariables,
8
12
  flattenItems,
9
- authModeParams,
13
+ generateGraphQLDocument,
10
14
  getCustomHeaders,
15
+ initializeModel,
11
16
  } from '../APIClient';
12
17
  import {
13
18
  AuthModeParams,
14
19
  ClientWithModels,
20
+ GraphQLResult,
15
21
  ListArgs,
16
22
  V6Client,
17
23
  V6ClientSSRRequest,
18
- GraphQLResult,
19
24
  } from '../../types';
20
- import {
21
- ModelIntrospectionSchema,
22
- SchemaModel,
23
- } from '@aws-amplify/core/internals/utils';
24
25
 
25
26
  export function listFactory(
26
27
  client: ClientWithModels,
@@ -51,12 +52,7 @@ async function _list(
51
52
  ) {
52
53
  const { name } = model;
53
54
 
54
- const query = generateGraphQLDocument(
55
- modelIntrospection,
56
- name,
57
- 'LIST',
58
- args,
59
- );
55
+ const query = generateGraphQLDocument(modelIntrospection, name, 'LIST', args);
60
56
  const variables = buildGraphQLVariables(
61
57
  model,
62
58
  'LIST',
@@ -69,7 +65,7 @@ async function _list(
69
65
 
70
66
  const headers = getCustomHeaders(client, args?.headers);
71
67
 
72
- const { data, extensions } = !!contextSpec
68
+ const { data, extensions } = contextSpec
73
69
  ? ((await (client as V6ClientSSRRequest<Record<string, any>>).graphql(
74
70
  contextSpec,
75
71
  {
@@ -1,9 +1,10 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { Observable } from 'rxjs';
4
- import { findIndexByFields, resolvePKFields } from '../../utils';
5
4
  import { SchemaModel } from '@aws-amplify/core/internals/utils';
6
5
 
6
+ import { findIndexByFields, resolvePKFields } from '../../utils';
7
+
7
8
  export function observeQueryFactory(models: any, model: SchemaModel) {
8
9
  const { name } = model;
9
10
 
@@ -124,6 +125,7 @@ export function observeQueryFactory(models: any, model: SchemaModel) {
124
125
  // switch the queue to write directly to the items collection
125
126
  receiveMessages = (...messages: typeof messageQueue) => {
126
127
  ingestMessages(messages);
128
+
127
129
  return items.length;
128
130
  };
129
131
  })();
@@ -2,20 +2,21 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { map } from 'rxjs';
5
- import { V6Client, GraphqlSubscriptionResult } from '../../types';
6
- import {
7
- initializeModel,
8
- generateGraphQLDocument,
9
- buildGraphQLVariables,
10
- authModeParams,
11
- ModelOperation,
12
- getCustomHeaders,
13
- } from '../APIClient';
14
5
  import {
15
6
  ModelIntrospectionSchema,
16
7
  SchemaModel,
17
8
  } from '@aws-amplify/core/internals/utils';
18
9
 
10
+ import { GraphqlSubscriptionResult, V6Client } from '../../types';
11
+ import {
12
+ ModelOperation,
13
+ authModeParams,
14
+ buildGraphQLVariables,
15
+ generateGraphQLDocument,
16
+ getCustomHeaders,
17
+ initializeModel,
18
+ } from '../APIClient';
19
+
19
20
  export function subscriptionFactory(
20
21
  client: any,
21
22
  modelIntrospection: ModelIntrospectionSchema,
@@ -29,7 +30,7 @@ export function subscriptionFactory(
29
30
  modelIntrospection,
30
31
  name,
31
32
  operation,
32
- args
33
+ args,
33
34
  );
34
35
 
35
36
  const variables = buildGraphQLVariables(
@@ -64,6 +65,7 @@ export function subscriptionFactory(
64
65
  auth.authMode,
65
66
  auth.authToken,
66
67
  );
68
+
67
69
  return initialized;
68
70
  }),
69
71
  );
@@ -1,18 +1,16 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { graphql, cancel, isCancelError } from '..';
5
- import { generateModelsProperty } from './generateModelsProperty';
6
- import { Hub } from '@aws-amplify/core';
4
+ import { cancel, graphql, isCancelError } from '..';
7
5
  import {
6
+ CommonPublicClientOptions,
7
+ ServerClientGenerationParams,
8
+ V6ClientSSRCookies,
9
+ V6ClientSSRRequest,
8
10
  __amplify,
9
11
  __authMode,
10
12
  __authToken,
11
13
  __headers,
12
- V6ClientSSRRequest,
13
- V6ClientSSRCookies,
14
- ServerClientGenerationParams,
15
- CommonPublicClientOptions,
16
14
  } from '../../types';
17
15
  import { isApiGraphQLConfig } from '../utils/runtimeTypeGuards/isApiGraphQLProviderConfig';
18
16
  import { generateEnumsProperty } from '../utils/clientProperties/generateEnumsProperty';
@@ -21,6 +19,8 @@ import {
21
19
  generateCustomQueriesProperty,
22
20
  } from '../generateCustomOperationsProperty';
23
21
 
22
+ import { generateModelsProperty } from './generateModelsProperty';
23
+
24
24
  /**
25
25
  * @private
26
26
  *
@@ -1,26 +1,32 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { ModelTypes } from '@aws-amplify/data-schema-types';
4
- import { graphQLOperationsInfo, ModelOperation } from '../APIClient';
5
- import { ServerClientGenerationParams } from '../../types/';
6
- import { V6ClientSSRRequest, V6ClientSSRCookies } from '../../types';
7
4
  import { ModelIntrospectionSchema } from '@aws-amplify/core/internals/utils';
8
5
 
6
+ import { ModelOperation, graphQLOperationsInfo } from '../APIClient';
7
+ import {
8
+ ServerClientGenerationParams,
9
+ V6ClientSSRCookies,
10
+ V6ClientSSRRequest,
11
+ } from '../../types/';
9
12
  import { listFactory } from '../operations/list';
10
13
  import { indexQueryFactory } from '../operations/indexQuery';
11
14
  import { getFactory } from '../operations/get';
12
15
  import { getSecondaryIndexesFromSchemaModel } from '../clientUtils';
13
16
 
14
17
  export function generateModelsProperty<
15
- _T extends Record<any, any> = never,
18
+ T extends Record<any, any> = never,
16
19
  ClientType extends
17
20
  | V6ClientSSRRequest<Record<string, any>>
18
21
  | V6ClientSSRCookies<Record<string, any>> = V6ClientSSRCookies<
19
22
  Record<string, any>
20
23
  >,
21
- >(client: ClientType, params: ServerClientGenerationParams): ClientType {
24
+ >(
25
+ client: ClientType,
26
+ params: ServerClientGenerationParams,
27
+ ): ModelTypes<T> | ModelTypes<never> {
22
28
  const models = {} as any;
23
- const config = params.config;
29
+ const { config } = params;
24
30
  const useContext = params.amplify === null;
25
31
 
26
32
  if (!config) {
@@ -16,8 +16,8 @@ export type ClientGenerationParams = {
16
16
  /**
17
17
  * Common options that can be used on public `generateClient()` interfaces.
18
18
  */
19
- export type CommonPublicClientOptions = {
19
+ export interface CommonPublicClientOptions {
20
20
  authMode?: GraphQLAuthMode;
21
21
  authToken?: string;
22
22
  headers?: CustomHeaders;
23
- };
23
+ }
@@ -2,8 +2,8 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import {
5
- ModelIntrospectionSchema,
6
5
  GraphQLProviderConfig,
6
+ ModelIntrospectionSchema,
7
7
  } from '@aws-amplify/core/internals/utils';
8
8
  import { EnumTypes } from '@aws-amplify/data-schema-types';
9
9
 
@@ -17,11 +17,12 @@ export const generateEnumsProperty = <T extends Record<any, any> = never>(
17
17
  return {} as EnumTypes<never>;
18
18
  }
19
19
 
20
- const enums: {
21
- [EnumName: string]: {
22
- values: () => string[];
23
- };
24
- } = {};
20
+ const enums: Record<
21
+ string,
22
+ {
23
+ values(): string[];
24
+ }
25
+ > = {};
25
26
 
26
27
  for (const [_, enumData] of Object.entries(modelIntrospection.enums)) {
27
28
  enums[enumData.name] = {
@@ -1,23 +1,24 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { ModelTypes } from '@aws-amplify/data-schema-types';
4
- import { graphQLOperationsInfo, ModelOperation } from '../../APIClient';
5
- import { V6Client, __authMode, __authToken } from '../../../types';
4
+ import {
5
+ GraphQLProviderConfig,
6
+ ModelIntrospectionSchema,
7
+ } from '@aws-amplify/core/internals/utils';
8
+
9
+ import { ModelOperation, graphQLOperationsInfo } from '../../APIClient';
10
+ import { V6Client } from '../../../types';
6
11
  import { listFactory } from '../../operations/list';
7
12
  import { indexQueryFactory } from '../../operations/indexQuery';
8
13
  import { getFactory } from '../../operations/get';
9
14
  import { subscriptionFactory } from '../../operations/subscription';
10
15
  import { observeQueryFactory } from '../../operations/observeQuery';
11
- import {
12
- ModelIntrospectionSchema,
13
- GraphQLProviderConfig,
14
- } from '@aws-amplify/core/internals/utils';
15
16
  import { getSecondaryIndexesFromSchemaModel } from '../../clientUtils';
16
17
 
17
18
  export function generateModelsProperty<T extends Record<any, any> = never>(
18
19
  client: V6Client<Record<string, any>>,
19
20
  apiGraphQLConfig: GraphQLProviderConfig['GraphQL'],
20
- ): ModelTypes<T> {
21
+ ): ModelTypes<T> | ModelTypes<never> {
21
22
  const models = {} as any;
22
23
 
23
24
  const modelIntrospection: ModelIntrospectionSchema | undefined =
@@ -1,15 +1,16 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+ import { CustomHeaders } from '@aws-amplify/data-schema-types';
4
+
3
5
  import { GraphQLAPI } from '../GraphQLAPI';
4
6
  import {
7
+ GraphQLOptionsV6,
8
+ GraphQLResponseV6,
9
+ V6Client,
5
10
  __amplify,
6
11
  __authMode,
7
12
  __authToken,
8
- V6Client,
9
- GraphQLOptionsV6,
10
- GraphQLResponseV6,
11
13
  } from '../types';
12
- import { CustomHeaders } from '@aws-amplify/data-schema-types';
13
14
 
14
15
  /**
15
16
  * Invokes graphql operations against a graphql service, providing correct input and
@@ -117,6 +118,7 @@ export function graphql<
117
118
  options,
118
119
  additionalHeaders,
119
120
  );
121
+
120
122
  return result as any;
121
123
  }
122
124
 
@@ -5,6 +5,8 @@ import {
5
5
  AmplifyServer,
6
6
  getAmplifyServerContext,
7
7
  } from '@aws-amplify/core/internals/adapter-core';
8
+ import { CustomHeaders } from '@aws-amplify/data-schema-types';
9
+
8
10
  import { generateClientWithAmplifyInstance } from '../internals/server';
9
11
  import {
10
12
  GenerateServerClientParams,
@@ -14,7 +16,6 @@ import {
14
16
  V6ClientSSRRequest,
15
17
  __amplify,
16
18
  } from '../types';
17
- import { CustomHeaders } from '@aws-amplify/data-schema-types';
18
19
 
19
20
  /**
20
21
  * Generates an GraphQL API client that works with Amplify server context.
@@ -55,6 +56,7 @@ export function generateClient<T extends Record<any, any> = never>({
55
56
  additionalHeaders?: CustomHeaders,
56
57
  ) => {
57
58
  const amplifyInstance = getAmplifyServerContext(contextSpec).amplify;
59
+
58
60
  return prevGraphql.call(
59
61
  { [__amplify]: amplifyInstance },
60
62
  options,
@@ -63,5 +65,6 @@ export function generateClient<T extends Record<any, any> = never>({
63
65
  };
64
66
 
65
67
  client.graphql = wrappedGraphql as unknown as GraphQLMethodSSR;
68
+
66
69
  return client;
67
70
  }