@aws-amplify/api-graphql 4.0.1-api-v6-models.9351bcf.0 → 4.0.1-api-v6-models.891fe0d.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.
Files changed (95) hide show
  1. package/internals/server/package.json +8 -0
  2. package/lib/GraphQLAPI.d.ts +1 -1
  3. package/lib/GraphQLAPI.js +23 -28
  4. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +467 -582
  5. package/lib/index.js +1 -1
  6. package/lib/internals/APIClient.d.ts +16 -1
  7. package/lib/internals/APIClient.js +232 -206
  8. package/lib/internals/InternalGraphQLAPI.d.ts +1 -1
  9. package/lib/internals/InternalGraphQLAPI.js +205 -247
  10. package/lib/internals/generateClient.js +14 -13
  11. package/lib/internals/generateModelsProperty.js +19 -312
  12. package/lib/internals/operations/get.d.ts +2 -0
  13. package/lib/internals/operations/get.js +59 -0
  14. package/lib/internals/operations/list.d.ts +2 -0
  15. package/lib/internals/operations/list.js +71 -0
  16. package/lib/internals/operations/observeQuery.d.ts +2 -0
  17. package/lib/internals/operations/observeQuery.js +119 -0
  18. package/lib/internals/operations/subscription.d.ts +1 -0
  19. package/lib/internals/operations/subscription.js +27 -0
  20. package/lib/internals/server/generateClient.d.ts +11 -0
  21. package/lib/internals/server/generateClient.js +30 -0
  22. package/lib/internals/server/generateModelsProperty.d.ts +3 -0
  23. package/lib/internals/server/generateModelsProperty.js +45 -0
  24. package/lib/internals/server/index.d.ts +1 -0
  25. package/lib/internals/server/index.js +7 -0
  26. package/lib/internals/v6.js +3 -3
  27. package/lib/tsconfig.tsbuildinfo +1 -1
  28. package/lib/types/index.d.ts +36 -4
  29. package/lib/utils/ConnectionStateMonitor.js +51 -64
  30. package/lib/utils/ReachabilityMonitor/index.js +2 -2
  31. package/lib/utils/ReachabilityMonitor/index.native.js +4 -6
  32. package/lib/utils/ReconnectionMonitor.js +20 -24
  33. package/lib/utils/errors/APIError.js +7 -11
  34. package/lib/utils/errors/assertValidationError.js +4 -4
  35. package/lib/utils/errors/validation.js +5 -6
  36. package/lib/utils/findIndexByFields.js +4 -9
  37. package/lib/utils/resolveConfig.js +9 -10
  38. package/lib/utils/resolveLibraryOptions.js +4 -5
  39. package/lib/utils/resolveOwnerFields.js +10 -33
  40. package/lib/utils/resolvePKFields.js +2 -3
  41. package/lib-esm/GraphQLAPI.d.ts +1 -1
  42. package/lib-esm/GraphQLAPI.js +23 -29
  43. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +459 -575
  44. package/lib-esm/Providers/constants.js +10 -10
  45. package/lib-esm/internals/APIClient.d.ts +16 -1
  46. package/lib-esm/internals/APIClient.js +230 -205
  47. package/lib-esm/internals/InternalGraphQLAPI.d.ts +1 -1
  48. package/lib-esm/internals/InternalGraphQLAPI.js +201 -244
  49. package/lib-esm/internals/generateClient.js +11 -10
  50. package/lib-esm/internals/generateModelsProperty.js +19 -312
  51. package/lib-esm/internals/operations/get.d.ts +2 -0
  52. package/lib-esm/internals/operations/get.js +55 -0
  53. package/lib-esm/internals/operations/list.d.ts +2 -0
  54. package/lib-esm/internals/operations/list.js +67 -0
  55. package/lib-esm/internals/operations/observeQuery.d.ts +2 -0
  56. package/lib-esm/internals/operations/observeQuery.js +115 -0
  57. package/lib-esm/internals/operations/subscription.d.ts +1 -0
  58. package/lib-esm/internals/operations/subscription.js +23 -0
  59. package/lib-esm/internals/server/generateClient.d.ts +11 -0
  60. package/lib-esm/internals/server/generateClient.js +26 -0
  61. package/lib-esm/internals/server/generateModelsProperty.d.ts +3 -0
  62. package/lib-esm/internals/server/generateModelsProperty.js +41 -0
  63. package/lib-esm/internals/server/index.d.ts +1 -0
  64. package/lib-esm/internals/server/index.js +3 -0
  65. package/lib-esm/internals/v6.js +1 -1
  66. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  67. package/lib-esm/types/index.d.ts +36 -4
  68. package/lib-esm/types/index.js +3 -3
  69. package/lib-esm/utils/ConnectionStateMonitor.js +49 -63
  70. package/lib-esm/utils/ReachabilityMonitor/index.js +1 -1
  71. package/lib-esm/utils/ReachabilityMonitor/index.native.js +1 -3
  72. package/lib-esm/utils/ReconnectionMonitor.js +19 -24
  73. package/lib-esm/utils/errors/APIError.js +6 -11
  74. package/lib-esm/utils/errors/assertValidationError.js +2 -2
  75. package/lib-esm/utils/errors/validation.js +5 -6
  76. package/lib-esm/utils/findIndexByFields.js +4 -9
  77. package/lib-esm/utils/resolveConfig.js +8 -9
  78. package/lib-esm/utils/resolveLibraryOptions.js +4 -5
  79. package/lib-esm/utils/resolveOwnerFields.js +10 -33
  80. package/lib-esm/utils/resolvePKFields.js +2 -3
  81. package/package.json +8 -7
  82. package/src/GraphQLAPI.ts +6 -2
  83. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +6 -5
  84. package/src/internals/APIClient.ts +122 -35
  85. package/src/internals/InternalGraphQLAPI.ts +35 -10
  86. package/src/internals/generateClient.ts +0 -1
  87. package/src/internals/generateModelsProperty.ts +27 -340
  88. package/src/internals/operations/get.ts +121 -0
  89. package/src/internals/operations/list.ts +111 -0
  90. package/src/internals/operations/observeQuery.ts +140 -0
  91. package/src/internals/operations/subscription.ts +59 -0
  92. package/src/internals/server/generateClient.ts +45 -0
  93. package/src/internals/server/generateModelsProperty.ts +70 -0
  94. package/src/internals/server/index.ts +4 -0
  95. package/src/types/index.ts +65 -9
@@ -1,4 +1,3 @@
1
- import { __assign, __awaiter, __generator, __read } from "tslib";
2
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
2
  // SPDX-License-Identifier: Apache-2.0
4
3
  import { print, parse, GraphQLError, } from 'graphql';
@@ -9,116 +8,98 @@ import { isCancelError as isCancelErrorREST } from '@aws-amplify/api-rest';
9
8
  import { post, cancel as cancelREST, updateRequestToBeCancellable, } from '@aws-amplify/api-rest/internals';
10
9
  import { AWSAppSyncRealTimeProvider } from '../Providers/AWSAppSyncRealTimeProvider';
11
10
  import { resolveConfig, resolveLibraryOptions } from '../utils';
12
- var USER_AGENT_HEADER = 'x-amz-user-agent';
13
- var logger = new ConsoleLogger('GraphQLAPI');
14
- export var graphqlOperation = function (query, variables, authToken) {
15
- if (variables === void 0) { variables = {}; }
16
- return ({
17
- query: query,
18
- variables: variables,
19
- authToken: authToken,
20
- });
11
+ const USER_AGENT_HEADER = 'x-amz-user-agent';
12
+ const logger = new ConsoleLogger('GraphQLAPI');
13
+ export const graphqlOperation = (query, variables = {}, authToken) => ({
14
+ query,
15
+ variables,
16
+ authToken,
17
+ });
18
+ const isAmplifyInstance = (amplify) => {
19
+ return typeof amplify !== 'function';
21
20
  };
22
21
  /**
23
22
  * Export Cloud Logic APIs
24
23
  */
25
- var InternalGraphQLAPIClass = /** @class */ (function () {
24
+ export class InternalGraphQLAPIClass {
26
25
  /**
27
26
  * Initialize GraphQL API with AWS configuration
28
27
  * @param {Object} options - Configuration object for API
29
28
  */
30
- function InternalGraphQLAPIClass(options) {
29
+ constructor(options) {
31
30
  this._api = {
32
- post: post,
33
- cancelREST: cancelREST,
34
- isCancelErrorREST: isCancelErrorREST,
35
- updateRequestToBeCancellable: updateRequestToBeCancellable,
31
+ post,
32
+ cancelREST,
33
+ isCancelErrorREST,
34
+ updateRequestToBeCancellable,
36
35
  };
37
36
  this._options = options;
38
37
  logger.debug('API Options', this._options);
39
38
  }
40
- InternalGraphQLAPIClass.prototype.getModuleName = function () {
39
+ getModuleName() {
41
40
  return 'InternalGraphQLAPI';
42
- };
43
- InternalGraphQLAPIClass.prototype._headerBasedAuth = function (amplify, authMode, additionalHeaders) {
44
- var _a;
45
- if (additionalHeaders === void 0) { additionalHeaders = {}; }
46
- return __awaiter(this, void 0, void 0, function () {
47
- var config, _b, region, appSyncGraphqlEndpoint, apiKey, defaultAuthMode, authenticationType, headers, _c, session, token, e_1;
48
- return __generator(this, function (_d) {
49
- switch (_d.label) {
50
- case 0:
51
- config = amplify.getConfig();
52
- _b = resolveConfig(amplify), region = _b.region, appSyncGraphqlEndpoint = _b.endpoint, apiKey = _b.apiKey, defaultAuthMode = _b.defaultAuthMode;
53
- authenticationType = authMode || defaultAuthMode || 'iam';
54
- headers = {};
55
- _c = authenticationType;
56
- switch (_c) {
57
- case 'apiKey': return [3 /*break*/, 1];
58
- case 'iam': return [3 /*break*/, 2];
59
- case 'oidc': return [3 /*break*/, 4];
60
- case 'userPool': return [3 /*break*/, 4];
61
- case 'lambda': return [3 /*break*/, 8];
62
- case 'none': return [3 /*break*/, 9];
63
- }
64
- return [3 /*break*/, 10];
65
- case 1:
66
- if (!apiKey) {
67
- throw new Error(GraphQLAuthError.NO_API_KEY);
68
- }
69
- headers = {
70
- 'X-Api-Key': apiKey,
71
- };
72
- return [3 /*break*/, 11];
73
- case 2: return [4 /*yield*/, amplify.Auth.fetchAuthSession()];
74
- case 3:
75
- session = _d.sent();
76
- if (session.credentials === undefined) {
77
- throw new Error(GraphQLAuthError.NO_CREDENTIALS);
78
- }
79
- return [3 /*break*/, 11];
80
- case 4:
81
- _d.trys.push([4, 6, , 7]);
82
- token = void 0;
83
- return [4 /*yield*/, amplify.Auth.fetchAuthSession()];
84
- case 5:
85
- token = (_a = (_d.sent()).tokens) === null || _a === void 0 ? void 0 : _a.accessToken.toString();
86
- if (!token) {
87
- throw new Error(GraphQLAuthError.NO_FEDERATED_JWT);
88
- }
89
- headers = {
90
- Authorization: token,
91
- };
92
- return [3 /*break*/, 7];
93
- case 6:
94
- e_1 = _d.sent();
95
- throw new Error(GraphQLAuthError.NO_CURRENT_USER);
96
- case 7: return [3 /*break*/, 11];
97
- case 8:
98
- if (!additionalHeaders.Authorization) {
99
- throw new Error(GraphQLAuthError.NO_AUTH_TOKEN);
100
- }
101
- headers = {
102
- Authorization: additionalHeaders.Authorization,
103
- };
104
- return [3 /*break*/, 11];
105
- case 9: return [3 /*break*/, 11];
106
- case 10: return [3 /*break*/, 11];
107
- case 11: return [2 /*return*/, headers];
41
+ }
42
+ async _headerBasedAuth(amplify, authMode, additionalHeaders = {}) {
43
+ const config = amplify.getConfig();
44
+ const { region: region, endpoint: appSyncGraphqlEndpoint, apiKey, defaultAuthMode, } = resolveConfig(amplify);
45
+ const authenticationType = authMode || defaultAuthMode || 'iam';
46
+ let headers = {};
47
+ switch (authenticationType) {
48
+ case 'apiKey':
49
+ if (!apiKey) {
50
+ throw new Error(GraphQLAuthError.NO_API_KEY);
108
51
  }
109
- });
110
- });
111
- };
52
+ headers = {
53
+ 'X-Api-Key': apiKey,
54
+ };
55
+ break;
56
+ case 'iam':
57
+ const session = await amplify.Auth.fetchAuthSession();
58
+ if (session.credentials === undefined) {
59
+ throw new Error(GraphQLAuthError.NO_CREDENTIALS);
60
+ }
61
+ break;
62
+ case 'oidc':
63
+ case 'userPool':
64
+ try {
65
+ let token;
66
+ token = (await amplify.Auth.fetchAuthSession()).tokens?.accessToken.toString();
67
+ if (!token) {
68
+ throw new Error(GraphQLAuthError.NO_FEDERATED_JWT);
69
+ }
70
+ headers = {
71
+ Authorization: token,
72
+ };
73
+ }
74
+ catch (e) {
75
+ throw new Error(GraphQLAuthError.NO_CURRENT_USER);
76
+ }
77
+ break;
78
+ case 'lambda':
79
+ if (!additionalHeaders.Authorization) {
80
+ throw new Error(GraphQLAuthError.NO_AUTH_TOKEN);
81
+ }
82
+ headers = {
83
+ Authorization: additionalHeaders.Authorization,
84
+ };
85
+ break;
86
+ case 'none':
87
+ break;
88
+ default:
89
+ break;
90
+ }
91
+ return headers;
92
+ }
112
93
  /**
113
94
  * to get the operation type
114
95
  * @param operation
115
96
  */
116
- InternalGraphQLAPIClass.prototype.getGraphqlOperationType = function (operation) {
117
- var doc = parse(operation);
118
- var definitions = doc.definitions;
119
- var _a = __read(definitions, 1), operationType = _a[0].operation;
97
+ getGraphqlOperationType(operation) {
98
+ const doc = parse(operation);
99
+ const definitions = doc.definitions;
100
+ const [{ operation: operationType }] = definitions;
120
101
  return operationType;
121
- };
102
+ }
122
103
  /**
123
104
  * Executes a GraphQL operation
124
105
  *
@@ -126,14 +107,13 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
126
107
  * @param [additionalHeaders] - headers to merge in after any `graphql_headers` set in the config
127
108
  * @returns An Observable if the query is a subscription query, else a promise of the graphql result.
128
109
  */
129
- InternalGraphQLAPIClass.prototype.graphql = function (amplify, _a, additionalHeaders, customUserAgentDetails) {
130
- var paramQuery = _a.query, _b = _a.variables, variables = _b === void 0 ? {} : _b, authMode = _a.authMode, authToken = _a.authToken;
131
- var query = typeof paramQuery === 'string'
110
+ graphql(amplify, { query: paramQuery, variables = {}, authMode, authToken }, additionalHeaders, customUserAgentDetails) {
111
+ const query = typeof paramQuery === 'string'
132
112
  ? parse(paramQuery)
133
113
  : parse(print(paramQuery));
134
- var _c = __read(query.definitions.filter(function (def) { return def.kind === 'OperationDefinition'; }), 1), _d = _c[0], operationDef = _d === void 0 ? {} : _d;
135
- var operationType = operationDef.operation;
136
- var headers = additionalHeaders || {};
114
+ const [operationDef = {}] = query.definitions.filter(def => def.kind === 'OperationDefinition');
115
+ const { operation: operationType } = operationDef;
116
+ const headers = additionalHeaders || {};
137
117
  // if an authorization header is set, have the explicit authToken take precedence
138
118
  if (authToken) {
139
119
  headers.Authorization = authToken;
@@ -141,174 +121,151 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
141
121
  switch (operationType) {
142
122
  case 'query':
143
123
  case 'mutation':
144
- var abortController = new AbortController();
145
- var responsePromise = this._graphql(amplify, { query: query, variables: variables, authMode: authMode }, headers, abortController, customUserAgentDetails);
124
+ const abortController = new AbortController();
125
+ let responsePromise;
126
+ if (isAmplifyInstance(amplify)) {
127
+ responsePromise = this._graphql(amplify, { query, variables, authMode }, headers, abortController, customUserAgentDetails);
128
+ }
129
+ else {
130
+ const wrapper = amplifyInstance => this._graphql(amplifyInstance, { query, variables, authMode }, headers, abortController, customUserAgentDetails);
131
+ responsePromise = amplify(wrapper);
132
+ }
146
133
  this._api.updateRequestToBeCancellable(responsePromise, abortController);
147
134
  return responsePromise;
148
135
  case 'subscription':
149
- return this._graphqlSubscribe(amplify, { query: query, variables: variables, authMode: authMode }, headers, customUserAgentDetails);
136
+ return this._graphqlSubscribe(amplify, { query, variables, authMode }, headers, customUserAgentDetails);
150
137
  default:
151
- throw new Error("invalid operation type: ".concat(operationType));
138
+ throw new Error(`invalid operation type: ${operationType}`);
152
139
  }
153
- };
154
- InternalGraphQLAPIClass.prototype._graphql = function (amplify, _a, additionalHeaders, abortController, customUserAgentDetails) {
155
- var query = _a.query, variables = _a.variables, authMode = _a.authMode;
156
- if (additionalHeaders === void 0) { additionalHeaders = {}; }
157
- return __awaiter(this, void 0, void 0, function () {
158
- var _b, region, appSyncGraphqlEndpoint, customEndpoint, customEndpointRegion, _c, customHeaders, withCredentials, headers, _d, _e, _f, _g, _h, _j, _k, body, signingServiceInfo, endpoint, error, response, responseBody, result, err_1, errors;
159
- var _l;
160
- return __generator(this, function (_m) {
161
- switch (_m.label) {
162
- case 0:
163
- _b = resolveConfig(amplify), region = _b.region, appSyncGraphqlEndpoint = _b.endpoint, customEndpoint = _b.customEndpoint, customEndpointRegion = _b.customEndpointRegion;
164
- _c = resolveLibraryOptions(amplify), customHeaders = _c.headers, withCredentials = _c.withCredentials;
165
- _d = [{}];
166
- _e = !customEndpoint;
167
- if (!_e) return [3 /*break*/, 2];
168
- return [4 /*yield*/, this._headerBasedAuth(amplify, authMode, additionalHeaders)];
169
- case 1:
170
- _e = (_m.sent());
171
- _m.label = 2;
172
- case 2:
173
- _f = [__assign.apply(void 0, _d.concat([(_e)]))];
174
- _g = customEndpoint;
175
- if (!_g) return [3 /*break*/, 6];
176
- if (!customEndpointRegion) return [3 /*break*/, 4];
177
- return [4 /*yield*/, this._headerBasedAuth(amplify, authMode, additionalHeaders)];
178
- case 3:
179
- _h = _m.sent();
180
- return [3 /*break*/, 5];
181
- case 4:
182
- _h = {};
183
- _m.label = 5;
184
- case 5:
185
- _g = (_h);
186
- _m.label = 6;
187
- case 6:
188
- _j = [__assign.apply(void 0, _f.concat([((_g) ||
189
- {})]))];
190
- _k = customHeaders;
191
- if (!_k) return [3 /*break*/, 8];
192
- return [4 /*yield*/, customHeaders({
193
- query: print(query),
194
- variables: variables,
195
- })];
196
- case 7:
197
- _k = (_m.sent());
198
- _m.label = 8;
199
- case 8:
200
- headers = __assign.apply(void 0, [__assign.apply(void 0, [__assign.apply(void 0, _j.concat([(_k)])), additionalHeaders]), (!customEndpoint && (_l = {},
201
- _l[USER_AGENT_HEADER] = getAmplifyUserAgent(customUserAgentDetails),
202
- _l))]);
203
- body = {
204
- query: print(query),
205
- variables: variables || null,
206
- };
207
- /**
208
- * We do not send the signing service info to the REST API under the
209
- * following conditions (i.e. it will not sign the request):
210
- * - there is a custom endpoint but no region
211
- * - the auth mode is `none`, or `apiKey`
212
- * - the auth mode is a type other than the types listed below
213
- */
214
- if ((customEndpoint && !customEndpointRegion) ||
215
- (authMode !== 'oidc' &&
216
- authMode !== 'userPool' &&
217
- authMode !== 'iam' &&
218
- authMode !== 'lambda')) {
219
- signingServiceInfo = null;
220
- }
221
- else {
222
- signingServiceInfo = {
223
- service: !customEndpointRegion ? 'appsync' : 'execute-api',
224
- region: !customEndpointRegion ? region : customEndpointRegion,
225
- };
226
- }
227
- endpoint = customEndpoint || appSyncGraphqlEndpoint;
228
- if (!endpoint) {
229
- error = new GraphQLError('No graphql endpoint provided.');
230
- throw {
231
- data: {},
232
- errors: [error],
233
- };
234
- }
235
- _m.label = 9;
236
- case 9:
237
- _m.trys.push([9, 12, , 13]);
238
- return [4 /*yield*/, this._api.post({
239
- url: new AmplifyUrl(endpoint),
240
- options: {
241
- headers: headers,
242
- body: body,
243
- signingServiceInfo: signingServiceInfo,
244
- withCredentials: withCredentials,
245
- },
246
- abortController: abortController,
247
- })];
248
- case 10:
249
- responseBody = (_m.sent()).body;
250
- return [4 /*yield*/, responseBody.json()];
251
- case 11:
252
- result = _m.sent();
253
- response = result;
254
- return [3 /*break*/, 13];
255
- case 12:
256
- err_1 = _m.sent();
257
- // If the exception is because user intentionally
258
- // cancelled the request, do not modify the exception
259
- // so that clients can identify the exception correctly.
260
- if (this._api.isCancelErrorREST(err_1)) {
261
- throw err_1;
262
- }
263
- response = {
264
- data: {},
265
- errors: [new GraphQLError(err_1.message, null, null, null, null, err_1)],
266
- };
267
- return [3 /*break*/, 13];
268
- case 13:
269
- errors = response.errors;
270
- if (errors && errors.length) {
271
- throw response;
272
- }
273
- return [2 /*return*/, response];
274
- }
140
+ }
141
+ async _graphql(amplify, { query, variables, authMode }, additionalHeaders = {}, abortController, customUserAgentDetails) {
142
+ const { region: region, endpoint: appSyncGraphqlEndpoint, customEndpoint, customEndpointRegion, } = resolveConfig(amplify);
143
+ // Retrieve library options from Amplify configuration
144
+ const { headers: customHeaders, withCredentials } = resolveLibraryOptions(amplify);
145
+ // TODO: Figure what we need to do to remove `!`'s.
146
+ const headers = {
147
+ ...(!customEndpoint &&
148
+ (await this._headerBasedAuth(amplify, authMode, additionalHeaders))),
149
+ /**
150
+ * Custom endpoint headers.
151
+ * If there is both a custom endpoint and custom region present, we get the headers.
152
+ * If there is a custom endpoint but no region, we return an empty object.
153
+ * If neither are present, we return an empty object.
154
+ */
155
+ ...((customEndpoint &&
156
+ (customEndpointRegion
157
+ ? await this._headerBasedAuth(amplify, authMode, additionalHeaders)
158
+ : {})) ||
159
+ {}),
160
+ // Custom headers included in Amplify configuration options:
161
+ ...(customHeaders &&
162
+ (await customHeaders({
163
+ query: print(query),
164
+ variables,
165
+ }))),
166
+ // Headers from individual calls to `graphql`:
167
+ ...additionalHeaders,
168
+ // User agent headers:
169
+ ...(!customEndpoint && {
170
+ [USER_AGENT_HEADER]: getAmplifyUserAgent(customUserAgentDetails),
171
+ }),
172
+ };
173
+ const body = {
174
+ query: print(query),
175
+ variables: variables || null,
176
+ };
177
+ let signingServiceInfo;
178
+ /**
179
+ * We do not send the signing service info to the REST API under the
180
+ * following conditions (i.e. it will not sign the request):
181
+ * - there is a custom endpoint but no region
182
+ * - the auth mode is `none`, or `apiKey`
183
+ * - the auth mode is a type other than the types listed below
184
+ */
185
+ if ((customEndpoint && !customEndpointRegion) ||
186
+ (authMode !== 'oidc' &&
187
+ authMode !== 'userPool' &&
188
+ authMode !== 'iam' &&
189
+ authMode !== 'lambda')) {
190
+ signingServiceInfo = null;
191
+ }
192
+ else {
193
+ signingServiceInfo = {
194
+ service: !customEndpointRegion ? 'appsync' : 'execute-api',
195
+ region: !customEndpointRegion ? region : customEndpointRegion,
196
+ };
197
+ }
198
+ const endpoint = customEndpoint || appSyncGraphqlEndpoint;
199
+ if (!endpoint) {
200
+ const error = new GraphQLError('No graphql endpoint provided.');
201
+ throw {
202
+ data: {},
203
+ errors: [error],
204
+ };
205
+ }
206
+ let response;
207
+ try {
208
+ const { body: responseBody } = await this._api.post({
209
+ url: new AmplifyUrl(endpoint),
210
+ options: {
211
+ headers,
212
+ body,
213
+ signingServiceInfo,
214
+ withCredentials,
215
+ },
216
+ abortController,
275
217
  });
276
- });
277
- };
218
+ const result = await responseBody.json();
219
+ response = result;
220
+ }
221
+ catch (err) {
222
+ // If the exception is because user intentionally
223
+ // cancelled the request, do not modify the exception
224
+ // so that clients can identify the exception correctly.
225
+ if (this.isCancelError(err)) {
226
+ throw err;
227
+ }
228
+ response = {
229
+ data: {},
230
+ errors: [new GraphQLError(err.message, null, null, null, null, err)],
231
+ };
232
+ }
233
+ const { errors } = response;
234
+ if (errors && errors.length) {
235
+ throw response;
236
+ }
237
+ return response;
238
+ }
278
239
  /**
279
240
  * Checks to see if an error thrown is from an api request cancellation
280
241
  * @param {any} error - Any error
281
242
  * @return {boolean} - A boolean indicating if the error was from an api request cancellation
282
243
  */
283
- InternalGraphQLAPIClass.prototype.isCancelError = function (error) {
244
+ isCancelError(error) {
284
245
  return this._api.isCancelErrorREST(error);
285
- };
246
+ }
286
247
  /**
287
248
  * Cancels an inflight request. Only applicable for graphql queries and mutations
288
249
  * @param {any} request - request to cancel
289
250
  * @returns - A boolean indicating if the request was cancelled
290
251
  */
291
- InternalGraphQLAPIClass.prototype.cancel = function (request, message) {
252
+ cancel(request, message) {
292
253
  return this._api.cancelREST(request, message);
293
- };
294
- InternalGraphQLAPIClass.prototype._graphqlSubscribe = function (amplify, _a, additionalHeaders, customUserAgentDetails) {
295
- var query = _a.query, variables = _a.variables, authMode = _a.authMode;
296
- if (additionalHeaders === void 0) { additionalHeaders = {}; }
297
- var config = resolveConfig(amplify);
254
+ }
255
+ _graphqlSubscribe(amplify, { query, variables, authMode }, additionalHeaders = {}, customUserAgentDetails) {
256
+ const config = resolveConfig(amplify);
298
257
  if (!this.appSyncRealTime) {
299
258
  this.appSyncRealTime = new AWSAppSyncRealTimeProvider();
300
259
  }
301
260
  return this.appSyncRealTime.subscribe({
302
261
  query: print(query),
303
- variables: variables,
304
- appSyncGraphqlEndpoint: config === null || config === void 0 ? void 0 : config.endpoint,
305
- region: config === null || config === void 0 ? void 0 : config.region,
306
- authenticationType: authMode || (config === null || config === void 0 ? void 0 : config.defaultAuthMode),
307
- apiKey: config === null || config === void 0 ? void 0 : config.apiKey,
308
- additionalHeaders: additionalHeaders,
262
+ variables,
263
+ appSyncGraphqlEndpoint: config?.endpoint,
264
+ region: config?.region,
265
+ authenticationType: authMode || config?.defaultAuthMode,
266
+ apiKey: config?.apiKey,
267
+ additionalHeaders,
309
268
  }, customUserAgentDetails);
310
- };
311
- return InternalGraphQLAPIClass;
312
- }());
313
- export { InternalGraphQLAPIClass };
314
- export var InternalGraphQLAPI = new InternalGraphQLAPIClass(null);
269
+ }
270
+ }
271
+ export const InternalGraphQLAPI = new InternalGraphQLAPIClass(null);
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  import { graphql, cancel, isCancelError } from './v6';
2
4
  import { generateModelsProperty } from './generateModelsProperty';
3
5
  import { __amplify, __authMode, __authToken } from '../types';
@@ -11,16 +13,15 @@ import { __amplify, __authMode, __authToken } from '../types';
11
13
  * @returns
12
14
  */
13
15
  export function generateClient(params) {
14
- var _a;
15
- var client = (_a = {},
16
- _a[__amplify] = params.amplify,
17
- _a[__authMode] = params.authMode,
18
- _a[__authToken] = params.authToken,
19
- _a.graphql = graphql,
20
- _a.cancel = cancel,
21
- _a.isCancelError = isCancelError,
22
- _a.models = {},
23
- _a);
16
+ const client = {
17
+ [__amplify]: params.amplify,
18
+ [__authMode]: params.authMode,
19
+ [__authToken]: params.authToken,
20
+ graphql,
21
+ cancel,
22
+ isCancelError,
23
+ models: {},
24
+ };
24
25
  client.models = generateModelsProperty(client, params);
25
26
  return client;
26
27
  }