@aws-amplify/api-graphql 3.4.11 → 4.0.1-console-preview.047a1dd.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 (114) hide show
  1. package/README.md +3 -0
  2. package/lib/GraphQLAPI.js +0 -5
  3. package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +67 -0
  4. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +817 -0
  5. package/lib/Providers/constants.d.ts +95 -0
  6. package/lib/Providers/constants.js +103 -0
  7. package/lib/index.d.ts +0 -3
  8. package/lib/index.js +3 -9
  9. package/lib/internals/InternalGraphQLAPI.d.ts +4 -40
  10. package/lib/internals/InternalGraphQLAPI.js +89 -243
  11. package/lib/internals/index.d.ts +1 -0
  12. package/lib/internals/index.js +2 -1
  13. package/lib/internals/v6.d.ts +89 -0
  14. package/lib/internals/v6.js +99 -0
  15. package/lib/tsconfig.tsbuildinfo +5790 -0
  16. package/lib/types/PubSub.d.ts +27 -0
  17. package/lib/types/PubSub.js +46 -0
  18. package/lib/types/index.d.ts +184 -4
  19. package/lib/types/index.js +0 -3
  20. package/lib/utils/ConnectionStateMonitor.d.ts +41 -0
  21. package/lib/utils/ConnectionStateMonitor.js +143 -0
  22. package/lib/utils/ReachabilityMonitor/index.d.ts +3 -0
  23. package/lib/utils/ReachabilityMonitor/index.js +6 -0
  24. package/lib/utils/ReachabilityMonitor/index.native.d.ts +3 -0
  25. package/lib/utils/ReachabilityMonitor/index.native.js +10 -0
  26. package/lib/utils/ReconnectionMonitor.d.ts +29 -0
  27. package/lib/utils/ReconnectionMonitor.js +72 -0
  28. package/lib/utils/errors/APIError.d.ts +7 -0
  29. package/lib/utils/errors/APIError.js +22 -0
  30. package/lib/utils/errors/assertValidationError.d.ts +5 -0
  31. package/lib/utils/errors/assertValidationError.js +16 -0
  32. package/lib/utils/errors/index.d.ts +3 -0
  33. package/lib/utils/errors/index.js +11 -0
  34. package/lib/utils/errors/validation.d.ts +8 -0
  35. package/lib/utils/errors/validation.js +26 -0
  36. package/lib/utils/index.d.ts +2 -0
  37. package/lib/utils/index.js +8 -0
  38. package/lib/utils/resolveConfig.d.ts +8 -0
  39. package/lib/utils/resolveConfig.js +17 -0
  40. package/lib/utils/resolveCredentials.d.ts +12 -0
  41. package/lib/utils/resolveCredentials.js +22 -0
  42. package/lib-esm/GraphQLAPI.js +0 -5
  43. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +67 -0
  44. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +815 -0
  45. package/lib-esm/Providers/constants.d.ts +95 -0
  46. package/lib-esm/Providers/constants.js +100 -0
  47. package/lib-esm/index.d.ts +0 -3
  48. package/lib-esm/index.js +0 -4
  49. package/lib-esm/internals/InternalGraphQLAPI.d.ts +4 -40
  50. package/lib-esm/internals/InternalGraphQLAPI.js +90 -244
  51. package/lib-esm/internals/index.d.ts +1 -0
  52. package/lib-esm/internals/index.js +1 -1
  53. package/lib-esm/internals/v6.d.ts +89 -0
  54. package/lib-esm/internals/v6.js +96 -0
  55. package/lib-esm/tsconfig.tsbuildinfo +5790 -0
  56. package/lib-esm/types/PubSub.d.ts +27 -0
  57. package/lib-esm/types/PubSub.js +44 -0
  58. package/lib-esm/types/index.d.ts +184 -4
  59. package/lib-esm/types/index.js +0 -3
  60. package/lib-esm/utils/ConnectionStateMonitor.d.ts +41 -0
  61. package/lib-esm/utils/ConnectionStateMonitor.js +141 -0
  62. package/lib-esm/utils/ReachabilityMonitor/index.d.ts +3 -0
  63. package/lib-esm/utils/ReachabilityMonitor/index.js +4 -0
  64. package/lib-esm/utils/ReachabilityMonitor/index.native.d.ts +3 -0
  65. package/lib-esm/utils/ReachabilityMonitor/index.native.js +7 -0
  66. package/lib-esm/utils/ReconnectionMonitor.d.ts +29 -0
  67. package/lib-esm/utils/ReconnectionMonitor.js +70 -0
  68. package/lib-esm/utils/errors/APIError.d.ts +7 -0
  69. package/lib-esm/utils/errors/APIError.js +20 -0
  70. package/lib-esm/utils/errors/assertValidationError.d.ts +5 -0
  71. package/lib-esm/utils/errors/assertValidationError.js +13 -0
  72. package/lib-esm/utils/errors/index.d.ts +3 -0
  73. package/lib-esm/utils/errors/index.js +5 -0
  74. package/lib-esm/utils/errors/validation.d.ts +8 -0
  75. package/lib-esm/utils/errors/validation.js +24 -0
  76. package/lib-esm/utils/index.d.ts +2 -0
  77. package/lib-esm/utils/index.js +4 -0
  78. package/lib-esm/utils/resolveConfig.d.ts +8 -0
  79. package/lib-esm/utils/resolveConfig.js +15 -0
  80. package/lib-esm/utils/resolveCredentials.d.ts +12 -0
  81. package/lib-esm/utils/resolveCredentials.js +20 -0
  82. package/package.json +114 -114
  83. package/src/GraphQLAPI.ts +0 -2
  84. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +1003 -0
  85. package/src/Providers/constants.ts +110 -0
  86. package/src/index.ts +0 -3
  87. package/src/internals/InternalGraphQLAPI.ts +62 -221
  88. package/src/internals/index.ts +2 -0
  89. package/src/internals/v6.ts +106 -0
  90. package/src/types/PubSub.ts +58 -0
  91. package/src/types/index.ts +234 -7
  92. package/src/utils/ConnectionStateMonitor.ts +195 -0
  93. package/src/utils/ReachabilityMonitor/index.native.ts +7 -0
  94. package/src/utils/ReachabilityMonitor/index.ts +4 -0
  95. package/src/utils/ReconnectionMonitor.ts +76 -0
  96. package/src/utils/errors/APIError.ts +18 -0
  97. package/src/utils/errors/assertValidationError.ts +19 -0
  98. package/src/utils/errors/index.ts +6 -0
  99. package/src/utils/errors/validation.ts +26 -0
  100. package/src/utils/index.ts +5 -0
  101. package/src/utils/resolveConfig.ts +20 -0
  102. package/src/utils/resolveCredentials.ts +14 -0
  103. package/lib/.tsbuildinfo +0 -3
  104. package/lib/GraphQLAPI.js.map +0 -1
  105. package/lib/index.js.map +0 -1
  106. package/lib/internals/InternalGraphQLAPI.js.map +0 -1
  107. package/lib/internals/index.js.map +0 -1
  108. package/lib/types/index.js.map +0 -1
  109. package/lib-esm/.tsbuildinfo +0 -3
  110. package/lib-esm/GraphQLAPI.js.map +0 -1
  111. package/lib-esm/index.js.map +0 -1
  112. package/lib-esm/internals/InternalGraphQLAPI.js.map +0 -1
  113. package/lib-esm/internals/index.js.map +0 -1
  114. package/lib-esm/types/index.js.map +0 -1
@@ -5,13 +5,12 @@ var tslib_1 = require("tslib");
5
5
  // SPDX-License-Identifier: Apache-2.0
6
6
  var graphql_1 = require("graphql");
7
7
  var core_1 = require("@aws-amplify/core");
8
- var internals_1 = require("@aws-amplify/pubsub/internals");
9
- var auth_1 = require("@aws-amplify/auth");
10
- var cache_1 = require("@aws-amplify/cache");
8
+ var utils_1 = require("@aws-amplify/core/internals/utils");
11
9
  var types_1 = require("../types");
12
10
  var api_rest_1 = require("@aws-amplify/api-rest");
11
+ var AWSAppSyncRealTimeProvider_1 = require("../Providers/AWSAppSyncRealTimeProvider");
13
12
  var USER_AGENT_HEADER = 'x-amz-user-agent';
14
- var logger = new core_1.ConsoleLogger('GraphQLAPI');
13
+ var logger = new utils_1.ConsoleLogger('GraphQLAPI');
15
14
  exports.graphqlOperation = function (query, variables, authToken) {
16
15
  if (variables === void 0) { variables = {}; }
17
16
  return ({
@@ -29,146 +28,78 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
29
28
  * @param {Object} options - Configuration object for API
30
29
  */
31
30
  function InternalGraphQLAPIClass(options) {
32
- this._api = null;
33
- this.Auth = auth_1.Auth;
34
- this.Cache = cache_1.Cache;
35
- this.Credentials = core_1.Credentials;
31
+ this.Cache = core_1.Cache;
36
32
  this._options = options;
37
33
  logger.debug('API Options', this._options);
38
34
  }
39
35
  InternalGraphQLAPIClass.prototype.getModuleName = function () {
40
36
  return 'InternalGraphQLAPI';
41
37
  };
42
- /**
43
- * Configure API
44
- * @param {Object} config - Configuration of the API
45
- * @return {Object} - The current configuration
46
- */
47
- InternalGraphQLAPIClass.prototype.configure = function (options) {
48
- var _a = options || {}, _b = _a.API, API = _b === void 0 ? {} : _b, otherOptions = tslib_1.__rest(_a, ["API"]);
49
- var opt = tslib_1.__assign(tslib_1.__assign({}, otherOptions), API);
50
- logger.debug('configure GraphQL API', { opt: opt });
51
- if (opt['aws_project_region']) {
52
- opt = Object.assign({}, opt, {
53
- region: opt['aws_project_region'],
54
- header: {},
55
- });
56
- }
57
- if (typeof opt.graphql_headers !== 'undefined' &&
58
- typeof opt.graphql_headers !== 'function') {
59
- logger.warn('graphql_headers should be a function');
60
- opt.graphql_headers = undefined;
61
- }
62
- this._options = Object.assign({}, this._options, opt);
63
- this.createInstance();
64
- return this._options;
65
- };
66
- /**
67
- * Create an instance of API for the library
68
- * @return - A promise of true if Success
69
- */
70
- InternalGraphQLAPIClass.prototype.createInstance = function () {
71
- logger.debug('create Rest instance');
72
- if (this._options) {
73
- this._api = new api_rest_1.RestClient(this._options);
74
- // Share instance Credentials with client for SSR
75
- this._api.Credentials = this.Credentials;
76
- return true;
77
- }
78
- else {
79
- return Promise.reject('API not configured');
80
- }
81
- };
82
- InternalGraphQLAPIClass.prototype._headerBasedAuth = function (defaultAuthenticationType, additionalHeaders) {
38
+ InternalGraphQLAPIClass.prototype._headerBasedAuth = function (defaultAuthenticationType, additionalHeaders, customUserAgentDetails) {
83
39
  if (additionalHeaders === void 0) { additionalHeaders = {}; }
40
+ var _a;
84
41
  return tslib_1.__awaiter(this, void 0, void 0, function () {
85
- var _a, aws_appsync_authenticationType, apiKey, authenticationType, headers, _b, credentialsOK, token, federatedInfo, currentUser, e_1, session, e_2;
86
- return tslib_1.__generator(this, function (_c) {
87
- switch (_c.label) {
42
+ var config, _b, region, appSyncGraphqlEndpoint, authenticationType, headers, _c, session, token, e_1;
43
+ return tslib_1.__generator(this, function (_d) {
44
+ switch (_d.label) {
88
45
  case 0:
89
- _a = this._options, aws_appsync_authenticationType = _a.aws_appsync_authenticationType, apiKey = _a.aws_appsync_apiKey;
90
- authenticationType = defaultAuthenticationType || aws_appsync_authenticationType || 'AWS_IAM';
46
+ config = core_1.Amplify.getConfig();
47
+ _b = config.API.AppSync, region = _b.region, appSyncGraphqlEndpoint = _b.endpoint, authenticationType = _b.defaultAuthMode;
91
48
  headers = {};
92
- _b = authenticationType;
93
- switch (_b) {
94
- case 'API_KEY': return [3 /*break*/, 1];
95
- case 'AWS_IAM': return [3 /*break*/, 2];
96
- case 'OPENID_CONNECT': return [3 /*break*/, 4];
97
- case 'AMAZON_COGNITO_USER_POOLS': return [3 /*break*/, 11];
98
- case 'AWS_LAMBDA': return [3 /*break*/, 15];
49
+ _c = authenticationType.type;
50
+ switch (_c) {
51
+ case 'apiKey': return [3 /*break*/, 1];
52
+ case 'iam': return [3 /*break*/, 2];
53
+ case 'jwt': return [3 /*break*/, 4];
54
+ case 'custom': return [3 /*break*/, 8];
99
55
  }
100
- return [3 /*break*/, 16];
56
+ return [3 /*break*/, 9];
101
57
  case 1:
102
- if (!apiKey) {
58
+ if (!authenticationType.apiKey) {
103
59
  throw new Error(types_1.GraphQLAuthError.NO_API_KEY);
104
60
  }
105
61
  headers = {
106
- Authorization: null,
107
- 'X-Api-Key': apiKey,
62
+ 'X-Api-Key': authenticationType.apiKey,
108
63
  };
109
- return [3 /*break*/, 17];
110
- case 2: return [4 /*yield*/, this._ensureCredentials()];
64
+ return [3 /*break*/, 10];
65
+ case 2: return [4 /*yield*/, core_1.fetchAuthSession()];
111
66
  case 3:
112
- credentialsOK = _c.sent();
113
- if (!credentialsOK) {
67
+ session = _d.sent();
68
+ if (session.credentials === undefined) {
114
69
  throw new Error(types_1.GraphQLAuthError.NO_CREDENTIALS);
115
70
  }
116
- return [3 /*break*/, 17];
71
+ return [3 /*break*/, 10];
117
72
  case 4:
118
- _c.trys.push([4, 9, , 10]);
73
+ _d.trys.push([4, 6, , 7]);
119
74
  token = void 0;
120
- return [4 /*yield*/, cache_1.Cache.getItem('federatedInfo')];
75
+ return [4 /*yield*/, core_1.fetchAuthSession()];
121
76
  case 5:
122
- federatedInfo = _c.sent();
123
- if (!federatedInfo) return [3 /*break*/, 6];
124
- token = federatedInfo.token;
125
- return [3 /*break*/, 8];
126
- case 6: return [4 /*yield*/, auth_1.Auth.currentAuthenticatedUser()];
127
- case 7:
128
- currentUser = _c.sent();
129
- if (currentUser) {
130
- token = currentUser.token;
131
- }
132
- _c.label = 8;
133
- case 8:
77
+ token = (_a = (_d.sent()).tokens) === null || _a === void 0 ? void 0 : _a.accessToken.toString();
134
78
  if (!token) {
135
79
  throw new Error(types_1.GraphQLAuthError.NO_FEDERATED_JWT);
136
80
  }
137
81
  headers = {
138
82
  Authorization: token,
139
83
  };
140
- return [3 /*break*/, 10];
141
- case 9:
142
- e_1 = _c.sent();
143
- throw new Error(types_1.GraphQLAuthError.NO_CURRENT_USER);
144
- case 10: return [3 /*break*/, 17];
145
- case 11:
146
- _c.trys.push([11, 13, , 14]);
147
- return [4 /*yield*/, this.Auth.currentSession()];
148
- case 12:
149
- session = _c.sent();
150
- headers = {
151
- Authorization: session.getAccessToken().getJwtToken(),
152
- };
153
- return [3 /*break*/, 14];
154
- case 13:
155
- e_2 = _c.sent();
84
+ return [3 /*break*/, 7];
85
+ case 6:
86
+ e_1 = _d.sent();
156
87
  throw new Error(types_1.GraphQLAuthError.NO_CURRENT_USER);
157
- case 14: return [3 /*break*/, 17];
158
- case 15:
88
+ case 7: return [3 /*break*/, 10];
89
+ case 8:
159
90
  if (!additionalHeaders.Authorization) {
160
91
  throw new Error(types_1.GraphQLAuthError.NO_AUTH_TOKEN);
161
92
  }
162
93
  headers = {
163
94
  Authorization: additionalHeaders.Authorization,
164
95
  };
165
- return [3 /*break*/, 17];
166
- case 16:
96
+ return [3 /*break*/, 10];
97
+ case 9:
167
98
  headers = {
168
99
  Authorization: null,
169
100
  };
170
- return [3 /*break*/, 17];
171
- case 17: return [2 /*return*/, headers];
101
+ return [3 /*break*/, 10];
102
+ case 10: return [2 /*return*/, headers];
172
103
  }
173
104
  });
174
105
  });
@@ -205,14 +136,7 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
205
136
  switch (operationType) {
206
137
  case 'query':
207
138
  case 'mutation':
208
- this.createInstanceIfNotCreated();
209
- var cancellableToken = this._api.getCancellableToken();
210
- var initParams = {
211
- cancellableToken: cancellableToken,
212
- withCredentials: this._options.withCredentials,
213
- };
214
- var responsePromise = this._graphql({ query: query, variables: variables, authMode: authMode }, headers, initParams, customUserAgentDetails);
215
- this._api.updateRequestToBeCancellable(responsePromise, cancellableToken);
139
+ var responsePromise = this._graphql({ query: query, variables: variables, authMode: authMode }, headers, customUserAgentDetails);
216
140
  return responsePromise;
217
141
  case 'subscription':
218
142
  return this._graphqlSubscribe({ query: query, variables: variables, authMode: authMode }, headers, customUserAgentDetails);
@@ -220,59 +144,49 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
220
144
  throw new Error("invalid operation type: " + operationType);
221
145
  }
222
146
  };
223
- InternalGraphQLAPIClass.prototype._graphql = function (_a, additionalHeaders, initParams, customUserAgentDetails) {
147
+ InternalGraphQLAPIClass.prototype._graphql = function (_a, additionalHeaders, customUserAgentDetails) {
224
148
  var query = _a.query, variables = _a.variables, authMode = _a.authMode;
225
149
  if (additionalHeaders === void 0) { additionalHeaders = {}; }
226
- if (initParams === void 0) { initParams = {}; }
227
150
  return tslib_1.__awaiter(this, void 0, void 0, function () {
228
- var _b, region, appSyncGraphqlEndpoint, _c, graphql_headers, customGraphqlEndpoint, customEndpointRegion, headers, _d, _e, _f, _g, _h, _j, body, init, endpoint, error, response, err_1, errors;
229
- var _k;
230
- return tslib_1.__generator(this, function (_l) {
231
- switch (_l.label) {
151
+ var config, _b, region, appSyncGraphqlEndpoint, customGraphqlEndpoint, customEndpointRegion, headers, _c, _d, _e, _f, _g, body, endpoint, error, response, err_1, errors;
152
+ var _h;
153
+ return tslib_1.__generator(this, function (_j) {
154
+ switch (_j.label) {
232
155
  case 0:
233
- this.createInstanceIfNotCreated();
234
- _b = this._options, region = _b.aws_appsync_region, appSyncGraphqlEndpoint = _b.aws_appsync_graphqlEndpoint, _c = _b.graphql_headers, graphql_headers = _c === void 0 ? function () { return ({}); } : _c, customGraphqlEndpoint = _b.graphql_endpoint, customEndpointRegion = _b.graphql_endpoint_iam_region;
235
- _d = [{}];
236
- _e = !customGraphqlEndpoint;
237
- if (!_e) return [3 /*break*/, 2];
238
- return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders)];
156
+ config = core_1.Amplify.getConfig();
157
+ _b = config.API.AppSync, region = _b.region, appSyncGraphqlEndpoint = _b.endpoint;
158
+ customGraphqlEndpoint = null;
159
+ customEndpointRegion = null;
160
+ _c = [{}];
161
+ _d = !customGraphqlEndpoint;
162
+ if (!_d) return [3 /*break*/, 2];
163
+ return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders, customUserAgentDetails)];
239
164
  case 1:
240
- _e = (_l.sent());
241
- _l.label = 2;
165
+ _d = (_j.sent());
166
+ _j.label = 2;
242
167
  case 2:
243
- _f = [tslib_1.__assign.apply(void 0, _d.concat([(_e)]))];
244
- _g = customGraphqlEndpoint;
245
- if (!_g) return [3 /*break*/, 6];
168
+ _e = [tslib_1.__assign.apply(void 0, _c.concat([(_d)]))];
169
+ _f = customGraphqlEndpoint;
170
+ if (!_f) return [3 /*break*/, 6];
246
171
  if (!customEndpointRegion) return [3 /*break*/, 4];
247
- return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders)];
172
+ return [4 /*yield*/, this._headerBasedAuth(authMode, additionalHeaders, customUserAgentDetails)];
248
173
  case 3:
249
- _h = _l.sent();
174
+ _g = _j.sent();
250
175
  return [3 /*break*/, 5];
251
176
  case 4:
252
- _h = { Authorization: null };
253
- _l.label = 5;
177
+ _g = { Authorization: null };
178
+ _j.label = 5;
254
179
  case 5:
255
- _g = (_h);
256
- _l.label = 6;
180
+ _f = (_g);
181
+ _j.label = 6;
257
182
  case 6:
258
- _j = [tslib_1.__assign.apply(void 0, _f.concat([(_g)]))];
259
- return [4 /*yield*/, graphql_headers({ query: query, variables: variables })];
260
- case 7:
261
- headers = tslib_1.__assign.apply(void 0, [tslib_1.__assign.apply(void 0, [tslib_1.__assign.apply(void 0, _j.concat([(_l.sent())])), additionalHeaders]), (!customGraphqlEndpoint && (_k = {},
262
- _k[USER_AGENT_HEADER] = core_1.getAmplifyUserAgent(customUserAgentDetails),
263
- _k))]);
183
+ 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 = {},
184
+ _h[USER_AGENT_HEADER] = utils_1.getAmplifyUserAgent(customUserAgentDetails),
185
+ _h))]);
264
186
  body = {
265
187
  query: graphql_1.print(query),
266
188
  variables: variables,
267
189
  };
268
- init = Object.assign({
269
- headers: headers,
270
- body: body,
271
- signerServiceInfo: {
272
- service: !customGraphqlEndpoint ? 'appsync' : 'execute-api',
273
- region: !customGraphqlEndpoint ? region : customEndpointRegion,
274
- },
275
- }, initParams);
276
190
  endpoint = customGraphqlEndpoint || appSyncGraphqlEndpoint;
277
191
  if (!endpoint) {
278
192
  error = new graphql_1.GraphQLError('No graphql endpoint provided.');
@@ -281,27 +195,26 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
281
195
  errors: [error],
282
196
  };
283
197
  }
284
- _l.label = 8;
198
+ _j.label = 7;
199
+ case 7:
200
+ _j.trys.push([7, 9, , 10]);
201
+ return [4 /*yield*/, api_rest_1.post(endpoint, {
202
+ headers: headers,
203
+ body: body,
204
+ region: region,
205
+ serviceName: 'appsync',
206
+ })];
285
207
  case 8:
286
- _l.trys.push([8, 10, , 11]);
287
- return [4 /*yield*/, this._api.post(endpoint, init)];
208
+ response = _j.sent();
209
+ return [3 /*break*/, 10];
288
210
  case 9:
289
- response = _l.sent();
290
- return [3 /*break*/, 11];
291
- case 10:
292
- err_1 = _l.sent();
293
- // If the exception is because user intentionally
294
- // cancelled the request, do not modify the exception
295
- // so that clients can identify the exception correctly.
296
- if (this._api.isCancel(err_1)) {
297
- throw err_1;
298
- }
211
+ err_1 = _j.sent();
299
212
  response = {
300
213
  data: {},
301
214
  errors: [new graphql_1.GraphQLError(err_1.message, null, null, null, null, err_1)],
302
215
  };
303
- return [3 /*break*/, 11];
304
- case 11:
216
+ return [3 /*break*/, 10];
217
+ case 10:
305
218
  errors = response.errors;
306
219
  if (errors && errors.length) {
307
220
  throw response;
@@ -311,90 +224,23 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
311
224
  });
312
225
  });
313
226
  };
314
- InternalGraphQLAPIClass.prototype.createInstanceIfNotCreated = function () {
315
- return tslib_1.__awaiter(this, void 0, void 0, function () {
316
- return tslib_1.__generator(this, function (_a) {
317
- switch (_a.label) {
318
- case 0:
319
- if (!!this._api) return [3 /*break*/, 2];
320
- return [4 /*yield*/, this.createInstance()];
321
- case 1:
322
- _a.sent();
323
- _a.label = 2;
324
- case 2: return [2 /*return*/];
325
- }
326
- });
327
- });
328
- };
329
- /**
330
- * Checks to see if an error thrown is from an api request cancellation
331
- * @param {any} error - Any error
332
- * @return {boolean} - A boolean indicating if the error was from an api request cancellation
333
- */
334
- InternalGraphQLAPIClass.prototype.isCancel = function (error) {
335
- return this._api.isCancel(error);
336
- };
337
- /**
338
- * Cancels an inflight request. Only applicable for graphql queries and mutations
339
- * @param {any} request - request to cancel
340
- * @return {boolean} - A boolean indicating if the request was cancelled
341
- */
342
- InternalGraphQLAPIClass.prototype.cancel = function (request, message) {
343
- return this._api.cancel(request, message);
344
- };
345
- /**
346
- * Check if the request has a corresponding cancel token in the WeakMap.
347
- * @params request - The request promise
348
- * @return if the request has a corresponding cancel token.
349
- */
350
- InternalGraphQLAPIClass.prototype.hasCancelToken = function (request) {
351
- return this._api.hasCancelToken(request);
352
- };
353
227
  InternalGraphQLAPIClass.prototype._graphqlSubscribe = function (_a, additionalHeaders, customUserAgentDetails) {
354
228
  var query = _a.query, variables = _a.variables, defaultAuthenticationType = _a.authMode, authToken = _a.authToken;
355
229
  if (additionalHeaders === void 0) { additionalHeaders = {}; }
356
- var _b = this._options, region = _b.aws_appsync_region, appSyncGraphqlEndpoint = _b.aws_appsync_graphqlEndpoint, aws_appsync_authenticationType = _b.aws_appsync_authenticationType, apiKey = _b.aws_appsync_apiKey, _c = _b.graphql_headers, graphql_headers = _c === void 0 ? function () { return ({}); } : _c;
357
- var authenticationType = defaultAuthenticationType || aws_appsync_authenticationType || 'AWS_IAM';
358
- if (internals_1.InternalPubSub && typeof internals_1.InternalPubSub.subscribe === 'function') {
359
- return internals_1.InternalPubSub.subscribe('', {
360
- provider: core_1.INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER,
361
- appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
362
- authenticationType: authenticationType,
363
- apiKey: apiKey,
364
- query: graphql_1.print(query),
365
- region: region,
366
- variables: variables,
367
- graphql_headers: graphql_headers,
368
- additionalHeaders: additionalHeaders,
369
- authToken: authToken,
370
- }, customUserAgentDetails);
371
- }
372
- else {
373
- logger.debug('No pubsub module applied for subscription');
374
- throw new Error('No pubsub module applied for subscription');
230
+ var _b;
231
+ var AppSync = ((_b = core_1.Amplify.getConfig().API) !== null && _b !== void 0 ? _b : {}).AppSync;
232
+ if (!this.appSyncRealTime) {
233
+ this.appSyncRealTime = new AWSAppSyncRealTimeProvider_1.AWSAppSyncRealTimeProvider();
375
234
  }
376
- };
377
- /**
378
- * @private
379
- */
380
- InternalGraphQLAPIClass.prototype._ensureCredentials = function () {
381
- var _this = this;
382
- return this.Credentials.get()
383
- .then(function (credentials) {
384
- if (!credentials)
385
- return false;
386
- var cred = _this.Credentials.shear(credentials);
387
- logger.debug('set credentials for api', cred);
388
- return true;
389
- })
390
- .catch(function (err) {
391
- logger.warn('ensure credentials error', err);
392
- return false;
235
+ return this.appSyncRealTime.subscribe({
236
+ query: graphql_1.print(query),
237
+ variables: variables,
238
+ appSyncGraphqlEndpoint: AppSync.endpoint,
239
+ region: AppSync.region,
240
+ authenticationType: AppSync.defaultAuthMode,
393
241
  });
394
242
  };
395
243
  return InternalGraphQLAPIClass;
396
244
  }());
397
245
  exports.InternalGraphQLAPIClass = InternalGraphQLAPIClass;
398
246
  exports.InternalGraphQLAPI = new InternalGraphQLAPIClass(null);
399
- core_1.Amplify.register(exports.InternalGraphQLAPI);
400
- //# sourceMappingURL=InternalGraphQLAPI.js.map
@@ -1 +1,2 @@
1
1
  export { InternalGraphQLAPI, InternalGraphQLAPIClass, } from './InternalGraphQLAPI';
2
+ export { graphql } from './v6';
@@ -5,4 +5,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
5
5
  var InternalGraphQLAPI_1 = require("./InternalGraphQLAPI");
6
6
  exports.InternalGraphQLAPI = InternalGraphQLAPI_1.InternalGraphQLAPI;
7
7
  exports.InternalGraphQLAPIClass = InternalGraphQLAPI_1.InternalGraphQLAPIClass;
8
- //# sourceMappingURL=index.js.map
8
+ var v6_1 = require("./v6");
9
+ exports.graphql = v6_1.graphql;
@@ -0,0 +1,89 @@
1
+ import { GraphQLOptionsV6, GraphQLResponseV6 } from '../types';
2
+ /**
3
+ * Invokes graphql operations against a graphql service, providing correct input and
4
+ * output types if Amplify-generated graphql from a recent version of the CLI/codegen
5
+ * are used *or* correct typing is provided via the type argument.
6
+ *
7
+ * Amplify-generated "branded" graphql queries will look similar to this:
8
+ *
9
+ * ```ts
10
+ * //
11
+ * // |-- branding
12
+ * // v
13
+ * export const getModel = `...` as GeneratedQuery<
14
+ * GetModelQueryVariables,
15
+ * GetModelQuery
16
+ * >;
17
+ * ```
18
+ *
19
+ * If this branding is not in your generated graphql, update to a newer version of
20
+ * CLI/codegen and regenerate your graphql using `amplify codegen`.
21
+ *
22
+ * ## Using Amplify-generated graphql
23
+ *
24
+ * ```ts
25
+ * import * as queries from './graphql/queries';
26
+ *
27
+ * //
28
+ * // |-- correctly typed graphql response containing a Widget
29
+ * // v
30
+ * const queryResult = await graphql({
31
+ * query: queries.getWidget,
32
+ * variables: {
33
+ * id: "abc", // <-- type hinted/enforced
34
+ * },
35
+ * });
36
+ *
37
+ * //
38
+ * // |-- a correctly typed Widget
39
+ * // v
40
+ * const fetchedWidget = queryResult.data?.getWidget!;
41
+ * ```
42
+ *
43
+ * ## Custom input + result types
44
+ *
45
+ * To provide input types (`variables`) and result types:
46
+ *
47
+ * ```ts
48
+ * type GetById_NameOnly = {
49
+ * variables: {
50
+ * id: string
51
+ * },
52
+ * result: Promise<{
53
+ * data: { getWidget: { name: string } }
54
+ * }>
55
+ * }
56
+ *
57
+ * //
58
+ * // |-- type is GetById_NameOnly["result"]
59
+ * // v
60
+ * const result = graphql<GetById_NameOnly>({
61
+ * query: "...",
62
+ * variables: { id: "abc" } // <-- type of GetById_NameOnly["variables"]
63
+ * });
64
+ * ```
65
+ *
66
+ * ## Custom result type only
67
+ *
68
+ * To specify result types only, use a type that is *not* in the `{variables, result}` shape:
69
+ *
70
+ * ```ts
71
+ * type MyResultType = Promise<{
72
+ * data: {
73
+ * getWidget: { name: string }
74
+ * }
75
+ * }>
76
+ *
77
+ * //
78
+ * // |-- type is MyResultType
79
+ * // v
80
+ * const result = graphql<MyResultType>({query: "..."});
81
+ * ```
82
+ *
83
+ * @param options
84
+ * @param additionalHeaders
85
+ */
86
+ export declare function graphql<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
87
+ [key: string]: string;
88
+ }): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
89
+ export { GraphQLOptionsV6, GraphQLResponseV6 };
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4
+ // SPDX-License-Identifier: Apache-2.0
5
+ var GraphQLAPI_1 = require("../GraphQLAPI");
6
+ /**
7
+ * Invokes graphql operations against a graphql service, providing correct input and
8
+ * output types if Amplify-generated graphql from a recent version of the CLI/codegen
9
+ * are used *or* correct typing is provided via the type argument.
10
+ *
11
+ * Amplify-generated "branded" graphql queries will look similar to this:
12
+ *
13
+ * ```ts
14
+ * //
15
+ * // |-- branding
16
+ * // v
17
+ * export const getModel = `...` as GeneratedQuery<
18
+ * GetModelQueryVariables,
19
+ * GetModelQuery
20
+ * >;
21
+ * ```
22
+ *
23
+ * If this branding is not in your generated graphql, update to a newer version of
24
+ * CLI/codegen and regenerate your graphql using `amplify codegen`.
25
+ *
26
+ * ## Using Amplify-generated graphql
27
+ *
28
+ * ```ts
29
+ * import * as queries from './graphql/queries';
30
+ *
31
+ * //
32
+ * // |-- correctly typed graphql response containing a Widget
33
+ * // v
34
+ * const queryResult = await graphql({
35
+ * query: queries.getWidget,
36
+ * variables: {
37
+ * id: "abc", // <-- type hinted/enforced
38
+ * },
39
+ * });
40
+ *
41
+ * //
42
+ * // |-- a correctly typed Widget
43
+ * // v
44
+ * const fetchedWidget = queryResult.data?.getWidget!;
45
+ * ```
46
+ *
47
+ * ## Custom input + result types
48
+ *
49
+ * To provide input types (`variables`) and result types:
50
+ *
51
+ * ```ts
52
+ * type GetById_NameOnly = {
53
+ * variables: {
54
+ * id: string
55
+ * },
56
+ * result: Promise<{
57
+ * data: { getWidget: { name: string } }
58
+ * }>
59
+ * }
60
+ *
61
+ * //
62
+ * // |-- type is GetById_NameOnly["result"]
63
+ * // v
64
+ * const result = graphql<GetById_NameOnly>({
65
+ * query: "...",
66
+ * variables: { id: "abc" } // <-- type of GetById_NameOnly["variables"]
67
+ * });
68
+ * ```
69
+ *
70
+ * ## Custom result type only
71
+ *
72
+ * To specify result types only, use a type that is *not* in the `{variables, result}` shape:
73
+ *
74
+ * ```ts
75
+ * type MyResultType = Promise<{
76
+ * data: {
77
+ * getWidget: { name: string }
78
+ * }
79
+ * }>
80
+ *
81
+ * //
82
+ * // |-- type is MyResultType
83
+ * // v
84
+ * const result = graphql<MyResultType>({query: "..."});
85
+ * ```
86
+ *
87
+ * @param options
88
+ * @param additionalHeaders
89
+ */
90
+ function graphql(options, additionalHeaders) {
91
+ /**
92
+ * The correctness of these typings depends on correct string branding or overrides.
93
+ * Neither of these can actually be validated at runtime. Hence, we don't perform
94
+ * any validation or type-guarding here.
95
+ */
96
+ var result = GraphQLAPI_1.GraphQLAPI.graphql(options, additionalHeaders);
97
+ return result;
98
+ }
99
+ exports.graphql = graphql;