@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
@@ -10,31 +10,30 @@ export var ReconnectEvent;
10
10
  * Attempting to reconnect only once creates unrecoverable states when the network state isn't
11
11
  * supported by the browser, so this keeps retrying every minute until halted.
12
12
  */
13
- var ReconnectionMonitor = /** @class */ (function () {
14
- function ReconnectionMonitor() {
13
+ export class ReconnectionMonitor {
14
+ constructor() {
15
15
  this.reconnectObservers = [];
16
16
  }
17
17
  /**
18
18
  * Add reconnect observer to the list of observers to alert on reconnect
19
19
  */
20
- ReconnectionMonitor.prototype.addObserver = function (reconnectObserver) {
20
+ addObserver(reconnectObserver) {
21
21
  this.reconnectObservers.push(reconnectObserver);
22
- };
22
+ }
23
23
  /**
24
24
  * Given a reconnect event, start the appropriate behavior
25
25
  */
26
- ReconnectionMonitor.prototype.record = function (event) {
27
- var _this = this;
26
+ record(event) {
28
27
  if (event === ReconnectEvent.START_RECONNECT) {
29
28
  // If the reconnection hasn't been started
30
29
  if (this.reconnectSetTimeoutId === undefined &&
31
30
  this.reconnectIntervalId === undefined) {
32
- this.reconnectSetTimeoutId = setTimeout(function () {
31
+ this.reconnectSetTimeoutId = setTimeout(() => {
33
32
  // Reconnect now
34
- _this._triggerReconnect();
33
+ this._triggerReconnect();
35
34
  // Retry reconnect every periodically until it works
36
- _this.reconnectIntervalId = setInterval(function () {
37
- _this._triggerReconnect();
35
+ this.reconnectIntervalId = setInterval(() => {
36
+ this._triggerReconnect();
38
37
  }, RECONNECT_INTERVAL);
39
38
  }, RECONNECT_DELAY);
40
39
  }
@@ -49,22 +48,18 @@ var ReconnectionMonitor = /** @class */ (function () {
49
48
  this.reconnectSetTimeoutId = undefined;
50
49
  }
51
50
  }
52
- };
51
+ }
53
52
  /**
54
53
  * Complete all reconnect observers
55
54
  */
56
- ReconnectionMonitor.prototype.close = function () {
57
- this.reconnectObservers.forEach(function (reconnectObserver) {
58
- var _a;
59
- (_a = reconnectObserver.complete) === null || _a === void 0 ? void 0 : _a.call(reconnectObserver);
55
+ close() {
56
+ this.reconnectObservers.forEach(reconnectObserver => {
57
+ reconnectObserver.complete?.();
60
58
  });
61
- };
62
- ReconnectionMonitor.prototype._triggerReconnect = function () {
63
- this.reconnectObservers.forEach(function (reconnectObserver) {
64
- var _a;
65
- (_a = reconnectObserver.next) === null || _a === void 0 ? void 0 : _a.call(reconnectObserver);
59
+ }
60
+ _triggerReconnect() {
61
+ this.reconnectObservers.forEach(reconnectObserver => {
62
+ reconnectObserver.next?.();
66
63
  });
67
- };
68
- return ReconnectionMonitor;
69
- }());
70
- export { ReconnectionMonitor };
64
+ }
65
+ }
@@ -1,20 +1,15 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { __extends } from "tslib";
4
3
  import { AmplifyError, } from '@aws-amplify/core/internals/utils';
5
4
  /**
6
5
  * @internal
7
6
  */
8
- var APIError = /** @class */ (function (_super) {
9
- __extends(APIError, _super);
10
- function APIError(params) {
11
- var _this = _super.call(this, params) || this;
7
+ export class APIError extends AmplifyError {
8
+ constructor(params) {
9
+ super(params);
12
10
  // Hack for making the custom error class work when transpiled to es5
13
11
  // TODO: Delete the following 2 lines after we change the build target to >= es2015
14
- _this.constructor = APIError;
15
- Object.setPrototypeOf(_this, APIError.prototype);
16
- return _this;
12
+ this.constructor = APIError;
13
+ Object.setPrototypeOf(this, APIError.prototype);
17
14
  }
18
- return APIError;
19
- }(AmplifyError));
20
- export { APIError };
15
+ }
@@ -6,8 +6,8 @@ import { validationErrorMap } from './validation';
6
6
  * @internal
7
7
  */
8
8
  export function assertValidationError(assertion, name) {
9
- var _a = validationErrorMap[name], message = _a.message, recoverySuggestion = _a.recoverySuggestion;
9
+ const { message, recoverySuggestion } = validationErrorMap[name];
10
10
  if (!assertion) {
11
- throw new APIError({ name: name, message: message, recoverySuggestion: recoverySuggestion });
11
+ throw new APIError({ name, message, recoverySuggestion });
12
12
  }
13
13
  }
@@ -1,24 +1,23 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- var _a;
4
3
  export var APIValidationErrorCode;
5
4
  (function (APIValidationErrorCode) {
6
5
  APIValidationErrorCode["NoAuthSession"] = "NoAuthSession";
7
6
  APIValidationErrorCode["NoRegion"] = "NoRegion";
8
7
  APIValidationErrorCode["NoCustomEndpoint"] = "NoCustomEndpoint";
9
8
  })(APIValidationErrorCode || (APIValidationErrorCode = {}));
10
- export var validationErrorMap = (_a = {},
11
- _a[APIValidationErrorCode.NoAuthSession] = {
9
+ export const validationErrorMap = {
10
+ [APIValidationErrorCode.NoAuthSession]: {
12
11
  message: 'Auth session should not be empty.',
13
12
  },
14
13
  // TODO: re-enable when working in all test environments:
15
14
  // [APIValidationErrorCode.NoEndpoint]: {
16
15
  // message: 'Missing endpoint',
17
16
  // },
18
- _a[APIValidationErrorCode.NoRegion] = {
17
+ [APIValidationErrorCode.NoRegion]: {
19
18
  message: 'Missing region.',
20
19
  },
21
- _a[APIValidationErrorCode.NoCustomEndpoint] = {
20
+ [APIValidationErrorCode.NoCustomEndpoint]: {
22
21
  message: 'Custom endpoint region is present without custom endpoint.',
23
22
  },
24
- _a);
23
+ };
@@ -4,18 +4,17 @@ import { APIValidationErrorCode, assertValidationError } from './errors';
4
4
  /**
5
5
  * @internal
6
6
  */
7
- export var resolveConfig = function (amplify) {
8
- var _a, _b;
9
- var _c = (_b = (_a = amplify.getConfig().API) === null || _a === void 0 ? void 0 : _a.GraphQL) !== null && _b !== void 0 ? _b : {}, apiKey = _c.apiKey, customEndpoint = _c.customEndpoint, customEndpointRegion = _c.customEndpointRegion, defaultAuthMode = _c.defaultAuthMode, endpoint = _c.endpoint, region = _c.region;
7
+ export const resolveConfig = (amplify) => {
8
+ const { apiKey, customEndpoint, customEndpointRegion, defaultAuthMode, endpoint, region, } = amplify.getConfig().API?.GraphQL ?? {};
10
9
  // TODO: re-enable when working in all test environments:
11
10
  // assertValidationError(!!endpoint, APIValidationErrorCode.NoEndpoint);
12
11
  assertValidationError(!(!customEndpoint && customEndpointRegion), APIValidationErrorCode.NoCustomEndpoint);
13
12
  return {
14
- apiKey: apiKey,
15
- customEndpoint: customEndpoint,
16
- customEndpointRegion: customEndpointRegion,
17
- defaultAuthMode: defaultAuthMode,
18
- endpoint: endpoint,
19
- region: region,
13
+ apiKey,
14
+ customEndpoint,
15
+ customEndpointRegion,
16
+ defaultAuthMode,
17
+ endpoint,
18
+ region,
20
19
  };
21
20
  };
@@ -3,9 +3,8 @@
3
3
  /**
4
4
  * @internal
5
5
  */
6
- export var resolveLibraryOptions = function (amplify) {
7
- var _a, _b, _c, _d, _e, _f;
8
- var headers = (_c = (_b = (_a = amplify.libraryOptions) === null || _a === void 0 ? void 0 : _a.API) === null || _b === void 0 ? void 0 : _b.GraphQL) === null || _c === void 0 ? void 0 : _c.headers;
9
- var withCredentials = (_f = (_e = (_d = amplify.libraryOptions) === null || _d === void 0 ? void 0 : _d.API) === null || _e === void 0 ? void 0 : _e.GraphQL) === null || _f === void 0 ? void 0 : _f.withCredentials;
10
- return { headers: headers, withCredentials: withCredentials };
6
+ export const resolveLibraryOptions = (amplify) => {
7
+ const headers = amplify.libraryOptions?.API?.GraphQL?.headers;
8
+ const withCredentials = amplify.libraryOptions?.API?.GraphQL?.withCredentials;
9
+ return { headers, withCredentials };
11
10
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/api-graphql",
3
- "version": "4.0.1-console-preview.d745d43.0+d745d43",
3
+ "version": "4.0.1-ssr-api.df60e41.0+df60e41",
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,13 @@
47
47
  "internals"
48
48
  ],
49
49
  "dependencies": {
50
- "@aws-amplify/api-rest": "4.0.1-console-preview.d745d43.0+d745d43",
51
- "@aws-amplify/auth": "6.0.1-console-preview.d745d43.0+d745d43",
52
- "@aws-amplify/core": "6.0.1-console-preview.d745d43.0+d745d43",
50
+ "@aws-amplify/api-rest": "4.0.1-ssr-api.df60e41.0+df60e41",
51
+ "@aws-amplify/auth": "6.0.1-ssr-api.df60e41.0+df60e41",
52
+ "@aws-amplify/core": "6.0.1-ssr-api.df60e41.0+df60e41",
53
53
  "@aws-sdk/types": "3.387.0",
54
54
  "graphql": "15.8.0",
55
55
  "rxjs": "^7.8.1",
56
- "tslib": "^1.8.0"
56
+ "tslib": "^2.5.0"
57
57
  },
58
58
  "size-limit": [
59
59
  {
@@ -120,5 +120,5 @@
120
120
  "<rootDir>/__tests__"
121
121
  ]
122
122
  },
123
- "gitHead": "d745d43595a690928a86070deff39f3eed526437"
123
+ "gitHead": "df60e41fbc70bd6277162b7780b3fc030c93c6a0"
124
124
  }
package/src/GraphQLAPI.ts CHANGED
@@ -32,13 +32,14 @@ export class GraphQLAPIClass extends InternalGraphQLAPIClass {
32
32
  * @returns An Observable if the query is a subscription query, else a promise of the graphql result.
33
33
  */
34
34
  graphql<T = any>(
35
- amplify: AmplifyClassV6,
35
+ amplify: AmplifyClassV6 | (() => Promise<AmplifyClassV6>),
36
+ // amplify: AmplifyClassV6 | (() => Promise<AmplifyClassV6>),
36
37
  options: GraphQLOptions,
37
38
  additionalHeaders?: { [key: string]: string }
38
39
  ): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
39
40
  return super.graphql(amplify, options, additionalHeaders, {
40
41
  category: Category.API,
41
- action: ApiAction.GraphQl
42
+ action: ApiAction.GraphQl,
42
43
  });
43
44
  }
44
45
 
@@ -45,6 +45,10 @@ export const graphqlOperation = (
45
45
  authToken,
46
46
  });
47
47
 
48
+ const isAmplifyInstance = (amplify): amplify is AmplifyClassV6 => {
49
+ return typeof amplify !== 'function';
50
+ };
51
+
48
52
  /**
49
53
  * Export Cloud Logic APIs
50
54
  */
@@ -163,7 +167,9 @@ export class InternalGraphQLAPIClass {
163
167
  * @returns An Observable if the query is a subscription query, else a promise of the graphql result.
164
168
  */
165
169
  graphql<T = any>(
166
- amplify: AmplifyClassV6,
170
+ amplify:
171
+ | AmplifyClassV6
172
+ | ((fn: (amplify: any) => Promise<any>) => Promise<AmplifyClassV6>),
167
173
  { query: paramQuery, variables = {}, authMode, authToken }: GraphQLOptions,
168
174
  additionalHeaders?: { [key: string]: string },
169
175
  customUserAgentDetails?: CustomUserAgentDetails
@@ -190,13 +196,32 @@ export class InternalGraphQLAPIClass {
190
196
  case 'query':
191
197
  case 'mutation':
192
198
  const abortController = new AbortController();
193
- const responsePromise = this._graphql<T>(
194
- amplify,
195
- { query, variables, authMode },
196
- headers,
197
- abortController,
198
- customUserAgentDetails
199
- );
199
+
200
+ let responsePromise: Promise<GraphQLResult<T>>;
201
+
202
+ if (isAmplifyInstance(amplify)) {
203
+ responsePromise = this._graphql<T>(
204
+ amplify,
205
+ { query, variables, authMode },
206
+ headers,
207
+ abortController,
208
+ customUserAgentDetails
209
+ );
210
+ } else {
211
+ const wrapper = amplifyInstance =>
212
+ this._graphql<T>(
213
+ amplifyInstance,
214
+ { query, variables, authMode },
215
+ headers,
216
+ abortController,
217
+ customUserAgentDetails
218
+ );
219
+
220
+ responsePromise = amplify(wrapper) as unknown as Promise<
221
+ GraphQLResult<T>
222
+ >;
223
+ }
224
+
200
225
  this._api.updateRequestToBeCancellable(
201
226
  responsePromise,
202
227
  abortController
@@ -204,7 +229,7 @@ export class InternalGraphQLAPIClass {
204
229
  return responsePromise;
205
230
  case 'subscription':
206
231
  return this._graphqlSubscribe(
207
- amplify,
232
+ amplify as AmplifyClassV6,
208
233
  { query, variables, authMode },
209
234
  headers,
210
235
  customUserAgentDetails
@@ -375,7 +400,7 @@ export class InternalGraphQLAPIClass {
375
400
  variables,
376
401
  appSyncGraphqlEndpoint: config?.endpoint,
377
402
  region: config?.region,
378
- authenticationType: config?.defaultAuthMode,
403
+ authenticationType: authMode ?? config?.defaultAuthMode,
379
404
  apiKey: config?.apiKey,
380
405
  additionalHeaders,
381
406
  },
@@ -9,6 +9,7 @@ import {
9
9
  GraphQLAuthMode,
10
10
  DocumentType,
11
11
  } from '@aws-amplify/core/internals/utils';
12
+ import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
12
13
  export { CONTROL_MSG, ConnectionState } from './PubSub';
13
14
  /**
14
15
  * Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
@@ -353,16 +354,45 @@ type ExcludeNeverFields<O> = {
353
354
 
354
355
  export const __amplify = Symbol('amplify');
355
356
 
357
+ /**
358
+ * TODO: Refactor these 2. Pull out `graphQL` into its own type
359
+ */
356
360
  export type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
357
361
  [__amplify]: AmplifyClassV6;
358
- graphql: <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(
359
- options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
360
- additionalHeaders?:
361
- | {
362
- [key: string]: string;
363
- }
364
- | undefined
365
- ) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
362
+ graphql: GraphQLMethod;
366
363
  cancel: (promise: Promise<any>, message?: string) => boolean;
367
364
  isCancelError: (error: any) => boolean;
368
365
  }>;
366
+
367
+ export type V6ClientSSR<T extends Record<any, any> = never> =
368
+ ExcludeNeverFields<{
369
+ [__amplify]: AmplifyClassV6;
370
+ graphql: GraphQLMethodSSR;
371
+ cancel: (promise: Promise<any>, message?: string) => boolean;
372
+ isCancelError: (error: any) => boolean;
373
+ }>;
374
+
375
+ export type GraphQLMethod = <
376
+ FALLBACK_TYPES = unknown,
377
+ TYPED_GQL_STRING extends string = string
378
+ >(
379
+ options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
380
+ additionalHeaders?:
381
+ | {
382
+ [key: string]: string;
383
+ }
384
+ | undefined
385
+ ) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
386
+
387
+ export type GraphQLMethodSSR = <
388
+ FALLBACK_TYPES = unknown,
389
+ TYPED_GQL_STRING extends string = string
390
+ >(
391
+ contextSpec: AmplifyServer.ContextSpec,
392
+ options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
393
+ additionalHeaders?:
394
+ | {
395
+ [key: string]: string;
396
+ }
397
+ | undefined
398
+ ) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;