@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.
- package/lib/internals/APIClient.d.ts +6 -1
- package/lib/internals/APIClient.js +30 -6
- package/lib/internals/InternalGraphQLAPI.js +1 -1
- package/lib/internals/generateModelsProperty.js +176 -22
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/utils/findIndexByFields.d.ts +9 -0
- package/lib/utils/findIndexByFields.js +28 -0
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +5 -1
- package/lib/utils/resolvePKFields.d.ts +13 -0
- package/lib/utils/resolvePKFields.js +16 -0
- package/lib-esm/internals/APIClient.d.ts +6 -1
- package/lib-esm/internals/APIClient.js +30 -6
- package/lib-esm/internals/InternalGraphQLAPI.js +1 -1
- package/lib-esm/internals/generateModelsProperty.js +178 -24
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/utils/findIndexByFields.d.ts +9 -0
- package/lib-esm/utils/findIndexByFields.js +24 -0
- package/lib-esm/utils/index.d.ts +2 -0
- package/lib-esm/utils/index.js +2 -0
- package/lib-esm/utils/resolvePKFields.d.ts +13 -0
- package/lib-esm/utils/resolvePKFields.js +12 -0
- package/package.json +5 -5
- package/src/internals/APIClient.ts +51 -10
- package/src/internals/InternalGraphQLAPI.ts +1 -1
- package/src/internals/generateModelsProperty.ts +169 -9
- package/src/utils/findIndexByFields.ts +29 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/resolvePKFields.ts +22 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Iterates through a collection to find a matching item and returns the index.
|
|
3
|
+
*
|
|
4
|
+
* @param needle The item to search for
|
|
5
|
+
* @param haystack The collection to search
|
|
6
|
+
* @param keyFields The fields used to indicate a match
|
|
7
|
+
* @returns Index of `needle` in `haystack`, otherwise -1 if not found.
|
|
8
|
+
*/
|
|
9
|
+
export declare function findIndexByFields<T>(needle: T, haystack: T[], keyFields: Array<keyof T>): number;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.findIndexByFields = void 0;
|
|
4
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
/**
|
|
7
|
+
* Iterates through a collection to find a matching item and returns the index.
|
|
8
|
+
*
|
|
9
|
+
* @param needle The item to search for
|
|
10
|
+
* @param haystack The collection to search
|
|
11
|
+
* @param keyFields The fields used to indicate a match
|
|
12
|
+
* @returns Index of `needle` in `haystack`, otherwise -1 if not found.
|
|
13
|
+
*/
|
|
14
|
+
function findIndexByFields(needle, haystack, keyFields) {
|
|
15
|
+
var searchObject = Object.fromEntries(keyFields.map(function (fieldName) { return [fieldName, needle[fieldName]]; }));
|
|
16
|
+
var _loop_1 = function (i) {
|
|
17
|
+
if (Object.keys(searchObject).every(function (k) { return searchObject[k] === haystack[i][k]; })) {
|
|
18
|
+
return { value: i };
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
for (var i = 0; i < haystack.length; i++) {
|
|
22
|
+
var state_1 = _loop_1(i);
|
|
23
|
+
if (typeof state_1 === "object")
|
|
24
|
+
return state_1.value;
|
|
25
|
+
}
|
|
26
|
+
return -1;
|
|
27
|
+
}
|
|
28
|
+
exports.findIndexByFields = findIndexByFields;
|
package/lib/utils/index.d.ts
CHANGED
package/lib/utils/index.js
CHANGED
|
@@ -2,8 +2,12 @@
|
|
|
2
2
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.resolveLibraryOptions = exports.resolveConfig = void 0;
|
|
5
|
+
exports.findIndexByFields = exports.resolvePKFields = exports.resolveLibraryOptions = exports.resolveConfig = void 0;
|
|
6
6
|
var resolveConfig_1 = require("./resolveConfig");
|
|
7
7
|
Object.defineProperty(exports, "resolveConfig", { enumerable: true, get: function () { return resolveConfig_1.resolveConfig; } });
|
|
8
8
|
var resolveLibraryOptions_1 = require("./resolveLibraryOptions");
|
|
9
9
|
Object.defineProperty(exports, "resolveLibraryOptions", { enumerable: true, get: function () { return resolveLibraryOptions_1.resolveLibraryOptions; } });
|
|
10
|
+
var resolvePKFields_1 = require("./resolvePKFields");
|
|
11
|
+
Object.defineProperty(exports, "resolvePKFields", { enumerable: true, get: function () { return resolvePKFields_1.resolvePKFields; } });
|
|
12
|
+
var findIndexByFields_1 = require("./findIndexByFields");
|
|
13
|
+
Object.defineProperty(exports, "findIndexByFields", { enumerable: true, get: function () { return findIndexByFields_1.findIndexByFields; } });
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ResourcesConfig } from '@aws-amplify/core';
|
|
2
|
+
type GraphQLConfig = Exclude<ResourcesConfig['API'], undefined>['GraphQL'];
|
|
3
|
+
type ModelIntrospectionSchema = Exclude<Exclude<GraphQLConfig, undefined>['modelIntrospection'], undefined>;
|
|
4
|
+
type SchemaModel = ModelIntrospectionSchema['models'][string];
|
|
5
|
+
/**
|
|
6
|
+
* Given a SchemaModel from a ModelIntrospectionSchema, returns the primary key
|
|
7
|
+
* as an array of field names.
|
|
8
|
+
*
|
|
9
|
+
* @param model The model object
|
|
10
|
+
* @returns Array of field names
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolvePKFields(model: SchemaModel): string[];
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolvePKFields = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
/**
|
|
6
|
+
* Given a SchemaModel from a ModelIntrospectionSchema, returns the primary key
|
|
7
|
+
* as an array of field names.
|
|
8
|
+
*
|
|
9
|
+
* @param model The model object
|
|
10
|
+
* @returns Array of field names
|
|
11
|
+
*/
|
|
12
|
+
function resolvePKFields(model) {
|
|
13
|
+
var _a = model.primaryKeyInfo, primaryKeyFieldName = _a.primaryKeyFieldName, sortKeyFieldNames = _a.sortKeyFieldNames;
|
|
14
|
+
return tslib_1.__spreadArray([primaryKeyFieldName], tslib_1.__read(sortKeyFieldNames), false);
|
|
15
|
+
}
|
|
16
|
+
exports.resolvePKFields = resolvePKFields;
|
|
@@ -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
|
/**
|
|
@@ -31,7 +31,7 @@ export var flattenItems = function (obj) {
|
|
|
31
31
|
return res;
|
|
32
32
|
};
|
|
33
33
|
// TODO: this should accept single result to support CRUD methods; create helper for array/list
|
|
34
|
-
export function initializeModel(client, modelName, result, modelIntrospection) {
|
|
34
|
+
export function initializeModel(client, modelName, result, modelIntrospection, authMode, authToken) {
|
|
35
35
|
var introModel = modelIntrospection.models[modelName];
|
|
36
36
|
var introModelFields = introModel.fields;
|
|
37
37
|
var modelFields = Object.entries(introModelFields)
|
|
@@ -64,10 +64,13 @@ export function initializeModel(client, modelName, result, modelIntrospection) {
|
|
|
64
64
|
return (acc[curVal] = record[curVal]);
|
|
65
65
|
}
|
|
66
66
|
}, {});
|
|
67
|
-
initializedRelationalFields[field] = function () {
|
|
67
|
+
initializedRelationalFields[field] = function (options) {
|
|
68
68
|
var _a;
|
|
69
69
|
if (record[targetNames[0]]) {
|
|
70
|
-
return client.models[relatedModelName].get(__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1)
|
|
70
|
+
return client.models[relatedModelName].get(__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1), {
|
|
71
|
+
authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
|
|
72
|
+
authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
|
|
73
|
+
});
|
|
71
74
|
}
|
|
72
75
|
return undefined;
|
|
73
76
|
};
|
|
@@ -85,10 +88,12 @@ export function initializeModel(client, modelName, result, modelIntrospection) {
|
|
|
85
88
|
}
|
|
86
89
|
return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
|
|
87
90
|
});
|
|
88
|
-
initializedRelationalFields[field] = function () {
|
|
91
|
+
initializedRelationalFields[field] = function (options) {
|
|
89
92
|
if (record[parentPk_1]) {
|
|
90
93
|
return client.models[relatedModelName].list({
|
|
91
94
|
filter: { and: hasManyFilter_1 },
|
|
95
|
+
authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
|
|
96
|
+
authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
|
|
92
97
|
});
|
|
93
98
|
}
|
|
94
99
|
return [];
|
|
@@ -102,10 +107,12 @@ export function initializeModel(client, modelName, result, modelIntrospection) {
|
|
|
102
107
|
}
|
|
103
108
|
return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
|
|
104
109
|
});
|
|
105
|
-
initializedRelationalFields[field] = function () {
|
|
110
|
+
initializedRelationalFields[field] = function (options) {
|
|
106
111
|
if (record[parentPk_1]) {
|
|
107
112
|
return client.models[relatedModelName].list({
|
|
108
113
|
filter: { and: hasManyFilter_2 },
|
|
114
|
+
authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
|
|
115
|
+
authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
|
|
109
116
|
});
|
|
110
117
|
}
|
|
111
118
|
return [];
|
|
@@ -140,6 +147,7 @@ export var graphQLOperationsInfo = {
|
|
|
140
147
|
ONCREATE: { operationPrefix: 'onCreate', usePlural: false },
|
|
141
148
|
ONUPDATE: { operationPrefix: 'onUpdate', usePlural: false },
|
|
142
149
|
ONDELETE: { operationPrefix: 'onDelete', usePlural: false },
|
|
150
|
+
OBSERVE_QUERY: { operationPrefix: 'observeQuery', usePlural: false },
|
|
143
151
|
};
|
|
144
152
|
var graphQLDocumentsCache = new Map();
|
|
145
153
|
var SELECTION_SET_WILDCARD = '*';
|
|
@@ -309,9 +317,11 @@ export function generateGraphQLDocument(modelIntrospection, modelName, modelOper
|
|
|
309
317
|
case 'LIST':
|
|
310
318
|
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
|
|
311
319
|
filter: "Model".concat(name, "FilterInput"),
|
|
320
|
+
limit: 'Int',
|
|
321
|
+
nextToken: 'String',
|
|
312
322
|
});
|
|
313
323
|
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'query');
|
|
314
|
-
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { ".concat(selectionSetFields, " }"));
|
|
324
|
+
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { ".concat(selectionSetFields, " } nextToken __typename"));
|
|
315
325
|
case 'ONCREATE':
|
|
316
326
|
case 'ONUPDATE':
|
|
317
327
|
case 'ONDELETE':
|
|
@@ -320,6 +330,10 @@ export function generateGraphQLDocument(modelIntrospection, modelName, modelOper
|
|
|
320
330
|
});
|
|
321
331
|
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'subscription');
|
|
322
332
|
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
|
|
333
|
+
break;
|
|
334
|
+
case 'OBSERVE_QUERY':
|
|
335
|
+
default:
|
|
336
|
+
throw new Error('Internal error: Attempted to generate graphql document for observeQuery. Please report this error.');
|
|
323
337
|
}
|
|
324
338
|
var graphQLDocument = "".concat(graphQLOperationType).concat(graphQLArguments
|
|
325
339
|
? "(".concat(Object.entries(graphQLArguments).map(function (_a) {
|
|
@@ -367,6 +381,13 @@ export function buildGraphQLVariables(modelDefinition, operation, arg, modelIntr
|
|
|
367
381
|
}
|
|
368
382
|
break;
|
|
369
383
|
case 'LIST':
|
|
384
|
+
if (arg === null || arg === void 0 ? void 0 : arg.filter) {
|
|
385
|
+
variables.filter = arg.filter;
|
|
386
|
+
}
|
|
387
|
+
if (arg === null || arg === void 0 ? void 0 : arg.nextToken) {
|
|
388
|
+
variables.nextToken = arg.nextToken;
|
|
389
|
+
}
|
|
390
|
+
break;
|
|
370
391
|
case 'ONCREATE':
|
|
371
392
|
case 'ONUPDATE':
|
|
372
393
|
case 'ONDELETE':
|
|
@@ -374,6 +395,9 @@ export function buildGraphQLVariables(modelDefinition, operation, arg, modelIntr
|
|
|
374
395
|
variables = { filter: arg.filter };
|
|
375
396
|
}
|
|
376
397
|
break;
|
|
398
|
+
case 'OBSERVE_QUERY':
|
|
399
|
+
throw new Error('Internal error: Attempted to build variables for observeQuery. Please report this error.');
|
|
400
|
+
break;
|
|
377
401
|
default:
|
|
378
402
|
var exhaustiveCheck = operation;
|
|
379
403
|
throw new Error("Unhandled operation case: ".concat(exhaustiveCheck));
|
|
@@ -303,7 +303,7 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
|
|
|
303
303
|
variables: variables,
|
|
304
304
|
appSyncGraphqlEndpoint: config === null || config === void 0 ? void 0 : config.endpoint,
|
|
305
305
|
region: config === null || config === void 0 ? void 0 : config.region,
|
|
306
|
-
authenticationType: config === null || config === void 0 ? void 0 : config.defaultAuthMode,
|
|
306
|
+
authenticationType: authMode || (config === null || config === void 0 ? void 0 : config.defaultAuthMode),
|
|
307
307
|
apiKey: config === null || config === void 0 ? void 0 : config.apiKey,
|
|
308
308
|
additionalHeaders: additionalHeaders,
|
|
309
309
|
}, customUserAgentDetails);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { __awaiter, __generator, __read, __values } from "tslib";
|
|
1
|
+
import { __assign, __awaiter, __generator, __read, __spreadArray, __values } from "tslib";
|
|
2
2
|
import { initializeModel, generateGraphQLDocument, buildGraphQLVariables, graphQLOperationsInfo, flattenItems, } from './APIClient';
|
|
3
|
-
import {
|
|
3
|
+
import { findIndexByFields, resolvePKFields } from '../utils';
|
|
4
|
+
import { Observable, map } from 'rxjs';
|
|
4
5
|
export function generateModelsProperty(client, params) {
|
|
5
6
|
var e_1, _a;
|
|
6
7
|
var _this = this;
|
|
@@ -19,7 +20,7 @@ export function generateModelsProperty(client, params) {
|
|
|
19
20
|
var operation = key;
|
|
20
21
|
if (operation === 'LIST') {
|
|
21
22
|
models[name_1][operationPrefix] = function (args) { return __awaiter(_this, void 0, void 0, function () {
|
|
22
|
-
var query, variables, _a, data,
|
|
23
|
+
var query, variables, options, _a, data, extensions, _b, key_1, flattenedResult, initialized, error_1;
|
|
23
24
|
return __generator(this, function (_c) {
|
|
24
25
|
switch (_c.label) {
|
|
25
26
|
case 0:
|
|
@@ -28,12 +29,14 @@ export function generateModelsProperty(client, params) {
|
|
|
28
29
|
_c.label = 1;
|
|
29
30
|
case 1:
|
|
30
31
|
_c.trys.push([1, 3, , 4]);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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 }))];
|
|
35
38
|
case 2:
|
|
36
|
-
_a = (_c.sent()), data = _a.data,
|
|
39
|
+
_a = (_c.sent()), data = _a.data, extensions = _a.extensions;
|
|
37
40
|
// flatten response
|
|
38
41
|
if (data !== undefined) {
|
|
39
42
|
_b = __read(Object.keys(data), 1), key_1 = _b[0];
|
|
@@ -43,22 +46,22 @@ export function generateModelsProperty(client, params) {
|
|
|
43
46
|
if (args === null || args === void 0 ? void 0 : args.selectionSet) {
|
|
44
47
|
return [2 /*return*/, {
|
|
45
48
|
data: flattenedResult,
|
|
46
|
-
nextToken: nextToken,
|
|
49
|
+
nextToken: data[key_1].nextToken,
|
|
47
50
|
extensions: extensions,
|
|
48
51
|
}];
|
|
49
52
|
}
|
|
50
53
|
else {
|
|
51
|
-
initialized = initializeModel(client, name_1, flattenedResult, modelIntrospection);
|
|
54
|
+
initialized = initializeModel(client, name_1, flattenedResult, modelIntrospection, options.authMode, options.authToken);
|
|
52
55
|
return [2 /*return*/, {
|
|
53
56
|
data: initialized,
|
|
54
|
-
nextToken: nextToken,
|
|
57
|
+
nextToken: data[key_1].nextToken,
|
|
55
58
|
extensions: extensions,
|
|
56
59
|
}];
|
|
57
60
|
}
|
|
58
61
|
}
|
|
59
62
|
return [2 /*return*/, {
|
|
60
63
|
data: data[key_1],
|
|
61
|
-
nextToken: nextToken,
|
|
64
|
+
nextToken: data[key_1].nextToken,
|
|
62
65
|
extensions: extensions,
|
|
63
66
|
}];
|
|
64
67
|
}
|
|
@@ -80,19 +83,173 @@ export function generateModelsProperty(client, params) {
|
|
|
80
83
|
}); };
|
|
81
84
|
}
|
|
82
85
|
else if (['ONCREATE', 'ONUPDATE', 'ONDELETE'].includes(operation)) {
|
|
83
|
-
models[name_1][operationPrefix] = function (
|
|
86
|
+
models[name_1][operationPrefix] = function (args) {
|
|
84
87
|
var query = generateGraphQLDocument(modelIntrospection.models, name_1, operation);
|
|
85
|
-
var variables = buildGraphQLVariables(model, operation,
|
|
86
|
-
var
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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 }));
|
|
90
95
|
return observable.pipe(map(function (value) {
|
|
91
96
|
var _a = __read(Object.keys(value.data), 1), key = _a[0];
|
|
92
|
-
|
|
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;
|
|
93
99
|
}));
|
|
94
100
|
};
|
|
95
101
|
}
|
|
102
|
+
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
|
+
};
|
|
252
|
+
}
|
|
96
253
|
else {
|
|
97
254
|
models[name_1][operationPrefix] = function (arg, options) { return __awaiter(_this, void 0, void 0, function () {
|
|
98
255
|
var query, variables, _a, data, extensions, _b, key_2, flattenedResult, _c, initialized, error_2;
|
|
@@ -104,10 +261,7 @@ export function generateModelsProperty(client, params) {
|
|
|
104
261
|
_d.label = 1;
|
|
105
262
|
case 1:
|
|
106
263
|
_d.trys.push([1, 3, , 4]);
|
|
107
|
-
return [4 /*yield*/, client.graphql({
|
|
108
|
-
query: query,
|
|
109
|
-
variables: variables,
|
|
110
|
-
})];
|
|
264
|
+
return [4 /*yield*/, client.graphql(__assign(__assign({}, options), { query: query, variables: variables }))];
|
|
111
265
|
case 2:
|
|
112
266
|
_a = (_d.sent()), data = _a.data, extensions = _a.extensions;
|
|
113
267
|
// flatten response
|
|
@@ -118,7 +272,7 @@ export function generateModelsProperty(client, params) {
|
|
|
118
272
|
return [2 /*return*/, { data: flattenedResult, extensions: extensions }];
|
|
119
273
|
}
|
|
120
274
|
else {
|
|
121
|
-
_c = __read(initializeModel(client, name_1, [flattenedResult], modelIntrospection), 1), initialized = _c[0];
|
|
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];
|
|
122
276
|
return [2 /*return*/, { data: initialized, extensions: extensions }];
|
|
123
277
|
}
|
|
124
278
|
}
|