@aws-amplify/api-graphql 4.0.1-api-v6-models.572a7b5.0 → 4.0.1-api-v6-models.9493d4d.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 +12 -0
- package/lib/internals/APIClient.js +66 -60
- package/lib/internals/generateModelsProperty.js +15 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/utils/resolveOwnerFields.d.ts +12 -0
- package/lib/utils/resolveOwnerFields.js +63 -0
- package/lib-esm/internals/APIClient.d.ts +12 -0
- package/lib-esm/internals/APIClient.js +66 -60
- package/lib-esm/internals/generateModelsProperty.js +15 -0
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/utils/resolveOwnerFields.d.ts +12 -0
- package/lib-esm/utils/resolveOwnerFields.js +59 -0
- package/package.json +5 -5
- package/src/internals/APIClient.ts +73 -56
- package/src/internals/generateModelsProperty.ts +28 -1
- package/src/utils/resolveOwnerFields.ts +65 -0
|
@@ -0,0 +1,12 @@
|
|
|
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 Model = ModelIntrospectionSchema['models'][string];
|
|
5
|
+
/**
|
|
6
|
+
* Given an introspection schema model, returns all owner fields.
|
|
7
|
+
*
|
|
8
|
+
* @param model Model from an introspection schema
|
|
9
|
+
* @returns List of owner field names
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolveOwnerFields(model: Model): string[];
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveOwnerFields = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
/**
|
|
6
|
+
* Given an introspection schema model, returns all owner fields.
|
|
7
|
+
*
|
|
8
|
+
* @param model Model from an introspection schema
|
|
9
|
+
* @returns List of owner field names
|
|
10
|
+
*/
|
|
11
|
+
function resolveOwnerFields(model) {
|
|
12
|
+
var e_1, _a, e_2, _b;
|
|
13
|
+
var ownerFields = new Set();
|
|
14
|
+
try {
|
|
15
|
+
for (var _c = tslib_1.__values(model.attributes || []), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
16
|
+
var attr = _d.value;
|
|
17
|
+
if (isAuthAttribute(attr)) {
|
|
18
|
+
try {
|
|
19
|
+
for (var _e = (e_2 = void 0, tslib_1.__values(attr.properties.rules)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
20
|
+
var rule = _f.value;
|
|
21
|
+
if (rule.allow === 'owner') {
|
|
22
|
+
ownerFields.add(rule.ownerField || 'owner');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
27
|
+
finally {
|
|
28
|
+
try {
|
|
29
|
+
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
30
|
+
}
|
|
31
|
+
finally { if (e_2) throw e_2.error; }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
37
|
+
finally {
|
|
38
|
+
try {
|
|
39
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
40
|
+
}
|
|
41
|
+
finally { if (e_1) throw e_1.error; }
|
|
42
|
+
}
|
|
43
|
+
return Array.from(ownerFields);
|
|
44
|
+
}
|
|
45
|
+
exports.resolveOwnerFields = resolveOwnerFields;
|
|
46
|
+
/**
|
|
47
|
+
* Type guard that identifies an auth attribute with an attached rules list that
|
|
48
|
+
* specifies an `allow` attribute at a minimum.
|
|
49
|
+
*
|
|
50
|
+
* @param attribute Any object. Ideally a model introspection schema model attribute
|
|
51
|
+
* @returns True if given object is an auth attribute
|
|
52
|
+
*/
|
|
53
|
+
function isAuthAttribute(attribute) {
|
|
54
|
+
var _a, _b;
|
|
55
|
+
if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'auth') {
|
|
56
|
+
if (typeof (attribute === null || attribute === void 0 ? void 0 : attribute.properties) === 'object') {
|
|
57
|
+
if (Array.isArray((_a = attribute === null || attribute === void 0 ? void 0 : attribute.properties) === null || _a === void 0 ? void 0 : _a.rules)) {
|
|
58
|
+
return ((_b = attribute === null || attribute === void 0 ? void 0 : attribute.properties) === null || _b === void 0 ? void 0 : _b.rules).every(function (rule) { return !!rule.allow; });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
@@ -30,6 +30,18 @@ export declare const graphQLOperationsInfo: {
|
|
|
30
30
|
operationPrefix: "list";
|
|
31
31
|
usePlural: boolean;
|
|
32
32
|
};
|
|
33
|
+
ONCREATE: {
|
|
34
|
+
operationPrefix: "onCreate";
|
|
35
|
+
usePlural: boolean;
|
|
36
|
+
};
|
|
37
|
+
ONUPDATE: {
|
|
38
|
+
operationPrefix: "onUpdate";
|
|
39
|
+
usePlural: boolean;
|
|
40
|
+
};
|
|
41
|
+
ONDELETE: {
|
|
42
|
+
operationPrefix: "onDelete";
|
|
43
|
+
usePlural: boolean;
|
|
44
|
+
};
|
|
33
45
|
};
|
|
34
46
|
export type ModelOperation = keyof typeof graphQLOperationsInfo;
|
|
35
47
|
/**
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { __assign, __read, __spreadArray, __values } from "tslib";
|
|
2
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { resolveOwnerFields } from '../utils/resolveOwnerFields';
|
|
2
5
|
var connectionType = {
|
|
3
6
|
HAS_ONE: 'HAS_ONE',
|
|
4
7
|
HAS_MANY: 'HAS_MANY',
|
|
@@ -134,17 +137,26 @@ export var graphQLOperationsInfo = {
|
|
|
134
137
|
UPDATE: { operationPrefix: 'update', usePlural: false },
|
|
135
138
|
DELETE: { operationPrefix: 'delete', usePlural: false },
|
|
136
139
|
LIST: { operationPrefix: 'list', usePlural: true },
|
|
140
|
+
ONCREATE: { operationPrefix: 'onCreate', usePlural: false },
|
|
141
|
+
ONUPDATE: { operationPrefix: 'onUpdate', usePlural: false },
|
|
142
|
+
ONDELETE: { operationPrefix: 'onDelete', usePlural: false },
|
|
137
143
|
};
|
|
138
144
|
var graphQLDocumentsCache = new Map();
|
|
139
|
-
var
|
|
145
|
+
var SELECTION_SET_WILDCARD = '*';
|
|
140
146
|
function defaultSelectionSetForModel(modelDefinition) {
|
|
147
|
+
// fields that are explicitly part of the graphql schema; not
|
|
148
|
+
// inferred from owner auth rules.
|
|
141
149
|
var fields = modelDefinition.fields;
|
|
142
|
-
|
|
150
|
+
var explicitFields = Object.values(fields)
|
|
143
151
|
.map(function (_a) {
|
|
144
152
|
var type = _a.type, name = _a.name;
|
|
145
153
|
return typeof type === 'string' && name;
|
|
146
154
|
}) // Default selection set omits model fields
|
|
147
155
|
.filter(Boolean);
|
|
156
|
+
// fields used for owner auth rules that may or may not also
|
|
157
|
+
// be explicit on the model.
|
|
158
|
+
var ownerFields = resolveOwnerFields(modelDefinition);
|
|
159
|
+
return Array.from(new Set(explicitFields.concat(ownerFields)));
|
|
148
160
|
}
|
|
149
161
|
var FIELD_IR = '';
|
|
150
162
|
/**
|
|
@@ -167,70 +179,53 @@ var FIELD_IR = '';
|
|
|
167
179
|
* ```
|
|
168
180
|
*/
|
|
169
181
|
export function customSelectionSetToIR(modelIntrospection, modelName, selectionSet) {
|
|
170
|
-
var e_2, _a;
|
|
171
|
-
var _b, _c, _d, _e;
|
|
172
182
|
var modelDefinition = modelIntrospection[modelName];
|
|
173
|
-
var
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
var
|
|
179
|
-
if (
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (!relatedModel) {
|
|
183
|
-
// TODO: may need to change this to support custom types
|
|
184
|
-
throw Error("".concat(modelFieldName, " is not a model field"));
|
|
185
|
-
}
|
|
186
|
-
if (selectedField === SELECTION_SET_ALL_NESTED) {
|
|
187
|
-
var relatedModelDefinition = modelIntrospection[relatedModel];
|
|
188
|
-
var defaultSelectionSet = defaultSelectionSetForModel(relatedModelDefinition);
|
|
189
|
-
var reduced = defaultSelectionSet.reduce(function (acc, curVal) {
|
|
190
|
-
acc[curVal] = FIELD_IR;
|
|
191
|
-
return acc;
|
|
192
|
-
}, {});
|
|
193
|
-
if ((_d = fields[modelFieldName]) === null || _d === void 0 ? void 0 : _d.isArray) {
|
|
194
|
-
intermediateSelectionSet[modelFieldName] = {
|
|
195
|
-
items: reduced,
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
199
|
-
intermediateSelectionSet[modelFieldName] = reduced;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
var getNestedSelSet = customSelectionSetToIR(modelIntrospection, relatedModel, [selectedField]);
|
|
204
|
-
if ((_e = fields[modelFieldName]) === null || _e === void 0 ? void 0 : _e.isArray) {
|
|
205
|
-
var existing = intermediateSelectionSet[modelFieldName] || { items: {} };
|
|
206
|
-
var merged = __assign(__assign({}, existing.items), getNestedSelSet);
|
|
207
|
-
intermediateSelectionSet[modelFieldName] = { items: merged };
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
var existingItems = intermediateSelectionSet[modelFieldName] || {};
|
|
211
|
-
var merged = __assign(__assign({}, existingItems), getNestedSelSet);
|
|
212
|
-
intermediateSelectionSet[modelFieldName] = merged;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
183
|
+
var modelFields = modelDefinition.fields;
|
|
184
|
+
return selectionSet.reduce(function (resultObj, path) {
|
|
185
|
+
var _a, _b, _c;
|
|
186
|
+
var _d = __read(path.split('.')), fieldName = _d[0], nested = _d[1], rest = _d.slice(2);
|
|
187
|
+
if (nested) {
|
|
188
|
+
var relatedModel = (_b = (_a = modelFields[fieldName]) === null || _a === void 0 ? void 0 : _a.type) === null || _b === void 0 ? void 0 : _b.model;
|
|
189
|
+
if (!relatedModel) {
|
|
190
|
+
// TODO: may need to change this to support custom types
|
|
191
|
+
throw Error("".concat(fieldName, " is not a model field"));
|
|
215
192
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
193
|
+
var relatedModelDefinition = modelIntrospection[relatedModel];
|
|
194
|
+
var selectionSet_1 = nested === SELECTION_SET_WILDCARD
|
|
195
|
+
? defaultSelectionSetIR(relatedModelDefinition)
|
|
196
|
+
: // if we have a path like 'field.anotherField' recursively build up selection set IR
|
|
197
|
+
customSelectionSetToIR(modelIntrospection, relatedModel, [
|
|
198
|
+
__spreadArray([nested], __read(rest), false).join('.'),
|
|
199
|
+
]);
|
|
200
|
+
if ((_c = modelFields[fieldName]) === null || _c === void 0 ? void 0 : _c.isArray) {
|
|
201
|
+
var existing = resultObj[fieldName] || {
|
|
202
|
+
items: {},
|
|
203
|
+
};
|
|
204
|
+
var merged_1 = __assign(__assign({}, existing.items), selectionSet_1);
|
|
205
|
+
resultObj[fieldName] = { items: merged_1 };
|
|
206
|
+
return resultObj;
|
|
222
207
|
}
|
|
208
|
+
var existingItems = resultObj[fieldName] || {};
|
|
209
|
+
var merged = __assign(__assign({}, existingItems), selectionSet_1);
|
|
210
|
+
resultObj[fieldName] = merged;
|
|
211
|
+
return resultObj;
|
|
223
212
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
try {
|
|
228
|
-
if (selectionSet_1_1 && !selectionSet_1_1.done && (_a = selectionSet_1.return)) _a.call(selectionSet_1);
|
|
213
|
+
var exists = Boolean(modelFields[fieldName]);
|
|
214
|
+
if (!exists) {
|
|
215
|
+
throw Error("".concat(fieldName, " is not a field of model ").concat(modelName));
|
|
229
216
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
217
|
+
resultObj[fieldName] = FIELD_IR;
|
|
218
|
+
return resultObj;
|
|
219
|
+
}, {});
|
|
233
220
|
}
|
|
221
|
+
var defaultSelectionSetIR = function (relatedModelDefinition) {
|
|
222
|
+
var defaultSelectionSet = defaultSelectionSetForModel(relatedModelDefinition);
|
|
223
|
+
var reduced = defaultSelectionSet.reduce(function (acc, curVal) {
|
|
224
|
+
acc[curVal] = FIELD_IR;
|
|
225
|
+
return acc;
|
|
226
|
+
}, {});
|
|
227
|
+
return reduced;
|
|
228
|
+
};
|
|
234
229
|
/**
|
|
235
230
|
* Stringifies selection set IR
|
|
236
231
|
* * @example
|
|
@@ -317,6 +312,14 @@ export function generateGraphQLDocument(modelIntrospection, modelName, modelOper
|
|
|
317
312
|
});
|
|
318
313
|
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'query');
|
|
319
314
|
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { ".concat(selectionSetFields, " }"));
|
|
315
|
+
case 'ONCREATE':
|
|
316
|
+
case 'ONUPDATE':
|
|
317
|
+
case 'ONDELETE':
|
|
318
|
+
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
|
|
319
|
+
filter: "ModelSubscription".concat(name, "FilterInput"),
|
|
320
|
+
});
|
|
321
|
+
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'subscription');
|
|
322
|
+
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
|
|
320
323
|
}
|
|
321
324
|
var graphQLDocument = "".concat(graphQLOperationType).concat(graphQLArguments
|
|
322
325
|
? "(".concat(Object.entries(graphQLArguments).map(function (_a) {
|
|
@@ -364,6 +367,9 @@ export function buildGraphQLVariables(modelDefinition, operation, arg, modelIntr
|
|
|
364
367
|
}
|
|
365
368
|
break;
|
|
366
369
|
case 'LIST':
|
|
370
|
+
case 'ONCREATE':
|
|
371
|
+
case 'ONUPDATE':
|
|
372
|
+
case 'ONDELETE':
|
|
367
373
|
if (arg === null || arg === void 0 ? void 0 : arg.filter) {
|
|
368
374
|
variables = { filter: arg.filter };
|
|
369
375
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { __awaiter, __generator, __read, __values } from "tslib";
|
|
2
2
|
import { initializeModel, generateGraphQLDocument, buildGraphQLVariables, graphQLOperationsInfo, flattenItems, } from './APIClient';
|
|
3
|
+
import { map } from 'rxjs';
|
|
3
4
|
export function generateModelsProperty(client, params) {
|
|
4
5
|
var e_1, _a;
|
|
5
6
|
var _this = this;
|
|
@@ -78,6 +79,20 @@ export function generateModelsProperty(client, params) {
|
|
|
78
79
|
});
|
|
79
80
|
}); };
|
|
80
81
|
}
|
|
82
|
+
else if (['ONCREATE', 'ONUPDATE', 'ONDELETE'].includes(operation)) {
|
|
83
|
+
models[name_1][operationPrefix] = function (arg, options) {
|
|
84
|
+
var query = generateGraphQLDocument(modelIntrospection.models, name_1, operation);
|
|
85
|
+
var variables = buildGraphQLVariables(model, operation, arg, modelIntrospection);
|
|
86
|
+
var observable = client.graphql({
|
|
87
|
+
query: query,
|
|
88
|
+
variables: variables,
|
|
89
|
+
});
|
|
90
|
+
return observable.pipe(map(function (value) {
|
|
91
|
+
var _a = __read(Object.keys(value.data), 1), key = _a[0];
|
|
92
|
+
return value.data[key];
|
|
93
|
+
}));
|
|
94
|
+
};
|
|
95
|
+
}
|
|
81
96
|
else {
|
|
82
97
|
models[name_1][operationPrefix] = function (arg, options) { return __awaiter(_this, void 0, void 0, function () {
|
|
83
98
|
var query, variables, _a, data, extensions, _b, key_2, flattenedResult, _c, initialized, error_2;
|