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