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