@aws-amplify/api-graphql 4.0.17 → 4.0.18-experimental-lerna-test.d2b0b08.55

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 (170) 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 +145 -54
  4. package/dist/cjs/internals/APIClient.js.map +1 -1
  5. package/dist/cjs/internals/InternalGraphQLAPI.js +41 -42
  6. package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -1
  7. package/dist/cjs/internals/generateClient.js +15 -5
  8. package/dist/cjs/internals/generateClient.js.map +1 -1
  9. package/dist/cjs/internals/generateCustomOperationsProperty.js +50 -0
  10. package/dist/cjs/internals/generateCustomOperationsProperty.js.map +1 -0
  11. package/dist/cjs/internals/operations/custom.js +356 -0
  12. package/dist/cjs/internals/operations/custom.js.map +1 -0
  13. package/dist/cjs/internals/operations/get.js +1 -1
  14. package/dist/cjs/internals/operations/get.js.map +1 -1
  15. package/dist/cjs/internals/operations/indexQuery.js +1 -1
  16. package/dist/cjs/internals/operations/indexQuery.js.map +1 -1
  17. package/dist/cjs/internals/operations/list.js +2 -2
  18. package/dist/cjs/internals/operations/list.js.map +1 -1
  19. package/dist/cjs/internals/operations/subscription.js +1 -1
  20. package/dist/cjs/internals/operations/subscription.js.map +1 -1
  21. package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js +12 -2
  22. package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js.map +1 -1
  23. package/dist/cjs/internals/server/generateModelsProperty.js +1 -1
  24. package/dist/cjs/internals/server/generateModelsProperty.js.map +1 -1
  25. package/dist/cjs/internals/utils/clientProperties/generateEnumsProperty.js.map +1 -0
  26. package/dist/cjs/internals/utils/{generateModelsProperty.js → clientProperties/generateModelsProperty.js} +7 -7
  27. package/dist/cjs/internals/utils/clientProperties/generateModelsProperty.js.map +1 -0
  28. package/dist/cjs/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.js.map +1 -0
  29. package/dist/cjs/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.js.map +1 -0
  30. package/dist/cjs/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.js +15 -0
  31. package/dist/cjs/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.js.map +1 -0
  32. package/dist/cjs/internals/v6.js +0 -2
  33. package/dist/cjs/internals/v6.js.map +1 -1
  34. package/dist/cjs/utils/errors/constants.js +47 -0
  35. package/dist/cjs/utils/errors/constants.js.map +1 -0
  36. package/dist/cjs/utils/errors/createGraphQLResultWithError.js +15 -0
  37. package/dist/cjs/utils/errors/createGraphQLResultWithError.js.map +1 -0
  38. package/dist/cjs/utils/errors/index.js +3 -1
  39. package/dist/cjs/utils/errors/index.js.map +1 -1
  40. package/dist/cjs/utils/errors/repackageAuthError.js +7 -7
  41. package/dist/cjs/utils/errors/repackageAuthError.js.map +1 -1
  42. package/dist/cjs/utils/resolveOwnerFields.js +6 -0
  43. package/dist/cjs/utils/resolveOwnerFields.js.map +1 -1
  44. package/dist/esm/GraphQLAPI.d.ts +2 -2
  45. package/dist/esm/GraphQLAPI.mjs.map +1 -1
  46. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +8 -8
  47. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs +40 -31
  48. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs.map +1 -1
  49. package/dist/esm/internals/APIClient.d.ts +10 -9
  50. package/dist/esm/internals/APIClient.mjs +144 -54
  51. package/dist/esm/internals/APIClient.mjs.map +1 -1
  52. package/dist/esm/internals/InternalGraphQLAPI.d.ts +1 -1
  53. package/dist/esm/internals/InternalGraphQLAPI.mjs +45 -44
  54. package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -1
  55. package/dist/esm/internals/generateClient.mjs +15 -5
  56. package/dist/esm/internals/generateClient.mjs.map +1 -1
  57. package/dist/esm/internals/generateCustomOperationsProperty.d.ts +9 -0
  58. package/dist/esm/internals/generateCustomOperationsProperty.mjs +45 -0
  59. package/dist/esm/internals/generateCustomOperationsProperty.mjs.map +1 -0
  60. package/dist/esm/internals/operations/custom.d.ts +63 -0
  61. package/dist/esm/internals/operations/custom.mjs +354 -0
  62. package/dist/esm/internals/operations/custom.mjs.map +1 -0
  63. package/dist/esm/internals/operations/get.d.ts +1 -1
  64. package/dist/esm/internals/operations/get.mjs +1 -1
  65. package/dist/esm/internals/operations/get.mjs.map +1 -1
  66. package/dist/esm/internals/operations/indexQuery.d.ts +3 -3
  67. package/dist/esm/internals/operations/indexQuery.mjs +1 -1
  68. package/dist/esm/internals/operations/indexQuery.mjs.map +1 -1
  69. package/dist/esm/internals/operations/list.d.ts +1 -1
  70. package/dist/esm/internals/operations/list.mjs +2 -2
  71. package/dist/esm/internals/operations/list.mjs.map +1 -1
  72. package/dist/esm/internals/operations/subscription.d.ts +1 -1
  73. package/dist/esm/internals/operations/subscription.mjs +1 -1
  74. package/dist/esm/internals/operations/subscription.mjs.map +1 -1
  75. package/dist/esm/internals/server/generateClientWithAmplifyInstance.d.ts +1 -1
  76. package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs +12 -2
  77. package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs.map +1 -1
  78. package/dist/esm/internals/server/generateModelsProperty.d.ts +3 -3
  79. package/dist/esm/internals/server/generateModelsProperty.mjs +1 -1
  80. package/dist/esm/internals/server/generateModelsProperty.mjs.map +1 -1
  81. package/dist/esm/internals/types.d.ts +2 -2
  82. package/dist/esm/internals/utils/clientProperties/generateEnumsProperty.mjs.map +1 -0
  83. package/dist/esm/internals/utils/{generateModelsProperty.d.ts → clientProperties/generateModelsProperty.d.ts} +2 -2
  84. package/dist/esm/internals/utils/{generateModelsProperty.mjs → clientProperties/generateModelsProperty.mjs} +7 -7
  85. package/dist/esm/internals/utils/clientProperties/generateModelsProperty.mjs.map +1 -0
  86. package/dist/esm/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.mjs.map +1 -0
  87. package/dist/esm/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.mjs.map +1 -0
  88. package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.d.ts +2 -0
  89. package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.mjs +12 -0
  90. package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.mjs.map +1 -0
  91. package/dist/esm/internals/v6.d.ts +1 -1
  92. package/dist/esm/internals/v6.mjs +0 -2
  93. package/dist/esm/internals/v6.mjs.map +1 -1
  94. package/dist/esm/types/index.d.ts +43 -35
  95. package/dist/esm/utils/ConnectionStateMonitor.d.ts +2 -2
  96. package/dist/esm/utils/ConnectionStateMonitor.mjs.map +1 -1
  97. package/dist/esm/utils/ReachabilityMonitor/index.native.mjs.map +1 -1
  98. package/dist/esm/utils/errors/constants.d.ts +7 -0
  99. package/dist/esm/utils/errors/constants.mjs +46 -0
  100. package/dist/esm/utils/errors/constants.mjs.map +1 -0
  101. package/dist/esm/utils/errors/createGraphQLResultWithError.d.ts +2 -0
  102. package/dist/esm/utils/errors/createGraphQLResultWithError.mjs +13 -0
  103. package/dist/esm/utils/errors/createGraphQLResultWithError.mjs.map +1 -0
  104. package/dist/esm/utils/errors/index.d.ts +1 -0
  105. package/dist/esm/utils/errors/index.mjs +1 -0
  106. package/dist/esm/utils/errors/index.mjs.map +1 -1
  107. package/dist/esm/utils/errors/repackageAuthError.d.ts +3 -7
  108. package/dist/esm/utils/errors/repackageAuthError.mjs +6 -6
  109. package/dist/esm/utils/errors/repackageAuthError.mjs.map +1 -1
  110. package/dist/esm/utils/findIndexByFields.d.ts +1 -1
  111. package/dist/esm/utils/resolveConfig.mjs +1 -0
  112. package/dist/esm/utils/resolveConfig.mjs.map +1 -1
  113. package/dist/esm/utils/resolveOwnerFields.mjs +6 -0
  114. package/dist/esm/utils/resolveOwnerFields.mjs.map +1 -1
  115. package/package.json +8 -7
  116. package/src/GraphQLAPI.ts +4 -3
  117. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +84 -64
  118. package/src/internals/APIClient.ts +242 -104
  119. package/src/internals/InternalGraphQLAPI.ts +65 -75
  120. package/src/internals/clientUtils.ts +1 -1
  121. package/src/internals/generateClient.ts +43 -6
  122. package/src/internals/generateCustomOperationsProperty.ts +112 -0
  123. package/src/internals/operations/custom.ts +492 -0
  124. package/src/internals/operations/get.ts +10 -9
  125. package/src/internals/operations/indexQuery.ts +13 -12
  126. package/src/internals/operations/list.ts +11 -15
  127. package/src/internals/operations/observeQuery.ts +3 -1
  128. package/src/internals/operations/subscription.ts +14 -10
  129. package/src/internals/server/generateClientWithAmplifyInstance.ts +26 -7
  130. package/src/internals/server/generateModelsProperty.ts +12 -6
  131. package/src/internals/types.ts +2 -2
  132. package/src/internals/utils/{generateEnumsProperty.ts → clientProperties/generateEnumsProperty.ts} +7 -6
  133. package/src/internals/utils/{generateModelsProperty.ts → clientProperties/generateModelsProperty.ts} +11 -10
  134. package/src/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.ts +15 -0
  135. package/src/internals/v6.ts +6 -4
  136. package/src/server/generateClient.ts +4 -1
  137. package/src/types/PubSub.ts +1 -0
  138. package/src/types/index.ts +61 -45
  139. package/src/utils/ConnectionStateMonitor.ts +8 -3
  140. package/src/utils/ReachabilityMonitor/index.native.ts +1 -1
  141. package/src/utils/ReachabilityMonitor/index.ts +1 -0
  142. package/src/utils/ReconnectionMonitor.ts +1 -0
  143. package/src/utils/errors/constants.ts +56 -0
  144. package/src/utils/errors/createGraphQLResultWithError.ts +15 -0
  145. package/src/utils/errors/index.ts +1 -0
  146. package/src/utils/errors/repackageAuthError.ts +11 -14
  147. package/src/utils/findIndexByFields.ts +1 -1
  148. package/src/utils/resolveConfig.ts +1 -0
  149. package/src/utils/resolveLibraryOptions.ts +1 -0
  150. package/src/utils/resolveOwnerFields.ts +19 -7
  151. package/src/utils/resolvePKFields.ts +1 -0
  152. package/dist/cjs/internals/utils/generateEnumsProperty.js.map +0 -1
  153. package/dist/cjs/internals/utils/generateModelsProperty.js.map +0 -1
  154. package/dist/cjs/internals/utils/isApiGraphQLProviderConfig.js.map +0 -1
  155. package/dist/cjs/internals/utils/isConfigureEventWithResourceConfig.js.map +0 -1
  156. package/dist/esm/internals/utils/generateEnumsProperty.mjs.map +0 -1
  157. package/dist/esm/internals/utils/generateModelsProperty.mjs.map +0 -1
  158. package/dist/esm/internals/utils/isApiGraphQLProviderConfig.mjs.map +0 -1
  159. package/dist/esm/internals/utils/isConfigureEventWithResourceConfig.mjs.map +0 -1
  160. /package/dist/cjs/internals/utils/{generateEnumsProperty.js → clientProperties/generateEnumsProperty.js} +0 -0
  161. /package/dist/cjs/internals/utils/{isApiGraphQLProviderConfig.js → runtimeTypeGuards/isApiGraphQLProviderConfig.js} +0 -0
  162. /package/dist/cjs/internals/utils/{isConfigureEventWithResourceConfig.js → runtimeTypeGuards/isConfigureEventWithResourceConfig.js} +0 -0
  163. /package/dist/esm/internals/utils/{generateEnumsProperty.d.ts → clientProperties/generateEnumsProperty.d.ts} +0 -0
  164. /package/dist/esm/internals/utils/{generateEnumsProperty.mjs → clientProperties/generateEnumsProperty.mjs} +0 -0
  165. /package/dist/esm/internals/utils/{isApiGraphQLProviderConfig.d.ts → runtimeTypeGuards/isApiGraphQLProviderConfig.d.ts} +0 -0
  166. /package/dist/esm/internals/utils/{isApiGraphQLProviderConfig.mjs → runtimeTypeGuards/isApiGraphQLProviderConfig.mjs} +0 -0
  167. /package/dist/esm/internals/utils/{isConfigureEventWithResourceConfig.d.ts → runtimeTypeGuards/isConfigureEventWithResourceConfig.d.ts} +0 -0
  168. /package/dist/esm/internals/utils/{isConfigureEventWithResourceConfig.mjs → runtimeTypeGuards/isConfigureEventWithResourceConfig.mjs} +0 -0
  169. /package/src/internals/utils/{isApiGraphQLProviderConfig.ts → runtimeTypeGuards/isApiGraphQLProviderConfig.ts} +0 -0
  170. /package/src/internals/utils/{isConfigureEventWithResourceConfig.ts → runtimeTypeGuards/isConfigureEventWithResourceConfig.ts} +0 -0
@@ -3,39 +3,43 @@
3
3
  import {
4
4
  DocumentNode,
5
5
  OperationDefinitionNode,
6
- print,
7
- parse,
8
- GraphQLError,
9
6
  OperationTypeNode,
7
+ parse,
8
+ print,
10
9
  } from 'graphql';
11
10
  import { Observable, catchError } from 'rxjs';
12
- import { AmplifyClassV6, ConsoleLogger } from '@aws-amplify/core';
11
+ import { AmplifyClassV6 } from '@aws-amplify/core';
13
12
  import {
14
- GraphQLAuthMode,
13
+ AmplifyUrl,
15
14
  CustomUserAgentDetails,
15
+ GraphQLAuthMode,
16
16
  getAmplifyUserAgent,
17
- AmplifyUrl,
18
17
  } from '@aws-amplify/core/internals/utils';
19
- import {
20
- GraphQLAuthError,
21
- GraphQLResult,
22
- GraphQLOperation,
23
- GraphQLOptions,
24
- } from '../types';
25
18
  import { isCancelError as isCancelErrorREST } from '@aws-amplify/api-rest';
26
19
  import {
27
- post,
28
20
  cancel as cancelREST,
21
+ post,
29
22
  updateRequestToBeCancellable,
30
23
  } from '@aws-amplify/api-rest/internals';
31
- import { AWSAppSyncRealTimeProvider } from '../Providers/AWSAppSyncRealTimeProvider';
32
24
  import { CustomHeaders, RequestOptions } from '@aws-amplify/data-schema-types';
25
+
26
+ import { AWSAppSyncRealTimeProvider } from '../Providers/AWSAppSyncRealTimeProvider';
27
+ import { GraphQLOperation, GraphQLOptions, GraphQLResult } from '../types';
33
28
  import { resolveConfig, resolveLibraryOptions } from '../utils';
34
- import { repackageUnauthError } from '../utils/errors/repackageAuthError';
29
+ import { repackageUnauthorizedError } from '../utils/errors/repackageAuthError';
30
+ import {
31
+ NO_API_KEY,
32
+ NO_AUTH_TOKEN_HEADER,
33
+ NO_ENDPOINT,
34
+ NO_SIGNED_IN_USER,
35
+ NO_VALID_AUTH_TOKEN,
36
+ NO_VALID_CREDENTIALS,
37
+ } from '../utils/errors/constants';
38
+ import { GraphQLApiError, createGraphQLResultWithError } from '../utils/errors';
35
39
 
36
- const USER_AGENT_HEADER = 'x-amz-user-agent';
40
+ import { isGraphQLResponseWithErrors } from './utils/runtimeTypeGuards/isGraphQLResponseWithErrors';
37
41
 
38
- const logger = new ConsoleLogger('GraphQLAPI');
42
+ const USER_AGENT_HEADER = 'x-amz-user-agent';
39
43
 
40
44
  const isAmplifyInstance = (
41
45
  amplify:
@@ -70,54 +74,60 @@ export class InternalGraphQLAPIClass {
70
74
  authMode: GraphQLAuthMode,
71
75
  additionalHeaders: Record<string, string> = {},
72
76
  ) {
73
- const {
74
- region: region,
75
- endpoint: appSyncGraphqlEndpoint,
76
- apiKey,
77
- } = resolveConfig(amplify);
77
+ const { apiKey } = resolveConfig(amplify);
78
78
 
79
79
  let headers = {};
80
80
 
81
81
  switch (authMode) {
82
82
  case 'apiKey':
83
83
  if (!apiKey) {
84
- throw new Error(GraphQLAuthError.NO_API_KEY);
84
+ throw new GraphQLApiError(NO_API_KEY);
85
85
  }
86
86
  headers = {
87
87
  'X-Api-Key': apiKey,
88
88
  };
89
89
  break;
90
- case 'iam':
90
+ case 'iam': {
91
91
  const session = await amplify.Auth.fetchAuthSession();
92
92
  if (session.credentials === undefined) {
93
- throw new Error(GraphQLAuthError.NO_CREDENTIALS);
93
+ throw new GraphQLApiError(NO_VALID_CREDENTIALS);
94
94
  }
95
95
  break;
96
+ }
96
97
  case 'oidc':
97
- case 'userPool':
98
- try {
99
- let token;
98
+ case 'userPool': {
99
+ let token: string | undefined;
100
100
 
101
+ try {
101
102
  token = (
102
103
  await amplify.Auth.fetchAuthSession()
103
104
  ).tokens?.accessToken.toString();
104
-
105
- if (!token) {
106
- throw new Error(GraphQLAuthError.NO_FEDERATED_JWT);
107
- }
108
- headers = {
109
- Authorization: token,
110
- };
111
105
  } catch (e) {
112
- throw new Error(GraphQLAuthError.NO_CURRENT_USER);
106
+ // fetchAuthSession failed
107
+ throw new GraphQLApiError({
108
+ ...NO_SIGNED_IN_USER,
109
+ underlyingError: e,
110
+ });
111
+ }
112
+
113
+ // `fetchAuthSession()` succeeded but didn't return `tokens`.
114
+ // This may happen when unauthenticated access is enabled and there is
115
+ // no user signed in.
116
+ if (!token) {
117
+ throw new GraphQLApiError(NO_VALID_AUTH_TOKEN);
113
118
  }
119
+
120
+ headers = {
121
+ Authorization: token,
122
+ };
114
123
  break;
124
+ }
115
125
  case 'lambda':
116
126
  if (
117
127
  typeof additionalHeaders === 'object' &&
118
128
  !additionalHeaders.Authorization
119
129
  ) {
120
- throw new Error(GraphQLAuthError.NO_AUTH_TOKEN);
130
+ throw new GraphQLApiError(NO_AUTH_TOKEN_HEADER);
121
131
  }
122
132
 
123
133
  headers = {
@@ -139,8 +149,7 @@ export class InternalGraphQLAPIClass {
139
149
  */
140
150
  getGraphqlOperationType(operation: GraphQLOperation): OperationTypeNode {
141
151
  const doc = parse(operation);
142
- const definitions =
143
- doc.definitions as ReadonlyArray<OperationDefinitionNode>;
152
+ const definitions = doc.definitions as readonly OperationDefinitionNode[];
144
153
  const [{ operation: operationType }] = definitions;
145
154
 
146
155
  return operationType;
@@ -176,7 +185,7 @@ export class InternalGraphQLAPIClass {
176
185
 
177
186
  switch (operationType) {
178
187
  case 'query':
179
- case 'mutation':
188
+ case 'mutation': {
180
189
  const abortController = new AbortController();
181
190
 
182
191
  let responsePromise: Promise<GraphQLResult<T>>;
@@ -215,7 +224,9 @@ export class InternalGraphQLAPIClass {
215
224
  responsePromise,
216
225
  abortController,
217
226
  );
227
+
218
228
  return responsePromise;
229
+ }
219
230
  case 'subscription':
220
231
  return this._graphqlSubscribe(
221
232
  amplify as AmplifyClassV6,
@@ -238,7 +249,7 @@ export class InternalGraphQLAPIClass {
238
249
  authToken?: string,
239
250
  ): Promise<GraphQLResult<T>> {
240
251
  const {
241
- region: region,
252
+ region,
242
253
  endpoint: appSyncGraphqlEndpoint,
243
254
  customEndpoint,
244
255
  customEndpointRegion,
@@ -355,17 +366,15 @@ export class InternalGraphQLAPIClass {
355
366
  const endpoint = customEndpoint || appSyncGraphqlEndpoint;
356
367
 
357
368
  if (!endpoint) {
358
- const error = new GraphQLError('No graphql endpoint provided.');
359
-
360
- throw {
361
- data: {},
362
- errors: [error],
363
- };
369
+ throw createGraphQLResultWithError<T>(new GraphQLApiError(NO_ENDPOINT));
364
370
  }
365
371
 
366
372
  let response: any;
367
373
 
368
374
  try {
375
+ // See the inline doc of the REST `post()` API for possible errors to be thrown.
376
+ // As these errors are catastrophic they should be caught and handled by GraphQL
377
+ // API consumers.
369
378
  const { body: responseBody } = await this._api.post(amplify, {
370
379
  url: new AmplifyUrl(endpoint),
371
380
  options: {
@@ -377,39 +386,20 @@ export class InternalGraphQLAPIClass {
377
386
  abortController,
378
387
  });
379
388
 
380
- const result = await responseBody.json();
381
-
382
- response = result;
383
- } catch (err) {
384
- // If the exception is because user intentionally
385
- // cancelled the request, do not modify the exception
386
- // so that clients can identify the exception correctly.
387
- if (this.isCancelError(err)) {
388
- throw err;
389
+ response = await responseBody.json();
390
+ } catch (error) {
391
+ if (this.isCancelError(error)) {
392
+ throw error;
389
393
  }
390
394
 
391
- response = {
392
- data: {},
393
- errors: [
394
- new GraphQLError(
395
- (err as any).message,
396
- null,
397
- null,
398
- null,
399
- null,
400
- err as any,
401
- ),
402
- ],
403
- };
395
+ response = createGraphQLResultWithError<T>(error as any);
404
396
  }
405
397
 
406
- const { errors } = response;
407
-
408
- if (errors && errors.length) {
409
- throw repackageUnauthError(response);
398
+ if (isGraphQLResponseWithErrors(response)) {
399
+ throw repackageUnauthorizedError(response);
410
400
  }
411
401
 
412
- return response;
402
+ return response as unknown as GraphQLResult<T>;
413
403
  }
414
404
 
415
405
  /**
@@ -467,7 +457,7 @@ export class InternalGraphQLAPIClass {
467
457
  .pipe(
468
458
  catchError(e => {
469
459
  if (e.errors) {
470
- throw repackageUnauthError(e);
460
+ throw repackageUnauthorizedError(e);
471
461
  }
472
462
  throw e;
473
463
  }),
@@ -2,8 +2,8 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import type {
5
- SchemaModel,
6
5
  ModelAttribute,
6
+ SchemaModel,
7
7
  SecondaryIndexAttribute,
8
8
  } from '@aws-amplify/core/internals/utils';
9
9
 
@@ -1,11 +1,14 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { Hub } from '@aws-amplify/core';
4
- import { EnumTypes, ModelTypes } from '@aws-amplify/data-schema-types';
5
- import { graphql, cancel, isCancelError } from './v6';
6
- import { generateEnumsProperty } from './utils/generateEnumsProperty';
7
- import { generateModelsProperty } from './utils/generateModelsProperty';
8
- import { isApiGraphQLConfig } from './utils/isApiGraphQLProviderConfig';
4
+ import {
5
+ CustomMutations,
6
+ CustomQueries,
7
+ CustomSubscriptions,
8
+ EnumTypes,
9
+ ModelTypes,
10
+ } from '@aws-amplify/data-schema-types';
11
+
9
12
  import {
10
13
  V6Client,
11
14
  __amplify,
@@ -13,8 +16,18 @@ import {
13
16
  __authToken,
14
17
  __headers,
15
18
  } from '../types';
19
+
20
+ import { cancel, graphql, isCancelError } from './v6';
21
+ import { generateEnumsProperty } from './utils/clientProperties/generateEnumsProperty';
22
+ import { generateModelsProperty } from './utils/clientProperties/generateModelsProperty';
23
+ import { isApiGraphQLConfig } from './utils/runtimeTypeGuards/isApiGraphQLProviderConfig';
24
+ import {
25
+ generateCustomMutationsProperty,
26
+ generateCustomQueriesProperty,
27
+ generateCustomSubscriptionsProperty,
28
+ } from './generateCustomOperationsProperty';
16
29
  import { ClientGenerationParams } from './types';
17
- import { isConfigureEventWithResourceConfig } from './utils/isConfigureEventWithResourceConfig';
30
+ import { isConfigureEventWithResourceConfig } from './utils/runtimeTypeGuards/isConfigureEventWithResourceConfig';
18
31
 
19
32
  /**
20
33
  * @private
@@ -38,6 +51,9 @@ export function generateClient<T extends Record<any, any> = never>(
38
51
  isCancelError,
39
52
  models: emptyProperty as ModelTypes<never>,
40
53
  enums: emptyProperty as EnumTypes<never>,
54
+ queries: emptyProperty as CustomQueries<never>,
55
+ mutations: emptyProperty as CustomMutations<never>,
56
+ subscriptions: emptyProperty as CustomSubscriptions<never>,
41
57
  } as any;
42
58
 
43
59
  const apiGraphqlConfig = params.amplify.getConfig().API?.GraphQL;
@@ -45,6 +61,15 @@ export function generateClient<T extends Record<any, any> = never>(
45
61
  if (isApiGraphQLConfig(apiGraphqlConfig)) {
46
62
  client.models = generateModelsProperty<T>(client, apiGraphqlConfig);
47
63
  client.enums = generateEnumsProperty<T>(apiGraphqlConfig);
64
+ client.queries = generateCustomQueriesProperty<T>(client, apiGraphqlConfig);
65
+ client.mutations = generateCustomMutationsProperty<T>(
66
+ client,
67
+ apiGraphqlConfig,
68
+ );
69
+ client.subscriptions = generateCustomSubscriptionsProperty(
70
+ client,
71
+ apiGraphqlConfig,
72
+ );
48
73
  } else {
49
74
  // This happens when the `Amplify.configure()` call gets evaluated after the `generateClient()` call.
50
75
  //
@@ -75,6 +100,18 @@ const generateModelsPropertyOnAmplifyConfigure = (clientRef: any) => {
75
100
  if (isApiGraphQLConfig(apiGraphQLConfig)) {
76
101
  clientRef.models = generateModelsProperty(clientRef, apiGraphQLConfig);
77
102
  clientRef.enums = generateEnumsProperty(apiGraphQLConfig);
103
+ clientRef.queries = generateCustomQueriesProperty(
104
+ clientRef,
105
+ apiGraphQLConfig,
106
+ );
107
+ clientRef.mutations = generateCustomMutationsProperty(
108
+ clientRef,
109
+ apiGraphQLConfig,
110
+ );
111
+ clientRef.subscriptions = generateCustomSubscriptionsProperty(
112
+ clientRef,
113
+ apiGraphQLConfig,
114
+ );
78
115
  }
79
116
  });
80
117
  };
@@ -0,0 +1,112 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import {
4
+ CustomMutations,
5
+ CustomQueries,
6
+ CustomSubscriptions,
7
+ } from '@aws-amplify/data-schema-types';
8
+ import {
9
+ GraphQLProviderConfig,
10
+ ModelIntrospectionSchema,
11
+ } from '@aws-amplify/core/internals/utils';
12
+
13
+ import { V6Client, __amplify } from '../types';
14
+
15
+ import { customOpFactory } from './operations/custom';
16
+
17
+ type OpTypes = 'queries' | 'mutations' | 'subscriptions';
18
+
19
+ type CustomOpsProperty<
20
+ T extends Record<any, any>,
21
+ OpType extends OpTypes,
22
+ > = OpType extends 'queries'
23
+ ? CustomQueries<T>
24
+ : OpType extends 'mutations'
25
+ ? CustomMutations<T>
26
+ : OpType extends 'subscriptions'
27
+ ? CustomSubscriptions<T>
28
+ : never;
29
+
30
+ const operationTypeMap = {
31
+ queries: 'query',
32
+ mutations: 'mutation',
33
+ subscriptions: 'subscription',
34
+ } as const;
35
+
36
+ export function generateCustomOperationsProperty<
37
+ T extends Record<any, any>,
38
+ OpType extends OpTypes,
39
+ >(
40
+ client: V6Client<Record<string, any>>,
41
+ config: GraphQLProviderConfig['GraphQL'],
42
+ operationsType: OpType,
43
+ ): OpType extends 'queries' ? CustomQueries<T> : CustomMutations<T> {
44
+ // some bundlers end up with `Amplify.configure` being called *after* generate client.
45
+ // if that occurs, we need to *not error* while we wait. handling for late configuration
46
+ // occurs in `generateClient()`. we do not need to subscribe to Hub events here.
47
+ if (!config) {
48
+ return {} as CustomOpsProperty<T, OpType>;
49
+ }
50
+
51
+ const modelIntrospection: ModelIntrospectionSchema | undefined =
52
+ config.modelIntrospection;
53
+
54
+ // model intro schema might be absent if there's not actually a configured GraphQL API
55
+ if (!modelIntrospection) {
56
+ return {} as CustomOpsProperty<T, OpType>;
57
+ }
58
+
59
+ // custom operations will be absent from model intro schema if no custom ops
60
+ // are present on the source schema.
61
+ const operations = modelIntrospection[operationsType];
62
+ if (!operations) {
63
+ return {} as CustomOpsProperty<T, OpType>;
64
+ }
65
+
66
+ const ops = {} as CustomOpsProperty<T, OpType>;
67
+ const useContext = client[__amplify] === null;
68
+ for (const operation of Object.values(operations)) {
69
+ (ops as any)[operation.name] = customOpFactory(
70
+ client,
71
+ modelIntrospection,
72
+ operationTypeMap[operationsType],
73
+ operation,
74
+ useContext,
75
+ );
76
+ }
77
+
78
+ return ops;
79
+ }
80
+
81
+ export function generateCustomMutationsProperty<T extends Record<any, any>>(
82
+ client: V6Client<Record<string, any>>,
83
+ config: GraphQLProviderConfig['GraphQL'],
84
+ ) {
85
+ return generateCustomOperationsProperty<T, 'mutations'>(
86
+ client,
87
+ config,
88
+ 'mutations',
89
+ );
90
+ }
91
+
92
+ export function generateCustomQueriesProperty<T extends Record<any, any>>(
93
+ client: V6Client<Record<string, any>>,
94
+ config: GraphQLProviderConfig['GraphQL'],
95
+ ) {
96
+ return generateCustomOperationsProperty<T, 'queries'>(
97
+ client,
98
+ config,
99
+ 'queries',
100
+ );
101
+ }
102
+
103
+ export function generateCustomSubscriptionsProperty<T extends Record<any, any>>(
104
+ client: V6Client<Record<string, any>>,
105
+ config: GraphQLProviderConfig['GraphQL'],
106
+ ) {
107
+ return generateCustomOperationsProperty<T, 'subscriptions'>(
108
+ client,
109
+ config,
110
+ 'subscriptions',
111
+ );
112
+ }