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