@aws-amplify/api-graphql 4.0.1-console-preview.b5a42af.0 → 4.0.1-console-preview.9607e4a.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.
@@ -5,7 +5,8 @@ import { print, parse, GraphQLError, } from 'graphql';
5
5
  import { Amplify, Cache, fetchAuthSession } from '@aws-amplify/core';
6
6
  import { ConsoleLogger as Logger, getAmplifyUserAgent, } from '@aws-amplify/core/internals/utils';
7
7
  import { GraphQLAuthError, } from '../types';
8
- import { post } from '@aws-amplify/api-rest/internals';
8
+ import { isCancelError as isCancelErrorREST } from '@aws-amplify/api-rest';
9
+ import { post, cancel as cancelREST, updateRequestToBeCancellable, } from '@aws-amplify/api-rest/internals';
9
10
  import { AWSAppSyncRealTimeProvider } from '../Providers/AWSAppSyncRealTimeProvider';
10
11
  var USER_AGENT_HEADER = 'x-amz-user-agent';
11
12
  var logger = new Logger('GraphQLAPI');
@@ -27,26 +28,27 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
27
28
  */
28
29
  function InternalGraphQLAPIClass(options) {
29
30
  this.Cache = Cache;
30
- this._api = { post: post };
31
+ this._api = { post: post, updateRequestToBeCancellable: updateRequestToBeCancellable };
31
32
  this._options = options;
32
33
  logger.debug('API Options', this._options);
33
34
  }
34
35
  InternalGraphQLAPIClass.prototype.getModuleName = function () {
35
36
  return 'InternalGraphQLAPI';
36
37
  };
37
- InternalGraphQLAPIClass.prototype._headerBasedAuth = function (defaultAuthenticationType, additionalHeaders, customUserAgentDetails) {
38
+ InternalGraphQLAPIClass.prototype._headerBasedAuth = function (authMode, additionalHeaders) {
38
39
  if (additionalHeaders === void 0) { additionalHeaders = {}; }
39
- var _a;
40
+ var _a, _b;
40
41
  return __awaiter(this, void 0, void 0, function () {
41
- var config, _b, region, appSyncGraphqlEndpoint, apiKey, defaultAuthMode, headers, _c, session, token, e_1;
42
- return __generator(this, function (_d) {
43
- switch (_d.label) {
42
+ var config, _c, region, appSyncGraphqlEndpoint, apiKey, defaultAuthMode, authenticationType, headers, _d, session, token, e_1;
43
+ return __generator(this, function (_e) {
44
+ switch (_e.label) {
44
45
  case 0:
45
46
  config = Amplify.getConfig();
46
- _b = config.API.GraphQL, region = _b.region, appSyncGraphqlEndpoint = _b.endpoint, apiKey = _b.apiKey, defaultAuthMode = _b.defaultAuthMode;
47
+ _c = ((_a = config.API) === null || _a === void 0 ? void 0 : _a.GraphQL) || {}, region = _c.region, appSyncGraphqlEndpoint = _c.endpoint, apiKey = _c.apiKey, defaultAuthMode = _c.defaultAuthMode;
48
+ authenticationType = authMode || defaultAuthMode || 'iam';
47
49
  headers = {};
48
- _c = defaultAuthMode;
49
- switch (_c) {
50
+ _d = authenticationType;
51
+ switch (_d) {
50
52
  case 'apiKey': return [3 /*break*/, 1];
51
53
  case 'iam': return [3 /*break*/, 2];
52
54
  case 'oidc': return [3 /*break*/, 4];
@@ -65,17 +67,17 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
65
67
  return [3 /*break*/, 11];
66
68
  case 2: return [4 /*yield*/, fetchAuthSession()];
67
69
  case 3:
68
- session = _d.sent();
70
+ session = _e.sent();
69
71
  if (session.credentials === undefined) {
70
72
  throw new Error(GraphQLAuthError.NO_CREDENTIALS);
71
73
  }
72
74
  return [3 /*break*/, 11];
73
75
  case 4:
74
- _d.trys.push([4, 6, , 7]);
76
+ _e.trys.push([4, 6, , 7]);
75
77
  token = void 0;
76
78
  return [4 /*yield*/, fetchAuthSession()];
77
79
  case 5:
78
- token = (_a = (_d.sent()).tokens) === null || _a === void 0 ? void 0 : _a.accessToken.toString();
80
+ token = (_b = (_e.sent()).tokens) === null || _b === void 0 ? void 0 : _b.accessToken.toString();
79
81
  if (!token) {
80
82
  throw new Error(GraphQLAuthError.NO_FEDERATED_JWT);
81
83
  }
@@ -84,7 +86,7 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
84
86
  };
85
87
  return [3 /*break*/, 7];
86
88
  case 6:
87
- e_1 = _d.sent();
89
+ e_1 = _e.sent();
88
90
  throw new Error(GraphQLAuthError.NO_CURRENT_USER);
89
91
  case 7: return [3 /*break*/, 11];
90
92
  case 8:
@@ -138,7 +140,9 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
138
140
  switch (operationType) {
139
141
  case 'query':
140
142
  case 'mutation':
141
- var responsePromise = this._graphql({ query: query, variables: variables, authMode: authMode }, headers, customUserAgentDetails);
143
+ var abortController = new AbortController();
144
+ var responsePromise = this._graphql({ query: query, variables: variables, authMode: authMode }, headers, abortController, customUserAgentDetails);
145
+ this._api.updateRequestToBeCancellable(responsePromise, abortController);
142
146
  return responsePromise;
143
147
  case 'subscription':
144
148
  return this._graphqlSubscribe({ query: query, variables: variables, authMode: authMode }, headers, customUserAgentDetails);
@@ -146,48 +150,50 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
146
150
  throw new Error("invalid operation type: " + operationType);
147
151
  }
148
152
  };
149
- InternalGraphQLAPIClass.prototype._graphql = function (_a, additionalHeaders, customUserAgentDetails) {
153
+ InternalGraphQLAPIClass.prototype._graphql = function (_a, additionalHeaders, abortController, customUserAgentDetails) {
150
154
  var query = _a.query, variables = _a.variables, authMode = _a.authMode;
151
155
  if (additionalHeaders === void 0) { additionalHeaders = {}; }
156
+ var _b;
152
157
  return __awaiter(this, void 0, void 0, function () {
153
- var config, _b, region, appSyncGraphqlEndpoint, customGraphqlEndpoint, customEndpointRegion, headers, _c, _d, _e, _f, _g, body, endpoint, error, response, responsePayload, err_1, errors;
154
- var _h;
155
- return __generator(this, function (_j) {
156
- switch (_j.label) {
158
+ var config, _c, region, appSyncGraphqlEndpoint, customGraphqlEndpoint, customEndpointRegion, headers, _d, _e, _f, _g, _h, body, endpoint, error, response, responseBody, result, err_1, errors;
159
+ var _j;
160
+ return __generator(this, function (_k) {
161
+ switch (_k.label) {
157
162
  case 0:
158
163
  config = Amplify.getConfig();
159
- _b = config.API.GraphQL, region = _b.region, appSyncGraphqlEndpoint = _b.endpoint;
164
+ _c = ((_b = config.API) === null || _b === void 0 ? void 0 : _b.GraphQL) || {}, region = _c.region, appSyncGraphqlEndpoint = _c.endpoint;
160
165
  customGraphqlEndpoint = null;
161
166
  customEndpointRegion = null;
162
- _c = [{}];
163
- _d = !customGraphqlEndpoint;
164
- if (!_d) return [3 /*break*/, 2];
165
- return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders, customUserAgentDetails)];
167
+ _d = [{}];
168
+ _e = !customGraphqlEndpoint;
169
+ if (!_e) return [3 /*break*/, 2];
170
+ return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders)];
166
171
  case 1:
167
- _d = (_j.sent());
168
- _j.label = 2;
172
+ _e = (_k.sent());
173
+ _k.label = 2;
169
174
  case 2:
170
- _e = [__assign.apply(void 0, _c.concat([(_d)]))];
171
- _f = customGraphqlEndpoint;
172
- if (!_f) return [3 /*break*/, 6];
175
+ _f = [__assign.apply(void 0, _d.concat([(_e)]))];
176
+ _g = customGraphqlEndpoint;
177
+ if (!_g) return [3 /*break*/, 6];
173
178
  if (!customEndpointRegion) return [3 /*break*/, 4];
174
- return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders, customUserAgentDetails)];
179
+ return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders)];
175
180
  case 3:
176
- _g = _j.sent();
181
+ _h = _k.sent();
177
182
  return [3 /*break*/, 5];
178
183
  case 4:
179
- _g = { Authorization: null };
180
- _j.label = 5;
184
+ _h = { Authorization: null };
185
+ _k.label = 5;
181
186
  case 5:
182
- _f = (_g);
183
- _j.label = 6;
187
+ _g = (_h);
188
+ _k.label = 6;
184
189
  case 6:
185
- headers = __assign.apply(void 0, [__assign.apply(void 0, [__assign.apply(void 0, _e.concat([(_f)])), additionalHeaders]), (!customGraphqlEndpoint && (_h = {},
186
- _h[USER_AGENT_HEADER] = getAmplifyUserAgent(customUserAgentDetails),
187
- _h))]);
190
+ headers = __assign.apply(void 0, [__assign.apply(void 0, [__assign.apply(void 0, _f.concat([((_g) ||
191
+ {})])), additionalHeaders]), (!customGraphqlEndpoint && (_j = {},
192
+ _j[USER_AGENT_HEADER] = getAmplifyUserAgent(customUserAgentDetails),
193
+ _j))]);
188
194
  body = {
189
195
  query: print(query),
190
- variables: variables,
196
+ variables: variables || null,
191
197
  };
192
198
  endpoint = customGraphqlEndpoint || appSyncGraphqlEndpoint;
193
199
  if (!endpoint) {
@@ -197,9 +203,9 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
197
203
  errors: [error],
198
204
  };
199
205
  }
200
- _j.label = 7;
206
+ _k.label = 7;
201
207
  case 7:
202
- _j.trys.push([7, 10, , 11]);
208
+ _k.trys.push([7, 10, , 11]);
203
209
  return [4 /*yield*/, this._api.post({
204
210
  url: new URL(endpoint),
205
211
  options: {
@@ -210,15 +216,23 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
210
216
  region: region,
211
217
  },
212
218
  },
219
+ abortController: abortController,
213
220
  })];
214
221
  case 8:
215
- responsePayload = (_j.sent()).body;
216
- return [4 /*yield*/, responsePayload.json()];
222
+ responseBody = (_k.sent()).body;
223
+ return [4 /*yield*/, responseBody.json()];
217
224
  case 9:
218
- response = _j.sent();
225
+ result = _k.sent();
226
+ response = result;
219
227
  return [3 /*break*/, 11];
220
228
  case 10:
221
- err_1 = _j.sent();
229
+ err_1 = _k.sent();
230
+ // If the exception is because user intentionally
231
+ // cancelled the request, do not modify the exception
232
+ // so that clients can identify the exception correctly.
233
+ if (isCancelErrorREST(err_1)) {
234
+ throw err_1;
235
+ }
222
236
  response = {
223
237
  data: {},
224
238
  errors: [new GraphQLError(err_1.message, null, null, null, null, err_1)],
@@ -234,8 +248,24 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
234
248
  });
235
249
  });
236
250
  };
251
+ /**
252
+ * Checks to see if an error thrown is from an api request cancellation
253
+ * @param {any} error - Any error
254
+ * @return {boolean} - A boolean indicating if the error was from an api request cancellation
255
+ */
256
+ InternalGraphQLAPIClass.prototype.isCancelError = function (error) {
257
+ return isCancelErrorREST(error);
258
+ };
259
+ /**
260
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
261
+ * @param {any} request - request to cancel
262
+ * @returns - A boolean indicating if the request was cancelled
263
+ */
264
+ InternalGraphQLAPIClass.prototype.cancel = function (request, message) {
265
+ return cancelREST(request, message);
266
+ };
237
267
  InternalGraphQLAPIClass.prototype._graphqlSubscribe = function (_a, additionalHeaders, customUserAgentDetails) {
238
- var query = _a.query, variables = _a.variables, defaultAuthenticationType = _a.authMode, authToken = _a.authToken;
268
+ var query = _a.query, variables = _a.variables, authMode = _a.authMode;
239
269
  if (additionalHeaders === void 0) { additionalHeaders = {}; }
240
270
  var _b;
241
271
  var GraphQL = ((_b = Amplify.getConfig().API) !== null && _b !== void 0 ? _b : {}).GraphQL;
@@ -247,9 +277,10 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
247
277
  variables: variables,
248
278
  appSyncGraphqlEndpoint: GraphQL === null || GraphQL === void 0 ? void 0 : GraphQL.endpoint,
249
279
  region: GraphQL === null || GraphQL === void 0 ? void 0 : GraphQL.region,
250
- authenticationType: GraphQL === null || GraphQL === void 0 ? void 0 : GraphQL.defaultAuthMode,
280
+ authenticationType: authMode !== null && authMode !== void 0 ? authMode : GraphQL === null || GraphQL === void 0 ? void 0 : GraphQL.defaultAuthMode,
251
281
  apiKey: GraphQL === null || GraphQL === void 0 ? void 0 : GraphQL.apiKey,
252
- });
282
+ additionalHeaders: additionalHeaders,
283
+ }, customUserAgentDetails);
253
284
  };
254
285
  return InternalGraphQLAPIClass;
255
286
  }());
@@ -1,2 +1,2 @@
1
1
  export { InternalGraphQLAPI, InternalGraphQLAPIClass, } from './InternalGraphQLAPI';
2
- export { graphql } from './v6';
2
+ export { graphql, cancel, isCancelError } from './v6';
@@ -1,4 +1,4 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  export { InternalGraphQLAPI, InternalGraphQLAPIClass, } from './InternalGraphQLAPI';
4
- export { graphql } from './v6';
4
+ export { graphql, cancel, isCancelError } from './v6';
@@ -86,4 +86,16 @@ import { GraphQLOptionsV6, GraphQLResponseV6 } from '../types';
86
86
  export declare function graphql<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
87
87
  [key: string]: string;
88
88
  }): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
89
+ /**
90
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
91
+ * @param {any} request - request to cancel
92
+ * @returns - A boolean indicating if the request was cancelled
93
+ */
94
+ export declare function cancel(promise: Promise<any>, message?: string): boolean;
95
+ /**
96
+ * Checks to see if an error thrown is from an api request cancellation
97
+ * @param {any} error - Any error
98
+ * @returns - A boolean indicating if the error was from an api request cancellation
99
+ */
100
+ export declare function isCancelError(error: any): boolean;
89
101
  export { GraphQLOptionsV6, GraphQLResponseV6 };
@@ -94,3 +94,19 @@ export function graphql(options, additionalHeaders) {
94
94
  var result = GraphQLAPI.graphql(options, additionalHeaders);
95
95
  return result;
96
96
  }
97
+ /**
98
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
99
+ * @param {any} request - request to cancel
100
+ * @returns - A boolean indicating if the request was cancelled
101
+ */
102
+ export function cancel(promise, message) {
103
+ return GraphQLAPI.cancel(promise, message);
104
+ }
105
+ /**
106
+ * Checks to see if an error thrown is from an api request cancellation
107
+ * @param {any} error - Any error
108
+ * @returns - A boolean indicating if the error was from an api request cancellation
109
+ */
110
+ export function isCancelError(error) {
111
+ return GraphQLAPI.isCancelError(error);
112
+ }