@aws-amplify/api-graphql 4.0.1-api-v6-models.9351bcf.0 → 4.0.1-api-v6-models.891fe0d.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,327 +1,34 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
/**
|
|
7
|
-
* Produces a parameter object that can contains auth mode/token overrides
|
|
8
|
-
* only if present in either `options` (first) or configured on the `client`
|
|
9
|
-
* as a fallback.
|
|
10
|
-
*
|
|
11
|
-
* @param client Configured client from `generateClient`
|
|
12
|
-
* @param options Args/Options obect from call site.
|
|
13
|
-
* @returns
|
|
14
|
-
*/
|
|
15
|
-
function authModeParams(client, options) {
|
|
16
|
-
if (options === void 0) { options = {}; }
|
|
17
|
-
return {
|
|
18
|
-
authMode: options.authMode || client[__authMode],
|
|
19
|
-
authToken: options.authToken || client[__authToken],
|
|
20
|
-
};
|
|
21
|
-
}
|
|
1
|
+
import { graphQLOperationsInfo } from './APIClient';
|
|
2
|
+
import { listFactory } from './operations/list';
|
|
3
|
+
import { getFactory } from './operations/get';
|
|
4
|
+
import { subscriptionFactory } from './operations/subscription';
|
|
5
|
+
import { observeQueryFactory } from './operations/observeQuery';
|
|
22
6
|
export function generateModelsProperty(client, params) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
var models = {};
|
|
27
|
-
var config = params.amplify.getConfig();
|
|
28
|
-
var modelIntrospection = (_c = (_b = config.API) === null || _b === void 0 ? void 0 : _b.GraphQL) === null || _c === void 0 ? void 0 : _c.modelIntrospection;
|
|
7
|
+
const models = {};
|
|
8
|
+
const config = params.amplify.getConfig();
|
|
9
|
+
const modelIntrospection = config.API?.GraphQL?.modelIntrospection;
|
|
29
10
|
if (!modelIntrospection) {
|
|
30
11
|
return {};
|
|
31
12
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
13
|
+
const SUBSCRIPTION_OPS = ['ONCREATE', 'ONUPDATE', 'ONDELETE'];
|
|
14
|
+
for (const model of Object.values(modelIntrospection.models)) {
|
|
15
|
+
const { name } = model;
|
|
16
|
+
models[name] = {};
|
|
17
|
+
Object.entries(graphQLOperationsInfo).forEach(([key, { operationPrefix }]) => {
|
|
18
|
+
const operation = key;
|
|
38
19
|
if (operation === 'LIST') {
|
|
39
|
-
models[
|
|
40
|
-
var query, variables, auth, _a, data, extensions, _b, key_1, flattenedResult, initialized, error_1;
|
|
41
|
-
return __generator(this, function (_c) {
|
|
42
|
-
switch (_c.label) {
|
|
43
|
-
case 0:
|
|
44
|
-
query = generateGraphQLDocument(modelIntrospection.models, name_1, 'LIST', args);
|
|
45
|
-
variables = buildGraphQLVariables(model, 'LIST', args, modelIntrospection);
|
|
46
|
-
_c.label = 1;
|
|
47
|
-
case 1:
|
|
48
|
-
_c.trys.push([1, 3, , 4]);
|
|
49
|
-
auth = authModeParams(client, args);
|
|
50
|
-
return [4 /*yield*/, client.graphql(__assign(__assign({}, auth), { query: query, variables: variables }))];
|
|
51
|
-
case 2:
|
|
52
|
-
_a = (_c.sent()), data = _a.data, extensions = _a.extensions;
|
|
53
|
-
// flatten response
|
|
54
|
-
if (data !== undefined) {
|
|
55
|
-
_b = __read(Object.keys(data), 1), key_1 = _b[0];
|
|
56
|
-
if (data[key_1].items) {
|
|
57
|
-
flattenedResult = flattenItems(data)[key_1];
|
|
58
|
-
// don't init if custom selection set
|
|
59
|
-
if (args === null || args === void 0 ? void 0 : args.selectionSet) {
|
|
60
|
-
return [2 /*return*/, {
|
|
61
|
-
data: flattenedResult,
|
|
62
|
-
nextToken: data[key_1].nextToken,
|
|
63
|
-
extensions: extensions,
|
|
64
|
-
}];
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
initialized = initializeModel(client, name_1, flattenedResult, modelIntrospection, auth.authMode, auth.authToken);
|
|
68
|
-
return [2 /*return*/, {
|
|
69
|
-
data: initialized,
|
|
70
|
-
nextToken: data[key_1].nextToken,
|
|
71
|
-
extensions: extensions,
|
|
72
|
-
}];
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return [2 /*return*/, {
|
|
76
|
-
data: data[key_1],
|
|
77
|
-
nextToken: data[key_1].nextToken,
|
|
78
|
-
extensions: extensions,
|
|
79
|
-
}];
|
|
80
|
-
}
|
|
81
|
-
return [3 /*break*/, 4];
|
|
82
|
-
case 3:
|
|
83
|
-
error_1 = _c.sent();
|
|
84
|
-
if (error_1.errors) {
|
|
85
|
-
// graphql errors pass through
|
|
86
|
-
return [2 /*return*/, error_1];
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
// non-graphql errors re re-thrown
|
|
90
|
-
throw error_1;
|
|
91
|
-
}
|
|
92
|
-
return [3 /*break*/, 4];
|
|
93
|
-
case 4: return [2 /*return*/];
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}); };
|
|
20
|
+
models[name][operationPrefix] = listFactory(client, modelIntrospection, model);
|
|
97
21
|
}
|
|
98
|
-
else if (
|
|
99
|
-
models[
|
|
100
|
-
var query = generateGraphQLDocument(modelIntrospection.models, name_1, operation);
|
|
101
|
-
var variables = buildGraphQLVariables(model, operation, args, modelIntrospection);
|
|
102
|
-
var auth = authModeParams(client, args);
|
|
103
|
-
var observable = client.graphql(__assign(__assign({}, auth), { query: query, variables: variables }));
|
|
104
|
-
return observable.pipe(map(function (value) {
|
|
105
|
-
var _a = __read(Object.keys(value.data), 1), key = _a[0];
|
|
106
|
-
var _b = __read(initializeModel(client, name_1, [value.data[key]], modelIntrospection, auth.authMode, auth.authToken), 1), initialized = _b[0];
|
|
107
|
-
return initialized;
|
|
108
|
-
}));
|
|
109
|
-
};
|
|
22
|
+
else if (SUBSCRIPTION_OPS.includes(operation)) {
|
|
23
|
+
models[name][operationPrefix] = subscriptionFactory(client, modelIntrospection, model, operation);
|
|
110
24
|
}
|
|
111
25
|
else if (operation === 'OBSERVE_QUERY') {
|
|
112
|
-
models[
|
|
113
|
-
return new Observable(function (subscriber) {
|
|
114
|
-
// what we'll be sending to our subscribers
|
|
115
|
-
var items = [];
|
|
116
|
-
// To enqueue subscription messages while we collect our initial
|
|
117
|
-
// result set.
|
|
118
|
-
var messageQueue = [];
|
|
119
|
-
// operation to take when message(s) arrive.
|
|
120
|
-
// this operation will be swapped out once initial "sync" is complete
|
|
121
|
-
// to immediately ingest messsages.
|
|
122
|
-
var receiveMessages = function () {
|
|
123
|
-
var messages = [];
|
|
124
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
125
|
-
messages[_i] = arguments[_i];
|
|
126
|
-
}
|
|
127
|
-
return messageQueue.push.apply(messageQueue, __spreadArray([], __read(messages), false));
|
|
128
|
-
};
|
|
129
|
-
// start subscriptions
|
|
130
|
-
var onCreateSub = models[name_1].onCreate(arg).subscribe({
|
|
131
|
-
next: function (item) {
|
|
132
|
-
receiveMessages({ item: item, type: 'create' });
|
|
133
|
-
},
|
|
134
|
-
error: function (error) {
|
|
135
|
-
subscriber.error({ type: 'onCreate', error: error });
|
|
136
|
-
},
|
|
137
|
-
});
|
|
138
|
-
var onUpdateSub = models[name_1].onUpdate(arg).subscribe({
|
|
139
|
-
next: function (item) {
|
|
140
|
-
receiveMessages({ item: item, type: 'update' });
|
|
141
|
-
},
|
|
142
|
-
error: function (error) {
|
|
143
|
-
subscriber.error({ type: 'onUpdate', error: error });
|
|
144
|
-
},
|
|
145
|
-
});
|
|
146
|
-
var onDeleteSub = models[name_1].onDelete(arg).subscribe({
|
|
147
|
-
next: function (item) {
|
|
148
|
-
receiveMessages({ item: item, type: 'delete' });
|
|
149
|
-
},
|
|
150
|
-
error: function (error) {
|
|
151
|
-
subscriber.error({ type: 'onDelete', error: error });
|
|
152
|
-
},
|
|
153
|
-
});
|
|
154
|
-
// consumes a list of messages and sends a snapshot
|
|
155
|
-
function ingestMessages(messages) {
|
|
156
|
-
var e_2, _a;
|
|
157
|
-
try {
|
|
158
|
-
for (var messages_1 = __values(messages), messages_1_1 = messages_1.next(); !messages_1_1.done; messages_1_1 = messages_1.next()) {
|
|
159
|
-
var message = messages_1_1.value;
|
|
160
|
-
var idx = findIndexByFields(message.item, items, pkFields);
|
|
161
|
-
switch (message.type) {
|
|
162
|
-
case 'create':
|
|
163
|
-
if (idx < 0)
|
|
164
|
-
items.push(message.item);
|
|
165
|
-
break;
|
|
166
|
-
case 'update':
|
|
167
|
-
if (idx >= 0)
|
|
168
|
-
items[idx] = message.item;
|
|
169
|
-
break;
|
|
170
|
-
case 'delete':
|
|
171
|
-
if (idx >= 0)
|
|
172
|
-
items.splice(idx, 1);
|
|
173
|
-
break;
|
|
174
|
-
default:
|
|
175
|
-
console.error('Unrecognized message in observeQuery.', message);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
180
|
-
finally {
|
|
181
|
-
try {
|
|
182
|
-
if (messages_1_1 && !messages_1_1.done && (_a = messages_1.return)) _a.call(messages_1);
|
|
183
|
-
}
|
|
184
|
-
finally { if (e_2) throw e_2.error; }
|
|
185
|
-
}
|
|
186
|
-
subscriber.next({
|
|
187
|
-
items: items,
|
|
188
|
-
isSynced: true,
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
var pkFields = resolvePKFields(model);
|
|
192
|
-
// initial results
|
|
193
|
-
(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
194
|
-
var firstPage, nextToken, _a, page, errors, _nextToken, isSynced, errors_1, errors_1_1, error;
|
|
195
|
-
var e_3, _b;
|
|
196
|
-
return __generator(this, function (_c) {
|
|
197
|
-
switch (_c.label) {
|
|
198
|
-
case 0:
|
|
199
|
-
firstPage = true;
|
|
200
|
-
nextToken = null;
|
|
201
|
-
_c.label = 1;
|
|
202
|
-
case 1:
|
|
203
|
-
if (!(!subscriber.closed && (firstPage || nextToken))) return [3 /*break*/, 3];
|
|
204
|
-
firstPage = false;
|
|
205
|
-
return [4 /*yield*/, models[name_1].list(__assign(__assign({}, arg), { nextToken: nextToken }))];
|
|
206
|
-
case 2:
|
|
207
|
-
_a = _c.sent(), page = _a.data, errors = _a.errors, _nextToken = _a.nextToken;
|
|
208
|
-
nextToken = _nextToken;
|
|
209
|
-
items.push.apply(items, __spreadArray([], __read(page), false));
|
|
210
|
-
isSynced = messageQueue.length === 0 &&
|
|
211
|
-
(nextToken === null || nextToken === undefined);
|
|
212
|
-
subscriber.next({
|
|
213
|
-
items: items,
|
|
214
|
-
isSynced: isSynced,
|
|
215
|
-
});
|
|
216
|
-
if (Array.isArray(errors)) {
|
|
217
|
-
try {
|
|
218
|
-
for (errors_1 = (e_3 = void 0, __values(errors)), errors_1_1 = errors_1.next(); !errors_1_1.done; errors_1_1 = errors_1.next()) {
|
|
219
|
-
error = errors_1_1.value;
|
|
220
|
-
subscriber.error(error);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
224
|
-
finally {
|
|
225
|
-
try {
|
|
226
|
-
if (errors_1_1 && !errors_1_1.done && (_b = errors_1.return)) _b.call(errors_1);
|
|
227
|
-
}
|
|
228
|
-
finally { if (e_3) throw e_3.error; }
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
return [3 /*break*/, 1];
|
|
232
|
-
case 3:
|
|
233
|
-
// play through the queue
|
|
234
|
-
if (messageQueue.length > 0) {
|
|
235
|
-
ingestMessages(messageQueue);
|
|
236
|
-
}
|
|
237
|
-
// switch the queue to write directly to the items collection
|
|
238
|
-
receiveMessages = function () {
|
|
239
|
-
var messages = [];
|
|
240
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
241
|
-
messages[_i] = arguments[_i];
|
|
242
|
-
}
|
|
243
|
-
ingestMessages(messages);
|
|
244
|
-
return items.length;
|
|
245
|
-
};
|
|
246
|
-
return [2 /*return*/];
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
}); })();
|
|
250
|
-
// when subscriber unsubscribes, tear down internal subs
|
|
251
|
-
return function () {
|
|
252
|
-
// 1. tear down internal subs
|
|
253
|
-
onCreateSub.unsubscribe();
|
|
254
|
-
onUpdateSub.unsubscribe();
|
|
255
|
-
onDeleteSub.unsubscribe();
|
|
256
|
-
// 2. there is no need to explicitly stop paging. instead, we
|
|
257
|
-
// just check `subscriber.closed` above before fetching each page.
|
|
258
|
-
};
|
|
259
|
-
});
|
|
260
|
-
};
|
|
26
|
+
models[name][operationPrefix] = observeQueryFactory(models, model);
|
|
261
27
|
}
|
|
262
28
|
else {
|
|
263
|
-
models[
|
|
264
|
-
var query, variables, auth, _a, data, extensions, _b, key_2, flattenedResult, _c, initialized, error_2;
|
|
265
|
-
return __generator(this, function (_d) {
|
|
266
|
-
switch (_d.label) {
|
|
267
|
-
case 0:
|
|
268
|
-
query = generateGraphQLDocument(modelIntrospection.models, name_1, operation, options);
|
|
269
|
-
variables = buildGraphQLVariables(model, operation, arg, modelIntrospection);
|
|
270
|
-
_d.label = 1;
|
|
271
|
-
case 1:
|
|
272
|
-
_d.trys.push([1, 3, , 4]);
|
|
273
|
-
auth = authModeParams(client, options);
|
|
274
|
-
return [4 /*yield*/, client.graphql(__assign(__assign({}, auth), { query: query, variables: variables }))];
|
|
275
|
-
case 2:
|
|
276
|
-
_a = (_d.sent()), data = _a.data, extensions = _a.extensions;
|
|
277
|
-
// flatten response
|
|
278
|
-
if (data !== undefined) {
|
|
279
|
-
_b = __read(Object.keys(data), 1), key_2 = _b[0];
|
|
280
|
-
flattenedResult = flattenItems(data)[key_2];
|
|
281
|
-
if (options === null || options === void 0 ? void 0 : options.selectionSet) {
|
|
282
|
-
return [2 /*return*/, { data: flattenedResult, extensions: extensions }];
|
|
283
|
-
}
|
|
284
|
-
else {
|
|
285
|
-
_c = __read(initializeModel(client, name_1, [flattenedResult], modelIntrospection, auth.authMode, auth.authToken), 1), initialized = _c[0];
|
|
286
|
-
return [2 /*return*/, { data: initialized, extensions: extensions }];
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
else {
|
|
290
|
-
return [2 /*return*/, { data: null, extensions: extensions }];
|
|
291
|
-
}
|
|
292
|
-
return [3 /*break*/, 4];
|
|
293
|
-
case 3:
|
|
294
|
-
error_2 = _d.sent();
|
|
295
|
-
if (error_2.errors) {
|
|
296
|
-
// graphql errors pass through
|
|
297
|
-
return [2 /*return*/, error_2];
|
|
298
|
-
}
|
|
299
|
-
else {
|
|
300
|
-
// non-graphql errors re re-thrown
|
|
301
|
-
throw error_2;
|
|
302
|
-
}
|
|
303
|
-
return [3 /*break*/, 4];
|
|
304
|
-
case 4: return [2 /*return*/];
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
}); };
|
|
29
|
+
models[name][operationPrefix] = getFactory(client, modelIntrospection, model, operation);
|
|
308
30
|
}
|
|
309
31
|
});
|
|
310
|
-
};
|
|
311
|
-
try {
|
|
312
|
-
// TODO: refactor this to use separate methods for each CRUDL.
|
|
313
|
-
// Doesn't make sense to gen the methods dynamically given the different args and return values
|
|
314
|
-
for (var _d = __values(Object.values(modelIntrospection.models)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
315
|
-
var model = _e.value;
|
|
316
|
-
_loop_1(model);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
320
|
-
finally {
|
|
321
|
-
try {
|
|
322
|
-
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
323
|
-
}
|
|
324
|
-
finally { if (e_1) throw e_1.error; }
|
|
325
32
|
}
|
|
326
33
|
return models;
|
|
327
34
|
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
|
|
2
|
+
export declare function getFactory(client: any, modelIntrospection: any, model: any, operation: any, context?: boolean): (contextSpec: AmplifyServer.ContextSpec, arg?: any, options?: any) => Promise<any>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { initializeModel, generateGraphQLDocument, buildGraphQLVariables, flattenItems, authModeParams, } from '../APIClient';
|
|
2
|
+
export function getFactory(client, modelIntrospection, model, operation, context = false) {
|
|
3
|
+
const getWithContext = async (contextSpec, arg, options) => {
|
|
4
|
+
return _get(client, modelIntrospection, model, arg, options, operation, contextSpec);
|
|
5
|
+
};
|
|
6
|
+
const get = async (arg, options) => {
|
|
7
|
+
return _get(client, modelIntrospection, model, arg, options, operation, context);
|
|
8
|
+
};
|
|
9
|
+
return context ? getWithContext : get;
|
|
10
|
+
}
|
|
11
|
+
async function _get(client, modelIntrospection, model, arg, options, operation, context) {
|
|
12
|
+
const { name } = model;
|
|
13
|
+
const query = generateGraphQLDocument(modelIntrospection.models, name, operation, options);
|
|
14
|
+
const variables = buildGraphQLVariables(model, operation, arg, modelIntrospection);
|
|
15
|
+
try {
|
|
16
|
+
const auth = authModeParams(client, options);
|
|
17
|
+
const { data, extensions } = context
|
|
18
|
+
? (await client.graphql(context, {
|
|
19
|
+
...auth,
|
|
20
|
+
query,
|
|
21
|
+
variables,
|
|
22
|
+
}))
|
|
23
|
+
: (await client.graphql({
|
|
24
|
+
...auth,
|
|
25
|
+
query,
|
|
26
|
+
variables,
|
|
27
|
+
}));
|
|
28
|
+
// flatten response
|
|
29
|
+
if (data !== undefined) {
|
|
30
|
+
const [key] = Object.keys(data);
|
|
31
|
+
const flattenedResult = flattenItems(data)[key];
|
|
32
|
+
if (options?.selectionSet) {
|
|
33
|
+
return { data: flattenedResult, extensions };
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// TODO: refactor to avoid destructuring here
|
|
37
|
+
const [initialized] = initializeModel(client, name, [flattenedResult], modelIntrospection, auth.authMode, auth.authToken, context);
|
|
38
|
+
return { data: initialized, extensions };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return { data: null, extensions };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error.errors) {
|
|
47
|
+
// graphql errors pass through
|
|
48
|
+
return error;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
// non-graphql errors re re-thrown
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { initializeModel, generateGraphQLDocument, buildGraphQLVariables, flattenItems, authModeParams, } from '../APIClient';
|
|
2
|
+
export function listFactory(client, modelIntrospection, model, context = false) {
|
|
3
|
+
const listWithContext = async (contextSpec, args) => {
|
|
4
|
+
return _list(client, modelIntrospection, model, args, contextSpec);
|
|
5
|
+
};
|
|
6
|
+
const list = async (args) => {
|
|
7
|
+
return _list(client, modelIntrospection, model, args, context);
|
|
8
|
+
};
|
|
9
|
+
return context ? listWithContext : list;
|
|
10
|
+
}
|
|
11
|
+
async function _list(client, modelIntrospection, model, args, context) {
|
|
12
|
+
const { name } = model;
|
|
13
|
+
const query = generateGraphQLDocument(modelIntrospection.models, name, 'LIST', args);
|
|
14
|
+
const variables = buildGraphQLVariables(model, 'LIST', args, modelIntrospection);
|
|
15
|
+
try {
|
|
16
|
+
const auth = authModeParams(client, args);
|
|
17
|
+
const { data, extensions } = context
|
|
18
|
+
? (await client.graphql(context, {
|
|
19
|
+
...auth,
|
|
20
|
+
query,
|
|
21
|
+
variables,
|
|
22
|
+
}))
|
|
23
|
+
: (await client.graphql({
|
|
24
|
+
...auth,
|
|
25
|
+
query,
|
|
26
|
+
variables,
|
|
27
|
+
}));
|
|
28
|
+
// flatten response
|
|
29
|
+
if (data !== undefined) {
|
|
30
|
+
const [key] = Object.keys(data);
|
|
31
|
+
if (data[key].items) {
|
|
32
|
+
const flattenedResult = flattenItems(data)[key];
|
|
33
|
+
// don't init if custom selection set
|
|
34
|
+
if (args?.selectionSet) {
|
|
35
|
+
return {
|
|
36
|
+
data: flattenedResult,
|
|
37
|
+
nextToken: data[key].nextToken,
|
|
38
|
+
extensions,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
const initialized = initializeModel(client, name, flattenedResult, modelIntrospection, auth.authMode, auth.authToken, context);
|
|
43
|
+
return {
|
|
44
|
+
data: initialized,
|
|
45
|
+
nextToken: data[key].nextToken,
|
|
46
|
+
extensions,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
data: data[key],
|
|
52
|
+
nextToken: data[key].nextToken,
|
|
53
|
+
extensions,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (error.errors) {
|
|
59
|
+
// graphql errors pass through
|
|
60
|
+
return error;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
// non-graphql errors re re-thrown
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
import { findIndexByFields, resolvePKFields } from '../../utils';
|
|
5
|
+
export function observeQueryFactory(models, model) {
|
|
6
|
+
const { name } = model;
|
|
7
|
+
const observeQuery = (arg) => new Observable(subscriber => {
|
|
8
|
+
// what we'll be sending to our subscribers
|
|
9
|
+
const items = [];
|
|
10
|
+
// To enqueue subscription messages while we collect our initial
|
|
11
|
+
// result set.
|
|
12
|
+
const messageQueue = [];
|
|
13
|
+
// operation to take when message(s) arrive.
|
|
14
|
+
// this operation will be swapped out once initial "sync" is complete
|
|
15
|
+
// to immediately ingest messsages.
|
|
16
|
+
let receiveMessages = (...messages) => {
|
|
17
|
+
return messageQueue.push(...messages);
|
|
18
|
+
};
|
|
19
|
+
// start subscriptions
|
|
20
|
+
const onCreateSub = models[name].onCreate(arg).subscribe({
|
|
21
|
+
next(item) {
|
|
22
|
+
receiveMessages({ item, type: 'create' });
|
|
23
|
+
},
|
|
24
|
+
error(error) {
|
|
25
|
+
subscriber.error({ type: 'onCreate', error });
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
const onUpdateSub = models[name].onUpdate(arg).subscribe({
|
|
29
|
+
next(item) {
|
|
30
|
+
receiveMessages({ item, type: 'update' });
|
|
31
|
+
},
|
|
32
|
+
error(error) {
|
|
33
|
+
subscriber.error({ type: 'onUpdate', error });
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
const onDeleteSub = models[name].onDelete(arg).subscribe({
|
|
37
|
+
next(item) {
|
|
38
|
+
receiveMessages({ item, type: 'delete' });
|
|
39
|
+
},
|
|
40
|
+
error(error) {
|
|
41
|
+
subscriber.error({ type: 'onDelete', error });
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
// consumes a list of messages and sends a snapshot
|
|
45
|
+
function ingestMessages(messages) {
|
|
46
|
+
for (const message of messages) {
|
|
47
|
+
const idx = findIndexByFields(message.item, items, pkFields);
|
|
48
|
+
switch (message.type) {
|
|
49
|
+
case 'create':
|
|
50
|
+
if (idx < 0)
|
|
51
|
+
items.push(message.item);
|
|
52
|
+
break;
|
|
53
|
+
case 'update':
|
|
54
|
+
if (idx >= 0)
|
|
55
|
+
items[idx] = message.item;
|
|
56
|
+
break;
|
|
57
|
+
case 'delete':
|
|
58
|
+
if (idx >= 0)
|
|
59
|
+
items.splice(idx, 1);
|
|
60
|
+
break;
|
|
61
|
+
default:
|
|
62
|
+
console.error('Unrecognized message in observeQuery.', message);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
subscriber.next({
|
|
66
|
+
items,
|
|
67
|
+
isSynced: true,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
const pkFields = resolvePKFields(model);
|
|
71
|
+
// initial results
|
|
72
|
+
(async () => {
|
|
73
|
+
let firstPage = true;
|
|
74
|
+
let nextToken = null;
|
|
75
|
+
while (!subscriber.closed && (firstPage || nextToken)) {
|
|
76
|
+
firstPage = false;
|
|
77
|
+
const { data: page, errors, nextToken: _nextToken, } = await models[name].list({ ...arg, nextToken });
|
|
78
|
+
nextToken = _nextToken;
|
|
79
|
+
items.push(...page);
|
|
80
|
+
// if there are no more pages and no items we already know about
|
|
81
|
+
// that need to be merged in from sub, we're "synced"
|
|
82
|
+
const isSynced = messageQueue.length === 0 &&
|
|
83
|
+
(nextToken === null || nextToken === undefined);
|
|
84
|
+
subscriber.next({
|
|
85
|
+
items,
|
|
86
|
+
isSynced,
|
|
87
|
+
});
|
|
88
|
+
if (Array.isArray(errors)) {
|
|
89
|
+
for (const error of errors) {
|
|
90
|
+
subscriber.error(error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// play through the queue
|
|
95
|
+
if (messageQueue.length > 0) {
|
|
96
|
+
ingestMessages(messageQueue);
|
|
97
|
+
}
|
|
98
|
+
// switch the queue to write directly to the items collection
|
|
99
|
+
receiveMessages = (...messages) => {
|
|
100
|
+
ingestMessages(messages);
|
|
101
|
+
return items.length;
|
|
102
|
+
};
|
|
103
|
+
})();
|
|
104
|
+
// when subscriber unsubscribes, tear down internal subs
|
|
105
|
+
return () => {
|
|
106
|
+
// 1. tear down internal subs
|
|
107
|
+
onCreateSub.unsubscribe();
|
|
108
|
+
onUpdateSub.unsubscribe();
|
|
109
|
+
onDeleteSub.unsubscribe();
|
|
110
|
+
// 2. there is no need to explicitly stop paging. instead, we
|
|
111
|
+
// just check `subscriber.closed` above before fetching each page.
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
return observeQuery;
|
|
115
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function subscriptionFactory(client: any, modelIntrospection: any, model: any, operation: any): (args?: any) => import("rxjs").Observable<any>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { map } from 'rxjs';
|
|
4
|
+
import { initializeModel, generateGraphQLDocument, buildGraphQLVariables, authModeParams, } from '../APIClient';
|
|
5
|
+
export function subscriptionFactory(client, modelIntrospection, model, operation) {
|
|
6
|
+
const { name } = model;
|
|
7
|
+
const subscription = (args) => {
|
|
8
|
+
const query = generateGraphQLDocument(modelIntrospection.models, name, operation);
|
|
9
|
+
const variables = buildGraphQLVariables(model, operation, args, modelIntrospection);
|
|
10
|
+
const auth = authModeParams(client, args);
|
|
11
|
+
const observable = client.graphql({
|
|
12
|
+
...auth,
|
|
13
|
+
query,
|
|
14
|
+
variables,
|
|
15
|
+
});
|
|
16
|
+
return observable.pipe(map(value => {
|
|
17
|
+
const [key] = Object.keys(value.data);
|
|
18
|
+
const [initialized] = initializeModel(client, name, [value.data[key]], modelIntrospection, auth.authMode, auth.authToken);
|
|
19
|
+
return initialized;
|
|
20
|
+
}));
|
|
21
|
+
};
|
|
22
|
+
return subscription;
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { V6ClientSSRRequest, V6ClientSSRCookies, ServerClientGenerationParams, CommonPublicClientOptions } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* @private
|
|
4
|
+
*
|
|
5
|
+
* Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`
|
|
6
|
+
* compatible context object for config and auth fetching.
|
|
7
|
+
*
|
|
8
|
+
* @param params
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateClient<T extends Record<any, any> = never, ClientType extends V6ClientSSRRequest<T> | V6ClientSSRCookies<T> = V6ClientSSRCookies<T>>(params: ServerClientGenerationParams & CommonPublicClientOptions): ClientType;
|