@aws-amplify/api-graphql 4.0.1-api-v6-models.9351bcf.0 → 4.0.1-api-v6-models.58f2536.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/internals/server/package.json +8 -0
- package/lib/GraphQLAPI.d.ts +1 -1
- package/lib/GraphQLAPI.js +23 -28
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +467 -582
- package/lib/index.js +1 -1
- package/lib/internals/APIClient.d.ts +16 -1
- package/lib/internals/APIClient.js +232 -206
- package/lib/internals/InternalGraphQLAPI.d.ts +1 -1
- package/lib/internals/InternalGraphQLAPI.js +205 -247
- package/lib/internals/generateClient.js +14 -13
- package/lib/internals/generateModelsProperty.js +19 -312
- package/lib/internals/operations/get.d.ts +2 -0
- package/lib/internals/operations/get.js +59 -0
- package/lib/internals/operations/list.d.ts +2 -0
- package/lib/internals/operations/list.js +71 -0
- package/lib/internals/operations/observeQuery.d.ts +2 -0
- package/lib/internals/operations/observeQuery.js +119 -0
- package/lib/internals/operations/subscription.d.ts +1 -0
- package/lib/internals/operations/subscription.js +27 -0
- package/lib/internals/server/generateClient.d.ts +11 -0
- package/lib/internals/server/generateClient.js +30 -0
- package/lib/internals/server/generateModelsProperty.d.ts +3 -0
- package/lib/internals/server/generateModelsProperty.js +45 -0
- package/lib/internals/server/index.d.ts +1 -0
- package/lib/internals/server/index.js +7 -0
- package/lib/internals/v6.js +3 -3
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/index.d.ts +36 -4
- package/lib/utils/ConnectionStateMonitor.js +51 -64
- package/lib/utils/ReachabilityMonitor/index.js +2 -2
- package/lib/utils/ReachabilityMonitor/index.native.js +4 -6
- package/lib/utils/ReconnectionMonitor.js +20 -24
- package/lib/utils/errors/APIError.js +7 -11
- package/lib/utils/errors/assertValidationError.js +4 -4
- package/lib/utils/errors/validation.js +5 -6
- package/lib/utils/findIndexByFields.js +4 -9
- package/lib/utils/resolveConfig.js +9 -10
- package/lib/utils/resolveLibraryOptions.js +4 -5
- package/lib/utils/resolveOwnerFields.js +10 -33
- package/lib/utils/resolvePKFields.js +2 -3
- package/lib-esm/GraphQLAPI.d.ts +1 -1
- package/lib-esm/GraphQLAPI.js +23 -29
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +459 -575
- package/lib-esm/Providers/constants.js +10 -10
- package/lib-esm/internals/APIClient.d.ts +16 -1
- package/lib-esm/internals/APIClient.js +230 -205
- package/lib-esm/internals/InternalGraphQLAPI.d.ts +1 -1
- package/lib-esm/internals/InternalGraphQLAPI.js +201 -244
- package/lib-esm/internals/generateClient.js +11 -10
- package/lib-esm/internals/generateModelsProperty.js +19 -312
- package/lib-esm/internals/operations/get.d.ts +2 -0
- package/lib-esm/internals/operations/get.js +55 -0
- package/lib-esm/internals/operations/list.d.ts +2 -0
- package/lib-esm/internals/operations/list.js +67 -0
- package/lib-esm/internals/operations/observeQuery.d.ts +2 -0
- package/lib-esm/internals/operations/observeQuery.js +115 -0
- package/lib-esm/internals/operations/subscription.d.ts +1 -0
- package/lib-esm/internals/operations/subscription.js +23 -0
- package/lib-esm/internals/server/generateClient.d.ts +11 -0
- package/lib-esm/internals/server/generateClient.js +26 -0
- package/lib-esm/internals/server/generateModelsProperty.d.ts +3 -0
- package/lib-esm/internals/server/generateModelsProperty.js +41 -0
- package/lib-esm/internals/server/index.d.ts +1 -0
- package/lib-esm/internals/server/index.js +3 -0
- package/lib-esm/internals/v6.js +1 -1
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/index.d.ts +36 -4
- package/lib-esm/types/index.js +3 -3
- package/lib-esm/utils/ConnectionStateMonitor.js +49 -63
- package/lib-esm/utils/ReachabilityMonitor/index.js +1 -1
- package/lib-esm/utils/ReachabilityMonitor/index.native.js +1 -3
- package/lib-esm/utils/ReconnectionMonitor.js +19 -24
- package/lib-esm/utils/errors/APIError.js +6 -11
- package/lib-esm/utils/errors/assertValidationError.js +2 -2
- package/lib-esm/utils/errors/validation.js +5 -6
- package/lib-esm/utils/findIndexByFields.js +4 -9
- package/lib-esm/utils/resolveConfig.js +8 -9
- package/lib-esm/utils/resolveLibraryOptions.js +4 -5
- package/lib-esm/utils/resolveOwnerFields.js +10 -33
- package/lib-esm/utils/resolvePKFields.js +2 -3
- package/package.json +8 -7
- package/src/GraphQLAPI.ts +6 -2
- package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +6 -5
- package/src/internals/APIClient.ts +122 -35
- package/src/internals/InternalGraphQLAPI.ts +35 -10
- package/src/internals/generateClient.ts +0 -1
- package/src/internals/generateModelsProperty.ts +27 -340
- package/src/internals/operations/get.ts +121 -0
- package/src/internals/operations/list.ts +111 -0
- package/src/internals/operations/observeQuery.ts +140 -0
- package/src/internals/operations/subscription.ts +59 -0
- package/src/internals/server/generateClient.ts +45 -0
- package/src/internals/server/generateModelsProperty.ts +70 -0
- package/src/internals/server/index.ts +4 -0
- package/src/types/index.ts +65 -9
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { __assign, __awaiter, __generator, __read } from "tslib";
|
|
2
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
3
|
import { print, parse, GraphQLError, } from 'graphql';
|
|
@@ -9,116 +8,98 @@ import { isCancelError as isCancelErrorREST } from '@aws-amplify/api-rest';
|
|
|
9
8
|
import { post, cancel as cancelREST, updateRequestToBeCancellable, } from '@aws-amplify/api-rest/internals';
|
|
10
9
|
import { AWSAppSyncRealTimeProvider } from '../Providers/AWSAppSyncRealTimeProvider';
|
|
11
10
|
import { resolveConfig, resolveLibraryOptions } from '../utils';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
const USER_AGENT_HEADER = 'x-amz-user-agent';
|
|
12
|
+
const logger = new ConsoleLogger('GraphQLAPI');
|
|
13
|
+
export const graphqlOperation = (query, variables = {}, authToken) => ({
|
|
14
|
+
query,
|
|
15
|
+
variables,
|
|
16
|
+
authToken,
|
|
17
|
+
});
|
|
18
|
+
const isAmplifyInstance = (amplify) => {
|
|
19
|
+
return typeof amplify !== 'function';
|
|
21
20
|
};
|
|
22
21
|
/**
|
|
23
22
|
* Export Cloud Logic APIs
|
|
24
23
|
*/
|
|
25
|
-
|
|
24
|
+
export class InternalGraphQLAPIClass {
|
|
26
25
|
/**
|
|
27
26
|
* Initialize GraphQL API with AWS configuration
|
|
28
27
|
* @param {Object} options - Configuration object for API
|
|
29
28
|
*/
|
|
30
|
-
|
|
29
|
+
constructor(options) {
|
|
31
30
|
this._api = {
|
|
32
|
-
post
|
|
33
|
-
cancelREST
|
|
34
|
-
isCancelErrorREST
|
|
35
|
-
updateRequestToBeCancellable
|
|
31
|
+
post,
|
|
32
|
+
cancelREST,
|
|
33
|
+
isCancelErrorREST,
|
|
34
|
+
updateRequestToBeCancellable,
|
|
36
35
|
};
|
|
37
36
|
this._options = options;
|
|
38
37
|
logger.debug('API Options', this._options);
|
|
39
38
|
}
|
|
40
|
-
|
|
39
|
+
getModuleName() {
|
|
41
40
|
return 'InternalGraphQLAPI';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
_b = resolveConfig(amplify), region = _b.region, appSyncGraphqlEndpoint = _b.endpoint, apiKey = _b.apiKey, defaultAuthMode = _b.defaultAuthMode;
|
|
53
|
-
authenticationType = authMode || defaultAuthMode || 'iam';
|
|
54
|
-
headers = {};
|
|
55
|
-
_c = authenticationType;
|
|
56
|
-
switch (_c) {
|
|
57
|
-
case 'apiKey': return [3 /*break*/, 1];
|
|
58
|
-
case 'iam': return [3 /*break*/, 2];
|
|
59
|
-
case 'oidc': return [3 /*break*/, 4];
|
|
60
|
-
case 'userPool': return [3 /*break*/, 4];
|
|
61
|
-
case 'lambda': return [3 /*break*/, 8];
|
|
62
|
-
case 'none': return [3 /*break*/, 9];
|
|
63
|
-
}
|
|
64
|
-
return [3 /*break*/, 10];
|
|
65
|
-
case 1:
|
|
66
|
-
if (!apiKey) {
|
|
67
|
-
throw new Error(GraphQLAuthError.NO_API_KEY);
|
|
68
|
-
}
|
|
69
|
-
headers = {
|
|
70
|
-
'X-Api-Key': apiKey,
|
|
71
|
-
};
|
|
72
|
-
return [3 /*break*/, 11];
|
|
73
|
-
case 2: return [4 /*yield*/, amplify.Auth.fetchAuthSession()];
|
|
74
|
-
case 3:
|
|
75
|
-
session = _d.sent();
|
|
76
|
-
if (session.credentials === undefined) {
|
|
77
|
-
throw new Error(GraphQLAuthError.NO_CREDENTIALS);
|
|
78
|
-
}
|
|
79
|
-
return [3 /*break*/, 11];
|
|
80
|
-
case 4:
|
|
81
|
-
_d.trys.push([4, 6, , 7]);
|
|
82
|
-
token = void 0;
|
|
83
|
-
return [4 /*yield*/, amplify.Auth.fetchAuthSession()];
|
|
84
|
-
case 5:
|
|
85
|
-
token = (_a = (_d.sent()).tokens) === null || _a === void 0 ? void 0 : _a.accessToken.toString();
|
|
86
|
-
if (!token) {
|
|
87
|
-
throw new Error(GraphQLAuthError.NO_FEDERATED_JWT);
|
|
88
|
-
}
|
|
89
|
-
headers = {
|
|
90
|
-
Authorization: token,
|
|
91
|
-
};
|
|
92
|
-
return [3 /*break*/, 7];
|
|
93
|
-
case 6:
|
|
94
|
-
e_1 = _d.sent();
|
|
95
|
-
throw new Error(GraphQLAuthError.NO_CURRENT_USER);
|
|
96
|
-
case 7: return [3 /*break*/, 11];
|
|
97
|
-
case 8:
|
|
98
|
-
if (!additionalHeaders.Authorization) {
|
|
99
|
-
throw new Error(GraphQLAuthError.NO_AUTH_TOKEN);
|
|
100
|
-
}
|
|
101
|
-
headers = {
|
|
102
|
-
Authorization: additionalHeaders.Authorization,
|
|
103
|
-
};
|
|
104
|
-
return [3 /*break*/, 11];
|
|
105
|
-
case 9: return [3 /*break*/, 11];
|
|
106
|
-
case 10: return [3 /*break*/, 11];
|
|
107
|
-
case 11: return [2 /*return*/, headers];
|
|
41
|
+
}
|
|
42
|
+
async _headerBasedAuth(amplify, authMode, additionalHeaders = {}) {
|
|
43
|
+
const config = amplify.getConfig();
|
|
44
|
+
const { region: region, endpoint: appSyncGraphqlEndpoint, apiKey, defaultAuthMode, } = resolveConfig(amplify);
|
|
45
|
+
const authenticationType = authMode || defaultAuthMode || 'iam';
|
|
46
|
+
let headers = {};
|
|
47
|
+
switch (authenticationType) {
|
|
48
|
+
case 'apiKey':
|
|
49
|
+
if (!apiKey) {
|
|
50
|
+
throw new Error(GraphQLAuthError.NO_API_KEY);
|
|
108
51
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
52
|
+
headers = {
|
|
53
|
+
'X-Api-Key': apiKey,
|
|
54
|
+
};
|
|
55
|
+
break;
|
|
56
|
+
case 'iam':
|
|
57
|
+
const session = await amplify.Auth.fetchAuthSession();
|
|
58
|
+
if (session.credentials === undefined) {
|
|
59
|
+
throw new Error(GraphQLAuthError.NO_CREDENTIALS);
|
|
60
|
+
}
|
|
61
|
+
break;
|
|
62
|
+
case 'oidc':
|
|
63
|
+
case 'userPool':
|
|
64
|
+
try {
|
|
65
|
+
let token;
|
|
66
|
+
token = (await amplify.Auth.fetchAuthSession()).tokens?.accessToken.toString();
|
|
67
|
+
if (!token) {
|
|
68
|
+
throw new Error(GraphQLAuthError.NO_FEDERATED_JWT);
|
|
69
|
+
}
|
|
70
|
+
headers = {
|
|
71
|
+
Authorization: token,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
throw new Error(GraphQLAuthError.NO_CURRENT_USER);
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
78
|
+
case 'lambda':
|
|
79
|
+
if (!additionalHeaders.Authorization) {
|
|
80
|
+
throw new Error(GraphQLAuthError.NO_AUTH_TOKEN);
|
|
81
|
+
}
|
|
82
|
+
headers = {
|
|
83
|
+
Authorization: additionalHeaders.Authorization,
|
|
84
|
+
};
|
|
85
|
+
break;
|
|
86
|
+
case 'none':
|
|
87
|
+
break;
|
|
88
|
+
default:
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
return headers;
|
|
92
|
+
}
|
|
112
93
|
/**
|
|
113
94
|
* to get the operation type
|
|
114
95
|
* @param operation
|
|
115
96
|
*/
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
97
|
+
getGraphqlOperationType(operation) {
|
|
98
|
+
const doc = parse(operation);
|
|
99
|
+
const definitions = doc.definitions;
|
|
100
|
+
const [{ operation: operationType }] = definitions;
|
|
120
101
|
return operationType;
|
|
121
|
-
}
|
|
102
|
+
}
|
|
122
103
|
/**
|
|
123
104
|
* Executes a GraphQL operation
|
|
124
105
|
*
|
|
@@ -126,14 +107,13 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
|
|
|
126
107
|
* @param [additionalHeaders] - headers to merge in after any `graphql_headers` set in the config
|
|
127
108
|
* @returns An Observable if the query is a subscription query, else a promise of the graphql result.
|
|
128
109
|
*/
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
var query = typeof paramQuery === 'string'
|
|
110
|
+
graphql(amplify, { query: paramQuery, variables = {}, authMode, authToken }, additionalHeaders, customUserAgentDetails) {
|
|
111
|
+
const query = typeof paramQuery === 'string'
|
|
132
112
|
? parse(paramQuery)
|
|
133
113
|
: parse(print(paramQuery));
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
114
|
+
const [operationDef = {}] = query.definitions.filter(def => def.kind === 'OperationDefinition');
|
|
115
|
+
const { operation: operationType } = operationDef;
|
|
116
|
+
const headers = additionalHeaders || {};
|
|
137
117
|
// if an authorization header is set, have the explicit authToken take precedence
|
|
138
118
|
if (authToken) {
|
|
139
119
|
headers.Authorization = authToken;
|
|
@@ -141,174 +121,151 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
|
|
|
141
121
|
switch (operationType) {
|
|
142
122
|
case 'query':
|
|
143
123
|
case 'mutation':
|
|
144
|
-
|
|
145
|
-
|
|
124
|
+
const abortController = new AbortController();
|
|
125
|
+
let responsePromise;
|
|
126
|
+
if (isAmplifyInstance(amplify)) {
|
|
127
|
+
responsePromise = this._graphql(amplify, { query, variables, authMode }, headers, abortController, customUserAgentDetails);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const wrapper = amplifyInstance => this._graphql(amplifyInstance, { query, variables, authMode }, headers, abortController, customUserAgentDetails);
|
|
131
|
+
responsePromise = amplify(wrapper);
|
|
132
|
+
}
|
|
146
133
|
this._api.updateRequestToBeCancellable(responsePromise, abortController);
|
|
147
134
|
return responsePromise;
|
|
148
135
|
case 'subscription':
|
|
149
|
-
return this._graphqlSubscribe(amplify, { query
|
|
136
|
+
return this._graphqlSubscribe(amplify, { query, variables, authMode }, headers, customUserAgentDetails);
|
|
150
137
|
default:
|
|
151
|
-
throw new Error(
|
|
138
|
+
throw new Error(`invalid operation type: ${operationType}`);
|
|
152
139
|
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
throw {
|
|
231
|
-
data: {},
|
|
232
|
-
errors: [error],
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
_m.label = 9;
|
|
236
|
-
case 9:
|
|
237
|
-
_m.trys.push([9, 12, , 13]);
|
|
238
|
-
return [4 /*yield*/, this._api.post({
|
|
239
|
-
url: new AmplifyUrl(endpoint),
|
|
240
|
-
options: {
|
|
241
|
-
headers: headers,
|
|
242
|
-
body: body,
|
|
243
|
-
signingServiceInfo: signingServiceInfo,
|
|
244
|
-
withCredentials: withCredentials,
|
|
245
|
-
},
|
|
246
|
-
abortController: abortController,
|
|
247
|
-
})];
|
|
248
|
-
case 10:
|
|
249
|
-
responseBody = (_m.sent()).body;
|
|
250
|
-
return [4 /*yield*/, responseBody.json()];
|
|
251
|
-
case 11:
|
|
252
|
-
result = _m.sent();
|
|
253
|
-
response = result;
|
|
254
|
-
return [3 /*break*/, 13];
|
|
255
|
-
case 12:
|
|
256
|
-
err_1 = _m.sent();
|
|
257
|
-
// If the exception is because user intentionally
|
|
258
|
-
// cancelled the request, do not modify the exception
|
|
259
|
-
// so that clients can identify the exception correctly.
|
|
260
|
-
if (this._api.isCancelErrorREST(err_1)) {
|
|
261
|
-
throw err_1;
|
|
262
|
-
}
|
|
263
|
-
response = {
|
|
264
|
-
data: {},
|
|
265
|
-
errors: [new GraphQLError(err_1.message, null, null, null, null, err_1)],
|
|
266
|
-
};
|
|
267
|
-
return [3 /*break*/, 13];
|
|
268
|
-
case 13:
|
|
269
|
-
errors = response.errors;
|
|
270
|
-
if (errors && errors.length) {
|
|
271
|
-
throw response;
|
|
272
|
-
}
|
|
273
|
-
return [2 /*return*/, response];
|
|
274
|
-
}
|
|
140
|
+
}
|
|
141
|
+
async _graphql(amplify, { query, variables, authMode }, additionalHeaders = {}, abortController, customUserAgentDetails) {
|
|
142
|
+
const { region: region, endpoint: appSyncGraphqlEndpoint, customEndpoint, customEndpointRegion, } = resolveConfig(amplify);
|
|
143
|
+
// Retrieve library options from Amplify configuration
|
|
144
|
+
const { headers: customHeaders, withCredentials } = resolveLibraryOptions(amplify);
|
|
145
|
+
// TODO: Figure what we need to do to remove `!`'s.
|
|
146
|
+
const headers = {
|
|
147
|
+
...(!customEndpoint &&
|
|
148
|
+
(await this._headerBasedAuth(amplify, authMode, additionalHeaders))),
|
|
149
|
+
/**
|
|
150
|
+
* Custom endpoint headers.
|
|
151
|
+
* If there is both a custom endpoint and custom region present, we get the headers.
|
|
152
|
+
* If there is a custom endpoint but no region, we return an empty object.
|
|
153
|
+
* If neither are present, we return an empty object.
|
|
154
|
+
*/
|
|
155
|
+
...((customEndpoint &&
|
|
156
|
+
(customEndpointRegion
|
|
157
|
+
? await this._headerBasedAuth(amplify, authMode, additionalHeaders)
|
|
158
|
+
: {})) ||
|
|
159
|
+
{}),
|
|
160
|
+
// Custom headers included in Amplify configuration options:
|
|
161
|
+
...(customHeaders &&
|
|
162
|
+
(await customHeaders({
|
|
163
|
+
query: print(query),
|
|
164
|
+
variables,
|
|
165
|
+
}))),
|
|
166
|
+
// Headers from individual calls to `graphql`:
|
|
167
|
+
...additionalHeaders,
|
|
168
|
+
// User agent headers:
|
|
169
|
+
...(!customEndpoint && {
|
|
170
|
+
[USER_AGENT_HEADER]: getAmplifyUserAgent(customUserAgentDetails),
|
|
171
|
+
}),
|
|
172
|
+
};
|
|
173
|
+
const body = {
|
|
174
|
+
query: print(query),
|
|
175
|
+
variables: variables || null,
|
|
176
|
+
};
|
|
177
|
+
let signingServiceInfo;
|
|
178
|
+
/**
|
|
179
|
+
* We do not send the signing service info to the REST API under the
|
|
180
|
+
* following conditions (i.e. it will not sign the request):
|
|
181
|
+
* - there is a custom endpoint but no region
|
|
182
|
+
* - the auth mode is `none`, or `apiKey`
|
|
183
|
+
* - the auth mode is a type other than the types listed below
|
|
184
|
+
*/
|
|
185
|
+
if ((customEndpoint && !customEndpointRegion) ||
|
|
186
|
+
(authMode !== 'oidc' &&
|
|
187
|
+
authMode !== 'userPool' &&
|
|
188
|
+
authMode !== 'iam' &&
|
|
189
|
+
authMode !== 'lambda')) {
|
|
190
|
+
signingServiceInfo = null;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
signingServiceInfo = {
|
|
194
|
+
service: !customEndpointRegion ? 'appsync' : 'execute-api',
|
|
195
|
+
region: !customEndpointRegion ? region : customEndpointRegion,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
const endpoint = customEndpoint || appSyncGraphqlEndpoint;
|
|
199
|
+
if (!endpoint) {
|
|
200
|
+
const error = new GraphQLError('No graphql endpoint provided.');
|
|
201
|
+
throw {
|
|
202
|
+
data: {},
|
|
203
|
+
errors: [error],
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
let response;
|
|
207
|
+
try {
|
|
208
|
+
const { body: responseBody } = await this._api.post({
|
|
209
|
+
url: new AmplifyUrl(endpoint),
|
|
210
|
+
options: {
|
|
211
|
+
headers,
|
|
212
|
+
body,
|
|
213
|
+
signingServiceInfo,
|
|
214
|
+
withCredentials,
|
|
215
|
+
},
|
|
216
|
+
abortController,
|
|
275
217
|
});
|
|
276
|
-
|
|
277
|
-
|
|
218
|
+
const result = await responseBody.json();
|
|
219
|
+
response = result;
|
|
220
|
+
}
|
|
221
|
+
catch (err) {
|
|
222
|
+
// If the exception is because user intentionally
|
|
223
|
+
// cancelled the request, do not modify the exception
|
|
224
|
+
// so that clients can identify the exception correctly.
|
|
225
|
+
if (this.isCancelError(err)) {
|
|
226
|
+
throw err;
|
|
227
|
+
}
|
|
228
|
+
response = {
|
|
229
|
+
data: {},
|
|
230
|
+
errors: [new GraphQLError(err.message, null, null, null, null, err)],
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
const { errors } = response;
|
|
234
|
+
if (errors && errors.length) {
|
|
235
|
+
throw response;
|
|
236
|
+
}
|
|
237
|
+
return response;
|
|
238
|
+
}
|
|
278
239
|
/**
|
|
279
240
|
* Checks to see if an error thrown is from an api request cancellation
|
|
280
241
|
* @param {any} error - Any error
|
|
281
242
|
* @return {boolean} - A boolean indicating if the error was from an api request cancellation
|
|
282
243
|
*/
|
|
283
|
-
|
|
244
|
+
isCancelError(error) {
|
|
284
245
|
return this._api.isCancelErrorREST(error);
|
|
285
|
-
}
|
|
246
|
+
}
|
|
286
247
|
/**
|
|
287
248
|
* Cancels an inflight request. Only applicable for graphql queries and mutations
|
|
288
249
|
* @param {any} request - request to cancel
|
|
289
250
|
* @returns - A boolean indicating if the request was cancelled
|
|
290
251
|
*/
|
|
291
|
-
|
|
252
|
+
cancel(request, message) {
|
|
292
253
|
return this._api.cancelREST(request, message);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
if (additionalHeaders === void 0) { additionalHeaders = {}; }
|
|
297
|
-
var config = resolveConfig(amplify);
|
|
254
|
+
}
|
|
255
|
+
_graphqlSubscribe(amplify, { query, variables, authMode }, additionalHeaders = {}, customUserAgentDetails) {
|
|
256
|
+
const config = resolveConfig(amplify);
|
|
298
257
|
if (!this.appSyncRealTime) {
|
|
299
258
|
this.appSyncRealTime = new AWSAppSyncRealTimeProvider();
|
|
300
259
|
}
|
|
301
260
|
return this.appSyncRealTime.subscribe({
|
|
302
261
|
query: print(query),
|
|
303
|
-
variables
|
|
304
|
-
appSyncGraphqlEndpoint: config
|
|
305
|
-
region: config
|
|
306
|
-
authenticationType: authMode ||
|
|
307
|
-
apiKey: config
|
|
308
|
-
additionalHeaders
|
|
262
|
+
variables,
|
|
263
|
+
appSyncGraphqlEndpoint: config?.endpoint,
|
|
264
|
+
region: config?.region,
|
|
265
|
+
authenticationType: authMode || config?.defaultAuthMode,
|
|
266
|
+
apiKey: config?.apiKey,
|
|
267
|
+
additionalHeaders,
|
|
309
268
|
}, customUserAgentDetails);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
export { InternalGraphQLAPIClass };
|
|
314
|
-
export var InternalGraphQLAPI = new InternalGraphQLAPIClass(null);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
export const InternalGraphQLAPI = new InternalGraphQLAPIClass(null);
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
1
3
|
import { graphql, cancel, isCancelError } from './v6';
|
|
2
4
|
import { generateModelsProperty } from './generateModelsProperty';
|
|
3
5
|
import { __amplify, __authMode, __authToken } from '../types';
|
|
@@ -11,16 +13,15 @@ import { __amplify, __authMode, __authToken } from '../types';
|
|
|
11
13
|
* @returns
|
|
12
14
|
*/
|
|
13
15
|
export function generateClient(params) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
_a);
|
|
16
|
+
const client = {
|
|
17
|
+
[__amplify]: params.amplify,
|
|
18
|
+
[__authMode]: params.authMode,
|
|
19
|
+
[__authToken]: params.authToken,
|
|
20
|
+
graphql,
|
|
21
|
+
cancel,
|
|
22
|
+
isCancelError,
|
|
23
|
+
models: {},
|
|
24
|
+
};
|
|
24
25
|
client.models = generateModelsProperty(client, params);
|
|
25
26
|
return client;
|
|
26
27
|
}
|