@aws-amplify/api-graphql 4.0.1-console-preview.b5a42af.0 → 4.0.1-console-preview.4ae78ed.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/api-graphql",
3
- "version": "4.0.1-console-preview.b5a42af.0+b5a42af",
3
+ "version": "4.0.1-console-preview.4ae78ed.0+4ae78ed",
4
4
  "description": "Api-graphql category of aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -47,13 +47,14 @@
47
47
  "internals"
48
48
  ],
49
49
  "dependencies": {
50
- "@aws-amplify/api-rest": "4.0.1-console-preview.b5a42af.0+b5a42af",
51
- "@aws-amplify/auth": "6.0.1-console-preview.b5a42af.0+b5a42af",
52
- "@aws-amplify/core": "6.0.1-console-preview.b5a42af.0+b5a42af",
50
+ "@aws-amplify/api-rest": "4.0.1-console-preview.4ae78ed.0+4ae78ed",
51
+ "@aws-amplify/auth": "6.0.1-console-preview.4ae78ed.0+4ae78ed",
52
+ "@aws-amplify/core": "6.0.1-console-preview.4ae78ed.0+4ae78ed",
53
53
  "@aws-sdk/types": "3.387.0",
54
54
  "graphql": "15.8.0",
55
55
  "rxjs": "^7.8.1",
56
56
  "tslib": "^1.8.0",
57
+ "url": "0.11.0",
57
58
  "uuid": "^3.2.1"
58
59
  },
59
60
  "size-limit": [
@@ -120,5 +121,5 @@
120
121
  "<rootDir>/__tests__"
121
122
  ]
122
123
  },
123
- "gitHead": "b5a42af519fb741bb8056a814addd8804c8f60ec"
124
+ "gitHead": "4ae78ed23fc03ae718488c98fc48782d2bd6f5f3"
124
125
  }
package/src/GraphQLAPI.ts CHANGED
@@ -35,6 +35,24 @@ export class GraphQLAPIClass extends InternalGraphQLAPIClass {
35
35
  ): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
36
36
  return super.graphql(options, additionalHeaders);
37
37
  }
38
+
39
+ /**
40
+ * Checks to see if an error thrown is from an api request cancellation
41
+ * @param error - Any error
42
+ * @returns A boolean indicating if the error was from an api request cancellation
43
+ */
44
+ isCancelError(error: any): boolean {
45
+ return super.isCancelError(error);
46
+ }
47
+
48
+ /**
49
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
50
+ * @param {any} request - request to cancel
51
+ * @returns A boolean indicating if the request was cancelled
52
+ */
53
+ cancel(request: Promise<any>, message?: string): boolean {
54
+ return super.cancel(request, message);
55
+ }
38
56
  }
39
57
 
40
58
  export const GraphQLAPI = new GraphQLAPIClass(null);
@@ -11,6 +11,7 @@ import {
11
11
  import { Observable } from 'rxjs';
12
12
  import { Amplify, Cache, fetchAuthSession } from '@aws-amplify/core';
13
13
  import {
14
+ APIAuthMode,
14
15
  CustomUserAgentDetails,
15
16
  ConsoleLogger as Logger,
16
17
  getAmplifyUserAgent,
@@ -21,7 +22,12 @@ import {
21
22
  GraphQLOperation,
22
23
  GraphQLOptions,
23
24
  } from '../types';
24
- import { post } from '@aws-amplify/api-rest/internals';
25
+ import { isCancelError as isCancelErrorREST } from '@aws-amplify/api-rest';
26
+ import {
27
+ post,
28
+ cancel as cancelREST,
29
+ updateRequestToBeCancellable,
30
+ } from '@aws-amplify/api-rest/internals';
25
31
  import { AWSAppSyncRealTimeProvider } from '../Providers/AWSAppSyncRealTimeProvider';
26
32
 
27
33
  const USER_AGENT_HEADER = 'x-amz-user-agent';
@@ -49,7 +55,7 @@ export class InternalGraphQLAPIClass {
49
55
  private appSyncRealTime: AWSAppSyncRealTimeProvider | null;
50
56
 
51
57
  Cache = Cache;
52
- private _api = { post };
58
+ private _api = { post, updateRequestToBeCancellable };
53
59
 
54
60
  /**
55
61
  * Initialize GraphQL API with AWS configuration
@@ -65,9 +71,8 @@ export class InternalGraphQLAPIClass {
65
71
  }
66
72
 
67
73
  private async _headerBasedAuth(
68
- defaultAuthenticationType?,
69
- additionalHeaders: { [key: string]: string } = {},
70
- customUserAgentDetails?: CustomUserAgentDetails
74
+ authMode: APIAuthMode,
75
+ additionalHeaders: { [key: string]: string } = {}
71
76
  ) {
72
77
  const config = Amplify.getConfig();
73
78
  const {
@@ -77,9 +82,10 @@ export class InternalGraphQLAPIClass {
77
82
  defaultAuthMode,
78
83
  } = config.API.GraphQL;
79
84
 
85
+ const authenticationType = authMode || defaultAuthMode || 'iam';
80
86
  let headers = {};
81
87
 
82
- switch (defaultAuthMode) {
88
+ switch (authenticationType) {
83
89
  case 'apiKey':
84
90
  if (!apiKey) {
85
91
  throw new Error(GraphQLAuthError.NO_API_KEY);
@@ -177,11 +183,17 @@ export class InternalGraphQLAPIClass {
177
183
  switch (operationType) {
178
184
  case 'query':
179
185
  case 'mutation':
186
+ const abortController = new AbortController();
180
187
  const responsePromise = this._graphql<T>(
181
188
  { query, variables, authMode },
182
189
  headers,
190
+ abortController,
183
191
  customUserAgentDetails
184
192
  );
193
+ this._api.updateRequestToBeCancellable(
194
+ responsePromise,
195
+ abortController
196
+ );
185
197
  return responsePromise;
186
198
  case 'subscription':
187
199
  return this._graphqlSubscribe(
@@ -197,6 +209,7 @@ export class InternalGraphQLAPIClass {
197
209
  private async _graphql<T = any>(
198
210
  { query, variables, authMode }: GraphQLOptions,
199
211
  additionalHeaders = {},
212
+ abortController: AbortController,
200
213
  customUserAgentDetails?: CustomUserAgentDetails
201
214
  ): Promise<GraphQLResult<T>> {
202
215
  const config = Amplify.getConfig();
@@ -209,18 +222,10 @@ export class InternalGraphQLAPIClass {
209
222
 
210
223
  const headers = {
211
224
  ...(!customGraphqlEndpoint &&
212
- (await this._headerBasedAuth(
213
- authMode,
214
- additionalHeaders,
215
- customUserAgentDetails
216
- ))),
225
+ (await this._headerBasedAuth(authMode, additionalHeaders))),
217
226
  ...(customGraphqlEndpoint &&
218
227
  (customEndpointRegion
219
- ? await this._headerBasedAuth(
220
- authMode,
221
- additionalHeaders,
222
- customUserAgentDetails
223
- )
228
+ ? await this._headerBasedAuth(authMode, additionalHeaders)
224
229
  : { Authorization: null })),
225
230
  ...additionalHeaders,
226
231
  ...(!customGraphqlEndpoint && {
@@ -246,7 +251,7 @@ export class InternalGraphQLAPIClass {
246
251
 
247
252
  let response;
248
253
  try {
249
- const { body: responsePayload } = await this._api.post({
254
+ const { body: responseBody } = await this._api.post({
250
255
  url: new URL(endpoint),
251
256
  options: {
252
257
  headers,
@@ -256,9 +261,20 @@ export class InternalGraphQLAPIClass {
256
261
  region,
257
262
  },
258
263
  },
264
+ abortController,
259
265
  });
260
- response = await responsePayload.json();
266
+
267
+ const result = await responseBody.json();
268
+
269
+ response = result;
261
270
  } catch (err) {
271
+ // If the exception is because user intentionally
272
+ // cancelled the request, do not modify the exception
273
+ // so that clients can identify the exception correctly.
274
+ if (isCancelErrorREST(err)) {
275
+ throw err;
276
+ }
277
+
262
278
  response = {
263
279
  data: {},
264
280
  errors: [new GraphQLError(err.message, null, null, null, null, err)],
@@ -274,13 +290,26 @@ export class InternalGraphQLAPIClass {
274
290
  return response;
275
291
  }
276
292
 
293
+ /**
294
+ * Checks to see if an error thrown is from an api request cancellation
295
+ * @param {any} error - Any error
296
+ * @return {boolean} - A boolean indicating if the error was from an api request cancellation
297
+ */
298
+ isCancelError(error: any): boolean {
299
+ return isCancelErrorREST(error);
300
+ }
301
+
302
+ /**
303
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
304
+ * @param {any} request - request to cancel
305
+ * @returns - A boolean indicating if the request was cancelled
306
+ */
307
+ cancel(request: Promise<any>, message?: string): boolean {
308
+ return cancelREST(request, message);
309
+ }
310
+
277
311
  private _graphqlSubscribe(
278
- {
279
- query,
280
- variables,
281
- authMode: defaultAuthenticationType,
282
- authToken,
283
- }: GraphQLOptions,
312
+ { query, variables, authMode }: GraphQLOptions,
284
313
  additionalHeaders = {},
285
314
  customUserAgentDetails?: CustomUserAgentDetails
286
315
  ): Observable<any> {
@@ -288,14 +317,18 @@ export class InternalGraphQLAPIClass {
288
317
  if (!this.appSyncRealTime) {
289
318
  this.appSyncRealTime = new AWSAppSyncRealTimeProvider();
290
319
  }
291
- return this.appSyncRealTime.subscribe({
292
- query: print(query as DocumentNode),
293
- variables,
294
- appSyncGraphqlEndpoint: GraphQL?.endpoint,
295
- region: GraphQL?.region,
296
- authenticationType: GraphQL?.defaultAuthMode,
297
- apiKey: GraphQL?.apiKey,
298
- });
320
+ return this.appSyncRealTime.subscribe(
321
+ {
322
+ query: print(query as DocumentNode),
323
+ variables,
324
+ appSyncGraphqlEndpoint: GraphQL?.endpoint,
325
+ region: GraphQL?.region,
326
+ authenticationType: authMode ?? GraphQL?.defaultAuthMode,
327
+ apiKey: GraphQL?.apiKey,
328
+ additionalHeaders,
329
+ },
330
+ customUserAgentDetails
331
+ );
299
332
  }
300
333
  }
301
334
 
@@ -5,4 +5,4 @@ export {
5
5
  InternalGraphQLAPIClass,
6
6
  } from './InternalGraphQLAPI';
7
7
 
8
- export { graphql } from './v6';
8
+ export { graphql, cancel, isCancelError } from './v6';
@@ -103,4 +103,22 @@ export function graphql<
103
103
  return result as any;
104
104
  }
105
105
 
106
+ /**
107
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
108
+ * @param {any} request - request to cancel
109
+ * @returns - A boolean indicating if the request was cancelled
110
+ */
111
+ export function cancel(promise: Promise<any>, message?: string): boolean {
112
+ return GraphQLAPI.cancel(promise, message);
113
+ }
114
+
115
+ /**
116
+ * Checks to see if an error thrown is from an api request cancellation
117
+ * @param {any} error - Any error
118
+ * @returns - A boolean indicating if the error was from an api request cancellation
119
+ */
120
+ export function isCancelError(error: any): boolean {
121
+ return GraphQLAPI.isCancelError(error);
122
+ }
123
+
106
124
  export { GraphQLOptionsV6, GraphQLResponseV6 };
@@ -12,7 +12,7 @@ export { CONTROL_MSG, ConnectionState } from './PubSub';
12
12
  export interface GraphQLOptions {
13
13
  query: string | DocumentNode;
14
14
  variables?: Record<string, DocumentType>;
15
- authMode?: string;
15
+ authMode?: APIAuthMode;
16
16
  authToken?: string;
17
17
  /**
18
18
  * @deprecated This property should not be used