@aws-amplify/api-graphql 4.0.1-api-v6-models.9493d4d.0 → 4.0.1-api-v6-models.c1977f8.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.
@@ -1,3 +1,4 @@
1
+ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
1
2
  type ListArgs = {
2
3
  selectionSet?: string[];
3
4
  filter?: {};
@@ -8,7 +9,7 @@ type ListArgs = {
8
9
  * @returns response object with `items` properties flattened
9
10
  */
10
11
  export declare const flattenItems: (obj: Record<string, any>) => Record<string, any>;
11
- export declare function initializeModel(client: any, modelName: string, result: any[], modelIntrospection: any): any[];
12
+ export declare function initializeModel(client: any, modelName: string, result: any[], modelIntrospection: any, authMode: GraphQLAuthMode, authToken: string | undefined): any[];
12
13
  export declare const graphQLOperationsInfo: {
13
14
  CREATE: {
14
15
  operationPrefix: "create";
@@ -42,6 +43,10 @@ export declare const graphQLOperationsInfo: {
42
43
  operationPrefix: "onDelete";
43
44
  usePlural: boolean;
44
45
  };
46
+ OBSERVE_QUERY: {
47
+ operationPrefix: "observeQuery";
48
+ usePlural: boolean;
49
+ };
45
50
  };
46
51
  export type ModelOperation = keyof typeof graphQLOperationsInfo;
47
52
  /**
@@ -35,7 +35,7 @@ var flattenItems = function (obj) {
35
35
  };
36
36
  exports.flattenItems = flattenItems;
37
37
  // TODO: this should accept single result to support CRUD methods; create helper for array/list
38
- function initializeModel(client, modelName, result, modelIntrospection) {
38
+ function initializeModel(client, modelName, result, modelIntrospection, authMode, authToken) {
39
39
  var introModel = modelIntrospection.models[modelName];
40
40
  var introModelFields = introModel.fields;
41
41
  var modelFields = Object.entries(introModelFields)
@@ -68,10 +68,13 @@ function initializeModel(client, modelName, result, modelIntrospection) {
68
68
  return (acc[curVal] = record[curVal]);
69
69
  }
70
70
  }, {});
71
- initializedRelationalFields[field] = function () {
71
+ initializedRelationalFields[field] = function (options) {
72
72
  var _a;
73
73
  if (record[targetNames[0]]) {
74
- return client.models[relatedModelName].get(tslib_1.__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1));
74
+ return client.models[relatedModelName].get(tslib_1.__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1), {
75
+ authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
76
+ authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
77
+ });
75
78
  }
76
79
  return undefined;
77
80
  };
@@ -89,10 +92,12 @@ function initializeModel(client, modelName, result, modelIntrospection) {
89
92
  }
90
93
  return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
91
94
  });
92
- initializedRelationalFields[field] = function () {
95
+ initializedRelationalFields[field] = function (options) {
93
96
  if (record[parentPk_1]) {
94
97
  return client.models[relatedModelName].list({
95
98
  filter: { and: hasManyFilter_1 },
99
+ authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
100
+ authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
96
101
  });
97
102
  }
98
103
  return [];
@@ -106,10 +111,12 @@ function initializeModel(client, modelName, result, modelIntrospection) {
106
111
  }
107
112
  return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
108
113
  });
109
- initializedRelationalFields[field] = function () {
114
+ initializedRelationalFields[field] = function (options) {
110
115
  if (record[parentPk_1]) {
111
116
  return client.models[relatedModelName].list({
112
117
  filter: { and: hasManyFilter_2 },
118
+ authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
119
+ authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
113
120
  });
114
121
  }
115
122
  return [];
@@ -145,6 +152,7 @@ exports.graphQLOperationsInfo = {
145
152
  ONCREATE: { operationPrefix: 'onCreate', usePlural: false },
146
153
  ONUPDATE: { operationPrefix: 'onUpdate', usePlural: false },
147
154
  ONDELETE: { operationPrefix: 'onDelete', usePlural: false },
155
+ OBSERVE_QUERY: { operationPrefix: 'observeQuery', usePlural: false },
148
156
  };
149
157
  var graphQLDocumentsCache = new Map();
150
158
  var SELECTION_SET_WILDCARD = '*';
@@ -317,9 +325,11 @@ function generateGraphQLDocument(modelIntrospection, modelName, modelOperation,
317
325
  case 'LIST':
318
326
  graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
319
327
  filter: "Model".concat(name, "FilterInput"),
328
+ limit: 'Int',
329
+ nextToken: 'String',
320
330
  });
321
331
  graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'query');
322
- graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { ".concat(selectionSetFields, " }"));
332
+ graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { ".concat(selectionSetFields, " } nextToken __typename"));
323
333
  case 'ONCREATE':
324
334
  case 'ONUPDATE':
325
335
  case 'ONDELETE':
@@ -328,6 +338,10 @@ function generateGraphQLDocument(modelIntrospection, modelName, modelOperation,
328
338
  });
329
339
  graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'subscription');
330
340
  graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
341
+ break;
342
+ case 'OBSERVE_QUERY':
343
+ default:
344
+ throw new Error('Internal error: Attempted to generate graphql document for observeQuery. Please report this error.');
331
345
  }
332
346
  var graphQLDocument = "".concat(graphQLOperationType).concat(graphQLArguments
333
347
  ? "(".concat(Object.entries(graphQLArguments).map(function (_a) {
@@ -376,6 +390,13 @@ function buildGraphQLVariables(modelDefinition, operation, arg, modelIntrospecti
376
390
  }
377
391
  break;
378
392
  case 'LIST':
393
+ if (arg === null || arg === void 0 ? void 0 : arg.filter) {
394
+ variables.filter = arg.filter;
395
+ }
396
+ if (arg === null || arg === void 0 ? void 0 : arg.nextToken) {
397
+ variables.nextToken = arg.nextToken;
398
+ }
399
+ break;
379
400
  case 'ONCREATE':
380
401
  case 'ONUPDATE':
381
402
  case 'ONDELETE':
@@ -383,6 +404,9 @@ function buildGraphQLVariables(modelDefinition, operation, arg, modelIntrospecti
383
404
  variables = { filter: arg.filter };
384
405
  }
385
406
  break;
407
+ case 'OBSERVE_QUERY':
408
+ throw new Error('Internal error: Attempted to build variables for observeQuery. Please report this error.');
409
+ break;
386
410
  default:
387
411
  var exhaustiveCheck = operation;
388
412
  throw new Error("Unhandled operation case: ".concat(exhaustiveCheck));
@@ -307,7 +307,7 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
307
307
  variables: variables,
308
308
  appSyncGraphqlEndpoint: config === null || config === void 0 ? void 0 : config.endpoint,
309
309
  region: config === null || config === void 0 ? void 0 : config.region,
310
- authenticationType: config === null || config === void 0 ? void 0 : config.defaultAuthMode,
310
+ authenticationType: authMode || (config === null || config === void 0 ? void 0 : config.defaultAuthMode),
311
311
  apiKey: config === null || config === void 0 ? void 0 : config.apiKey,
312
312
  additionalHeaders: additionalHeaders,
313
313
  }, customUserAgentDetails);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateModelsProperty = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var APIClient_1 = require("./APIClient");
6
+ var utils_1 = require("../utils");
6
7
  var rxjs_1 = require("rxjs");
7
8
  function generateModelsProperty(client, params) {
8
9
  var e_1, _a;
@@ -22,7 +23,7 @@ function generateModelsProperty(client, params) {
22
23
  var operation = key;
23
24
  if (operation === 'LIST') {
24
25
  models[name_1][operationPrefix] = function (args) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
25
- var query, variables, _a, data, nextToken, extensions, _b, key_1, flattenedResult, initialized, error_1;
26
+ var query, variables, options, _a, data, extensions, _b, key_1, flattenedResult, initialized, error_1;
26
27
  return tslib_1.__generator(this, function (_c) {
27
28
  switch (_c.label) {
28
29
  case 0:
@@ -31,12 +32,14 @@ function generateModelsProperty(client, params) {
31
32
  _c.label = 1;
32
33
  case 1:
33
34
  _c.trys.push([1, 3, , 4]);
34
- return [4 /*yield*/, client.graphql({
35
- query: query,
36
- variables: variables,
37
- })];
35
+ options = {};
36
+ if (args === null || args === void 0 ? void 0 : args.authMode)
37
+ options.authMode = args === null || args === void 0 ? void 0 : args.authMode;
38
+ if (args === null || args === void 0 ? void 0 : args.authToken)
39
+ options.authToken = args === null || args === void 0 ? void 0 : args.authToken;
40
+ return [4 /*yield*/, client.graphql(tslib_1.__assign(tslib_1.__assign({}, options), { query: query, variables: variables }))];
38
41
  case 2:
39
- _a = (_c.sent()), data = _a.data, nextToken = _a.nextToken, extensions = _a.extensions;
42
+ _a = (_c.sent()), data = _a.data, extensions = _a.extensions;
40
43
  // flatten response
41
44
  if (data !== undefined) {
42
45
  _b = tslib_1.__read(Object.keys(data), 1), key_1 = _b[0];
@@ -46,22 +49,22 @@ function generateModelsProperty(client, params) {
46
49
  if (args === null || args === void 0 ? void 0 : args.selectionSet) {
47
50
  return [2 /*return*/, {
48
51
  data: flattenedResult,
49
- nextToken: nextToken,
52
+ nextToken: data[key_1].nextToken,
50
53
  extensions: extensions,
51
54
  }];
52
55
  }
53
56
  else {
54
- initialized = (0, APIClient_1.initializeModel)(client, name_1, flattenedResult, modelIntrospection);
57
+ initialized = (0, APIClient_1.initializeModel)(client, name_1, flattenedResult, modelIntrospection, options.authMode, options.authToken);
55
58
  return [2 /*return*/, {
56
59
  data: initialized,
57
- nextToken: nextToken,
60
+ nextToken: data[key_1].nextToken,
58
61
  extensions: extensions,
59
62
  }];
60
63
  }
61
64
  }
62
65
  return [2 /*return*/, {
63
66
  data: data[key_1],
64
- nextToken: nextToken,
67
+ nextToken: data[key_1].nextToken,
65
68
  extensions: extensions,
66
69
  }];
67
70
  }
@@ -83,19 +86,173 @@ function generateModelsProperty(client, params) {
83
86
  }); };
84
87
  }
85
88
  else if (['ONCREATE', 'ONUPDATE', 'ONDELETE'].includes(operation)) {
86
- models[name_1][operationPrefix] = function (arg, options) {
89
+ models[name_1][operationPrefix] = function (args) {
87
90
  var query = (0, APIClient_1.generateGraphQLDocument)(modelIntrospection.models, name_1, operation);
88
- var variables = (0, APIClient_1.buildGraphQLVariables)(model, operation, arg, modelIntrospection);
89
- var observable = client.graphql({
90
- query: query,
91
- variables: variables,
92
- });
91
+ var variables = (0, APIClient_1.buildGraphQLVariables)(model, operation, args, modelIntrospection);
92
+ var options = {};
93
+ if (args === null || args === void 0 ? void 0 : args.authMode)
94
+ options.authMode = args === null || args === void 0 ? void 0 : args.authMode;
95
+ if (args === null || args === void 0 ? void 0 : args.authToken)
96
+ options.authToken = args === null || args === void 0 ? void 0 : args.authToken;
97
+ var observable = client.graphql(tslib_1.__assign(tslib_1.__assign({}, options), { query: query, variables: variables }));
93
98
  return observable.pipe((0, rxjs_1.map)(function (value) {
94
99
  var _a = tslib_1.__read(Object.keys(value.data), 1), key = _a[0];
95
- return value.data[key];
100
+ var _b = tslib_1.__read((0, APIClient_1.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];
101
+ return initialized;
96
102
  }));
97
103
  };
98
104
  }
105
+ else if (operation === 'OBSERVE_QUERY') {
106
+ models[name_1][operationPrefix] = function (arg) {
107
+ return new rxjs_1.Observable(function (subscriber) {
108
+ // what we'll be sending to our subscribers
109
+ var items = [];
110
+ // To enqueue subscription messages while we collect our initial
111
+ // result set.
112
+ var messageQueue = [];
113
+ // operation to take when message(s) arrive.
114
+ // this operation will be swapped out once initial "sync" is complete
115
+ // to immediately ingest messsages.
116
+ var receiveMessages = function () {
117
+ var messages = [];
118
+ for (var _i = 0; _i < arguments.length; _i++) {
119
+ messages[_i] = arguments[_i];
120
+ }
121
+ return messageQueue.push.apply(messageQueue, tslib_1.__spreadArray([], tslib_1.__read(messages), false));
122
+ };
123
+ // start subscriptions
124
+ var onCreateSub = models[name_1].onCreate(arg).subscribe({
125
+ next: function (item) {
126
+ receiveMessages({ item: item, type: 'create' });
127
+ },
128
+ error: function (error) {
129
+ subscriber.error({ type: 'onCreate', error: error });
130
+ },
131
+ });
132
+ var onUpdateSub = models[name_1].onUpdate(arg).subscribe({
133
+ next: function (item) {
134
+ receiveMessages({ item: item, type: 'update' });
135
+ },
136
+ error: function (error) {
137
+ subscriber.error({ type: 'onUpdate', error: error });
138
+ },
139
+ });
140
+ var onDeleteSub = models[name_1].onDelete(arg).subscribe({
141
+ next: function (item) {
142
+ receiveMessages({ item: item, type: 'delete' });
143
+ },
144
+ error: function (error) {
145
+ subscriber.error({ type: 'onDelete', error: error });
146
+ },
147
+ });
148
+ // consumes a list of messages and sends a snapshot
149
+ function ingestMessages(messages) {
150
+ var e_2, _a;
151
+ try {
152
+ for (var messages_1 = tslib_1.__values(messages), messages_1_1 = messages_1.next(); !messages_1_1.done; messages_1_1 = messages_1.next()) {
153
+ var message = messages_1_1.value;
154
+ var idx = (0, utils_1.findIndexByFields)(message.item, items, pkFields);
155
+ switch (message.type) {
156
+ case 'create':
157
+ if (idx < 0)
158
+ items.push(message.item);
159
+ break;
160
+ case 'update':
161
+ if (idx >= 0)
162
+ items[idx] = message.item;
163
+ break;
164
+ case 'delete':
165
+ if (idx >= 0)
166
+ items.splice(idx, 1);
167
+ break;
168
+ default:
169
+ console.error('Unrecognized message in observeQuery.', message);
170
+ }
171
+ }
172
+ }
173
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
174
+ finally {
175
+ try {
176
+ if (messages_1_1 && !messages_1_1.done && (_a = messages_1.return)) _a.call(messages_1);
177
+ }
178
+ finally { if (e_2) throw e_2.error; }
179
+ }
180
+ subscriber.next({
181
+ items: items,
182
+ isSynced: true,
183
+ });
184
+ }
185
+ var pkFields = (0, utils_1.resolvePKFields)(model);
186
+ // initial results
187
+ (function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
188
+ var firstPage, nextToken, _a, page, errors, _nextToken, isSynced, errors_1, errors_1_1, error;
189
+ var e_3, _b;
190
+ return tslib_1.__generator(this, function (_c) {
191
+ switch (_c.label) {
192
+ case 0:
193
+ firstPage = true;
194
+ nextToken = null;
195
+ _c.label = 1;
196
+ case 1:
197
+ if (!(!subscriber.closed && (firstPage || nextToken))) return [3 /*break*/, 3];
198
+ firstPage = false;
199
+ return [4 /*yield*/, models[name_1].list(tslib_1.__assign(tslib_1.__assign({}, arg), { nextToken: nextToken }))];
200
+ case 2:
201
+ _a = _c.sent(), page = _a.data, errors = _a.errors, _nextToken = _a.nextToken;
202
+ nextToken = _nextToken;
203
+ items.push.apply(items, tslib_1.__spreadArray([], tslib_1.__read(page), false));
204
+ isSynced = messageQueue.length === 0 &&
205
+ (nextToken === null || nextToken === undefined);
206
+ subscriber.next({
207
+ items: items,
208
+ isSynced: isSynced,
209
+ });
210
+ if (Array.isArray(errors)) {
211
+ try {
212
+ for (errors_1 = (e_3 = void 0, tslib_1.__values(errors)), errors_1_1 = errors_1.next(); !errors_1_1.done; errors_1_1 = errors_1.next()) {
213
+ error = errors_1_1.value;
214
+ subscriber.error(error);
215
+ }
216
+ }
217
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
218
+ finally {
219
+ try {
220
+ if (errors_1_1 && !errors_1_1.done && (_b = errors_1.return)) _b.call(errors_1);
221
+ }
222
+ finally { if (e_3) throw e_3.error; }
223
+ }
224
+ }
225
+ return [3 /*break*/, 1];
226
+ case 3:
227
+ // play through the queue
228
+ if (messageQueue.length > 0) {
229
+ ingestMessages(messageQueue);
230
+ }
231
+ // switch the queue to write directly to the items collection
232
+ receiveMessages = function () {
233
+ var messages = [];
234
+ for (var _i = 0; _i < arguments.length; _i++) {
235
+ messages[_i] = arguments[_i];
236
+ }
237
+ ingestMessages(messages);
238
+ return items.length;
239
+ };
240
+ return [2 /*return*/];
241
+ }
242
+ });
243
+ }); })();
244
+ // when subscriber unsubscribes, tear down internal subs
245
+ return function () {
246
+ // 1. tear down internal subs
247
+ onCreateSub.unsubscribe();
248
+ onUpdateSub.unsubscribe();
249
+ onDeleteSub.unsubscribe();
250
+ // 2. there is no need to explicitly stop paging. instead, we
251
+ // just check `subscriber.closed` above before fetching each page.
252
+ };
253
+ });
254
+ };
255
+ }
99
256
  else {
100
257
  models[name_1][operationPrefix] = function (arg, options) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
101
258
  var query, variables, _a, data, extensions, _b, key_2, flattenedResult, _c, initialized, error_2;
@@ -107,10 +264,7 @@ function generateModelsProperty(client, params) {
107
264
  _d.label = 1;
108
265
  case 1:
109
266
  _d.trys.push([1, 3, , 4]);
110
- return [4 /*yield*/, client.graphql({
111
- query: query,
112
- variables: variables,
113
- })];
267
+ return [4 /*yield*/, client.graphql(tslib_1.__assign(tslib_1.__assign({}, options), { query: query, variables: variables }))];
114
268
  case 2:
115
269
  _a = (_d.sent()), data = _a.data, extensions = _a.extensions;
116
270
  // flatten response
@@ -121,7 +275,7 @@ function generateModelsProperty(client, params) {
121
275
  return [2 /*return*/, { data: flattenedResult, extensions: extensions }];
122
276
  }
123
277
  else {
124
- _c = tslib_1.__read((0, APIClient_1.initializeModel)(client, name_1, [flattenedResult], modelIntrospection), 1), initialized = _c[0];
278
+ _c = tslib_1.__read((0, APIClient_1.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];
125
279
  return [2 /*return*/, { data: initialized, extensions: extensions }];
126
280
  }
127
281
  }