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