@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.
@@ -1,10 +1,10 @@
1
1
  import { GraphQLOptions, GraphQLResult } from './types';
2
2
  import { InternalGraphQLAPIClass } from './internals';
3
3
  import { Observable } from 'rxjs';
4
- export declare const graphqlOperation: (query: any, variables?: {}, authToken?: string) => {
4
+ export declare const graphqlOperation: (query: any, variables?: {}, authToken?: string | undefined) => {
5
5
  query: any;
6
6
  variables: {};
7
- authToken: string;
7
+ authToken: string | undefined;
8
8
  };
9
9
  /**
10
10
  * Export Cloud Logic APIs
@@ -21,5 +21,17 @@ export declare class GraphQLAPIClass extends InternalGraphQLAPIClass {
21
21
  graphql<T = any>(options: GraphQLOptions, additionalHeaders?: {
22
22
  [key: string]: string;
23
23
  }): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>>;
24
+ /**
25
+ * Checks to see if an error thrown is from an api request cancellation
26
+ * @param error - Any error
27
+ * @returns A boolean indicating if the error was from an api request cancellation
28
+ */
29
+ isCancelError(error: any): boolean;
30
+ /**
31
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
32
+ * @param {any} request - request to cancel
33
+ * @returns A boolean indicating if the request was cancelled
34
+ */
35
+ cancel(request: Promise<any>, message?: string): boolean;
24
36
  }
25
37
  export declare const GraphQLAPI: GraphQLAPIClass;
package/lib/GraphQLAPI.js CHANGED
@@ -31,6 +31,22 @@ var GraphQLAPIClass = /** @class */ (function (_super) {
31
31
  GraphQLAPIClass.prototype.graphql = function (options, additionalHeaders) {
32
32
  return _super.prototype.graphql.call(this, options, additionalHeaders);
33
33
  };
34
+ /**
35
+ * Checks to see if an error thrown is from an api request cancellation
36
+ * @param error - Any error
37
+ * @returns A boolean indicating if the error was from an api request cancellation
38
+ */
39
+ GraphQLAPIClass.prototype.isCancelError = function (error) {
40
+ return _super.prototype.isCancelError.call(this, error);
41
+ };
42
+ /**
43
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
44
+ * @param {any} request - request to cancel
45
+ * @returns A boolean indicating if the request was cancelled
46
+ */
47
+ GraphQLAPIClass.prototype.cancel = function (request, message) {
48
+ return _super.prototype.cancel.call(this, request, message);
49
+ };
34
50
  return GraphQLAPIClass;
35
51
  }(internals_1.InternalGraphQLAPIClass));
36
52
  exports.GraphQLAPIClass = GraphQLAPIClass;
@@ -781,6 +781,7 @@ var AWSAppSyncRealTimeProvider = /** @class */ (function () {
781
781
  url: new URL(request.url),
782
782
  body: request.data,
783
783
  }, {
784
+ // TODO: What do we need to do to remove these !'s?
784
785
  credentials: creds,
785
786
  signingRegion: endpointInfo.region,
786
787
  signingService: endpointInfo.service,
@@ -2,10 +2,10 @@ import { OperationTypeNode } from 'graphql';
2
2
  import { Observable } from 'rxjs';
3
3
  import { CustomUserAgentDetails } from '@aws-amplify/core/internals/utils';
4
4
  import { GraphQLResult, GraphQLOperation, GraphQLOptions } from '../types';
5
- export declare const graphqlOperation: (query: any, variables?: {}, authToken?: string) => {
5
+ export declare const graphqlOperation: (query: any, variables?: {}, authToken?: string | undefined) => {
6
6
  query: any;
7
7
  variables: {};
8
- authToken: string;
8
+ authToken: string | undefined;
9
9
  };
10
10
  /**
11
11
  * Export Cloud Logic APIs
@@ -41,6 +41,18 @@ export declare class InternalGraphQLAPIClass {
41
41
  [key: string]: string;
42
42
  }, customUserAgentDetails?: CustomUserAgentDetails): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>>;
43
43
  private _graphql;
44
+ /**
45
+ * Checks to see if an error thrown is from an api request cancellation
46
+ * @param {any} error - Any error
47
+ * @return {boolean} - A boolean indicating if the error was from an api request cancellation
48
+ */
49
+ isCancelError(error: any): boolean;
50
+ /**
51
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
52
+ * @param {any} request - request to cancel
53
+ * @returns - A boolean indicating if the request was cancelled
54
+ */
55
+ cancel(request: Promise<any>, message?: string): boolean;
44
56
  private _graphqlSubscribe;
45
57
  }
46
58
  export declare const InternalGraphQLAPI: InternalGraphQLAPIClass;
@@ -7,6 +7,7 @@ var graphql_1 = require("graphql");
7
7
  var core_1 = require("@aws-amplify/core");
8
8
  var utils_1 = require("@aws-amplify/core/internals/utils");
9
9
  var types_1 = require("../types");
10
+ var api_rest_1 = require("@aws-amplify/api-rest");
10
11
  var internals_1 = require("@aws-amplify/api-rest/internals");
11
12
  var AWSAppSyncRealTimeProvider_1 = require("../Providers/AWSAppSyncRealTimeProvider");
12
13
  var USER_AGENT_HEADER = 'x-amz-user-agent';
@@ -29,26 +30,27 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
29
30
  */
30
31
  function InternalGraphQLAPIClass(options) {
31
32
  this.Cache = core_1.Cache;
32
- this._api = { post: internals_1.post };
33
+ this._api = { post: internals_1.post, updateRequestToBeCancellable: internals_1.updateRequestToBeCancellable };
33
34
  this._options = options;
34
35
  logger.debug('API Options', this._options);
35
36
  }
36
37
  InternalGraphQLAPIClass.prototype.getModuleName = function () {
37
38
  return 'InternalGraphQLAPI';
38
39
  };
39
- InternalGraphQLAPIClass.prototype._headerBasedAuth = function (defaultAuthenticationType, additionalHeaders, customUserAgentDetails) {
40
+ InternalGraphQLAPIClass.prototype._headerBasedAuth = function (authMode, additionalHeaders) {
40
41
  if (additionalHeaders === void 0) { additionalHeaders = {}; }
41
- var _a;
42
+ var _a, _b;
42
43
  return tslib_1.__awaiter(this, void 0, void 0, function () {
43
- var config, _b, region, appSyncGraphqlEndpoint, apiKey, defaultAuthMode, headers, _c, session, token, e_1;
44
- return tslib_1.__generator(this, function (_d) {
45
- switch (_d.label) {
44
+ var config, _c, region, appSyncGraphqlEndpoint, apiKey, defaultAuthMode, authenticationType, headers, _d, session, token, e_1;
45
+ return tslib_1.__generator(this, function (_e) {
46
+ switch (_e.label) {
46
47
  case 0:
47
48
  config = core_1.Amplify.getConfig();
48
- _b = config.API.GraphQL, region = _b.region, appSyncGraphqlEndpoint = _b.endpoint, apiKey = _b.apiKey, defaultAuthMode = _b.defaultAuthMode;
49
+ _c = ((_a = config.API) === null || _a === void 0 ? void 0 : _a.GraphQL) || {}, region = _c.region, appSyncGraphqlEndpoint = _c.endpoint, apiKey = _c.apiKey, defaultAuthMode = _c.defaultAuthMode;
50
+ authenticationType = authMode || defaultAuthMode || 'iam';
49
51
  headers = {};
50
- _c = defaultAuthMode;
51
- switch (_c) {
52
+ _d = authenticationType;
53
+ switch (_d) {
52
54
  case 'apiKey': return [3 /*break*/, 1];
53
55
  case 'iam': return [3 /*break*/, 2];
54
56
  case 'oidc': return [3 /*break*/, 4];
@@ -67,17 +69,17 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
67
69
  return [3 /*break*/, 11];
68
70
  case 2: return [4 /*yield*/, core_1.fetchAuthSession()];
69
71
  case 3:
70
- session = _d.sent();
72
+ session = _e.sent();
71
73
  if (session.credentials === undefined) {
72
74
  throw new Error(types_1.GraphQLAuthError.NO_CREDENTIALS);
73
75
  }
74
76
  return [3 /*break*/, 11];
75
77
  case 4:
76
- _d.trys.push([4, 6, , 7]);
78
+ _e.trys.push([4, 6, , 7]);
77
79
  token = void 0;
78
80
  return [4 /*yield*/, core_1.fetchAuthSession()];
79
81
  case 5:
80
- token = (_a = (_d.sent()).tokens) === null || _a === void 0 ? void 0 : _a.accessToken.toString();
82
+ token = (_b = (_e.sent()).tokens) === null || _b === void 0 ? void 0 : _b.accessToken.toString();
81
83
  if (!token) {
82
84
  throw new Error(types_1.GraphQLAuthError.NO_FEDERATED_JWT);
83
85
  }
@@ -86,7 +88,7 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
86
88
  };
87
89
  return [3 /*break*/, 7];
88
90
  case 6:
89
- e_1 = _d.sent();
91
+ e_1 = _e.sent();
90
92
  throw new Error(types_1.GraphQLAuthError.NO_CURRENT_USER);
91
93
  case 7: return [3 /*break*/, 11];
92
94
  case 8:
@@ -140,7 +142,9 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
140
142
  switch (operationType) {
141
143
  case 'query':
142
144
  case 'mutation':
143
- var responsePromise = this._graphql({ query: query, variables: variables, authMode: authMode }, headers, customUserAgentDetails);
145
+ var abortController = new AbortController();
146
+ var responsePromise = this._graphql({ query: query, variables: variables, authMode: authMode }, headers, abortController, customUserAgentDetails);
147
+ this._api.updateRequestToBeCancellable(responsePromise, abortController);
144
148
  return responsePromise;
145
149
  case 'subscription':
146
150
  return this._graphqlSubscribe({ query: query, variables: variables, authMode: authMode }, headers, customUserAgentDetails);
@@ -148,48 +152,50 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
148
152
  throw new Error("invalid operation type: " + operationType);
149
153
  }
150
154
  };
151
- InternalGraphQLAPIClass.prototype._graphql = function (_a, additionalHeaders, customUserAgentDetails) {
155
+ InternalGraphQLAPIClass.prototype._graphql = function (_a, additionalHeaders, abortController, customUserAgentDetails) {
152
156
  var query = _a.query, variables = _a.variables, authMode = _a.authMode;
153
157
  if (additionalHeaders === void 0) { additionalHeaders = {}; }
158
+ var _b;
154
159
  return tslib_1.__awaiter(this, void 0, void 0, function () {
155
- var config, _b, region, appSyncGraphqlEndpoint, customGraphqlEndpoint, customEndpointRegion, headers, _c, _d, _e, _f, _g, body, endpoint, error, response, responsePayload, err_1, errors;
156
- var _h;
157
- return tslib_1.__generator(this, function (_j) {
158
- switch (_j.label) {
160
+ var config, _c, region, appSyncGraphqlEndpoint, customGraphqlEndpoint, customEndpointRegion, headers, _d, _e, _f, _g, _h, body, endpoint, error, response, responseBody, result, err_1, errors;
161
+ var _j;
162
+ return tslib_1.__generator(this, function (_k) {
163
+ switch (_k.label) {
159
164
  case 0:
160
165
  config = core_1.Amplify.getConfig();
161
- _b = config.API.GraphQL, region = _b.region, appSyncGraphqlEndpoint = _b.endpoint;
166
+ _c = ((_b = config.API) === null || _b === void 0 ? void 0 : _b.GraphQL) || {}, region = _c.region, appSyncGraphqlEndpoint = _c.endpoint;
162
167
  customGraphqlEndpoint = null;
163
168
  customEndpointRegion = null;
164
- _c = [{}];
165
- _d = !customGraphqlEndpoint;
166
- if (!_d) return [3 /*break*/, 2];
167
- return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders, customUserAgentDetails)];
169
+ _d = [{}];
170
+ _e = !customGraphqlEndpoint;
171
+ if (!_e) return [3 /*break*/, 2];
172
+ return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders)];
168
173
  case 1:
169
- _d = (_j.sent());
170
- _j.label = 2;
174
+ _e = (_k.sent());
175
+ _k.label = 2;
171
176
  case 2:
172
- _e = [tslib_1.__assign.apply(void 0, _c.concat([(_d)]))];
173
- _f = customGraphqlEndpoint;
174
- if (!_f) return [3 /*break*/, 6];
177
+ _f = [tslib_1.__assign.apply(void 0, _d.concat([(_e)]))];
178
+ _g = customGraphqlEndpoint;
179
+ if (!_g) return [3 /*break*/, 6];
175
180
  if (!customEndpointRegion) return [3 /*break*/, 4];
176
- return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders, customUserAgentDetails)];
181
+ return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders)];
177
182
  case 3:
178
- _g = _j.sent();
183
+ _h = _k.sent();
179
184
  return [3 /*break*/, 5];
180
185
  case 4:
181
- _g = { Authorization: null };
182
- _j.label = 5;
186
+ _h = { Authorization: null };
187
+ _k.label = 5;
183
188
  case 5:
184
- _f = (_g);
185
- _j.label = 6;
189
+ _g = (_h);
190
+ _k.label = 6;
186
191
  case 6:
187
- headers = tslib_1.__assign.apply(void 0, [tslib_1.__assign.apply(void 0, [tslib_1.__assign.apply(void 0, _e.concat([(_f)])), additionalHeaders]), (!customGraphqlEndpoint && (_h = {},
188
- _h[USER_AGENT_HEADER] = utils_1.getAmplifyUserAgent(customUserAgentDetails),
189
- _h))]);
192
+ headers = tslib_1.__assign.apply(void 0, [tslib_1.__assign.apply(void 0, [tslib_1.__assign.apply(void 0, _f.concat([((_g) ||
193
+ {})])), additionalHeaders]), (!customGraphqlEndpoint && (_j = {},
194
+ _j[USER_AGENT_HEADER] = utils_1.getAmplifyUserAgent(customUserAgentDetails),
195
+ _j))]);
190
196
  body = {
191
197
  query: graphql_1.print(query),
192
- variables: variables,
198
+ variables: variables || null,
193
199
  };
194
200
  endpoint = customGraphqlEndpoint || appSyncGraphqlEndpoint;
195
201
  if (!endpoint) {
@@ -199,9 +205,9 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
199
205
  errors: [error],
200
206
  };
201
207
  }
202
- _j.label = 7;
208
+ _k.label = 7;
203
209
  case 7:
204
- _j.trys.push([7, 10, , 11]);
210
+ _k.trys.push([7, 10, , 11]);
205
211
  return [4 /*yield*/, this._api.post({
206
212
  url: new URL(endpoint),
207
213
  options: {
@@ -212,15 +218,23 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
212
218
  region: region,
213
219
  },
214
220
  },
221
+ abortController: abortController,
215
222
  })];
216
223
  case 8:
217
- responsePayload = (_j.sent()).body;
218
- return [4 /*yield*/, responsePayload.json()];
224
+ responseBody = (_k.sent()).body;
225
+ return [4 /*yield*/, responseBody.json()];
219
226
  case 9:
220
- response = _j.sent();
227
+ result = _k.sent();
228
+ response = result;
221
229
  return [3 /*break*/, 11];
222
230
  case 10:
223
- err_1 = _j.sent();
231
+ err_1 = _k.sent();
232
+ // If the exception is because user intentionally
233
+ // cancelled the request, do not modify the exception
234
+ // so that clients can identify the exception correctly.
235
+ if (api_rest_1.isCancelError(err_1)) {
236
+ throw err_1;
237
+ }
224
238
  response = {
225
239
  data: {},
226
240
  errors: [new graphql_1.GraphQLError(err_1.message, null, null, null, null, err_1)],
@@ -236,8 +250,24 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
236
250
  });
237
251
  });
238
252
  };
253
+ /**
254
+ * Checks to see if an error thrown is from an api request cancellation
255
+ * @param {any} error - Any error
256
+ * @return {boolean} - A boolean indicating if the error was from an api request cancellation
257
+ */
258
+ InternalGraphQLAPIClass.prototype.isCancelError = function (error) {
259
+ return api_rest_1.isCancelError(error);
260
+ };
261
+ /**
262
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
263
+ * @param {any} request - request to cancel
264
+ * @returns - A boolean indicating if the request was cancelled
265
+ */
266
+ InternalGraphQLAPIClass.prototype.cancel = function (request, message) {
267
+ return internals_1.cancel(request, message);
268
+ };
239
269
  InternalGraphQLAPIClass.prototype._graphqlSubscribe = function (_a, additionalHeaders, customUserAgentDetails) {
240
- var query = _a.query, variables = _a.variables, defaultAuthenticationType = _a.authMode, authToken = _a.authToken;
270
+ var query = _a.query, variables = _a.variables, authMode = _a.authMode;
241
271
  if (additionalHeaders === void 0) { additionalHeaders = {}; }
242
272
  var _b;
243
273
  var GraphQL = ((_b = core_1.Amplify.getConfig().API) !== null && _b !== void 0 ? _b : {}).GraphQL;
@@ -249,9 +279,10 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
249
279
  variables: variables,
250
280
  appSyncGraphqlEndpoint: GraphQL === null || GraphQL === void 0 ? void 0 : GraphQL.endpoint,
251
281
  region: GraphQL === null || GraphQL === void 0 ? void 0 : GraphQL.region,
252
- authenticationType: GraphQL === null || GraphQL === void 0 ? void 0 : GraphQL.defaultAuthMode,
282
+ authenticationType: authMode !== null && authMode !== void 0 ? authMode : GraphQL === null || GraphQL === void 0 ? void 0 : GraphQL.defaultAuthMode,
253
283
  apiKey: GraphQL === null || GraphQL === void 0 ? void 0 : GraphQL.apiKey,
254
- });
284
+ additionalHeaders: additionalHeaders,
285
+ }, customUserAgentDetails);
255
286
  };
256
287
  return InternalGraphQLAPIClass;
257
288
  }());
@@ -1,2 +1,2 @@
1
1
  export { InternalGraphQLAPI, InternalGraphQLAPIClass, } from './InternalGraphQLAPI';
2
- export { graphql } from './v6';
2
+ export { graphql, cancel, isCancelError } from './v6';
@@ -7,3 +7,5 @@ exports.InternalGraphQLAPI = InternalGraphQLAPI_1.InternalGraphQLAPI;
7
7
  exports.InternalGraphQLAPIClass = InternalGraphQLAPI_1.InternalGraphQLAPIClass;
8
8
  var v6_1 = require("./v6");
9
9
  exports.graphql = v6_1.graphql;
10
+ exports.cancel = v6_1.cancel;
11
+ exports.isCancelError = v6_1.isCancelError;
@@ -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 };
@@ -97,3 +97,21 @@ function graphql(options, additionalHeaders) {
97
97
  return result;
98
98
  }
99
99
  exports.graphql = graphql;
100
+ /**
101
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
102
+ * @param {any} request - request to cancel
103
+ * @returns - A boolean indicating if the request was cancelled
104
+ */
105
+ function cancel(promise, message) {
106
+ return GraphQLAPI_1.GraphQLAPI.cancel(promise, message);
107
+ }
108
+ exports.cancel = cancel;
109
+ /**
110
+ * Checks to see if an error thrown is from an api request cancellation
111
+ * @param {any} error - Any error
112
+ * @returns - A boolean indicating if the error was from an api request cancellation
113
+ */
114
+ function isCancelError(error) {
115
+ return GraphQLAPI_1.GraphQLAPI.isCancelError(error);
116
+ }
117
+ exports.isCancelError = isCancelError;