@aws-amplify/api-graphql 4.0.1-server-generate-client.1e317a1.0 → 4.0.1-ssr-api.df60e41.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/GraphQLAPI.d.ts +1 -1
- package/lib/GraphQLAPI.js +25 -30
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +1 -1
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +468 -584
- package/lib/Providers/constants.js +4 -5
- package/lib/index.js +5 -6
- package/lib/internals/InternalGraphQLAPI.d.ts +1 -1
- package/lib/internals/InternalGraphQLAPI.js +203 -263
- package/lib/internals/index.d.ts +0 -1
- package/lib/internals/index.js +5 -8
- package/lib/internals/v6.js +3 -4
- package/lib/tsconfig.tsbuildinfo +6798 -1
- package/lib/types/PubSub.d.ts +2 -2
- package/lib/types/PubSub.js +2 -3
- package/lib/types/index.d.ts +39 -28
- package/lib/types/index.js +3 -4
- package/lib/utils/ConnectionStateMonitor.d.ts +3 -3
- package/lib/utils/ConnectionStateMonitor.js +51 -65
- package/lib/utils/ReachabilityMonitor/index.d.ts +1 -1
- package/lib/utils/ReachabilityMonitor/index.js +2 -4
- package/lib/utils/ReachabilityMonitor/index.native.d.ts +1 -1
- package/lib/utils/ReachabilityMonitor/index.native.js +4 -8
- package/lib/utils/ReconnectionMonitor.js +21 -26
- package/lib/utils/errors/APIError.js +7 -12
- package/lib/utils/errors/assertValidationError.js +4 -5
- package/lib/utils/errors/index.js +4 -5
- package/lib/utils/errors/validation.js +6 -8
- package/lib/utils/index.js +2 -3
- package/lib/utils/resolveConfig.d.ts +1 -1
- package/lib/utils/resolveConfig.js +10 -13
- package/lib/utils/resolveLibraryOptions.d.ts +1 -1
- package/lib/utils/resolveLibraryOptions.js +4 -7
- package/lib-esm/GraphQLAPI.d.ts +1 -1
- package/lib-esm/GraphQLAPI.js +25 -29
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +1 -1
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +459 -575
- package/lib-esm/Providers/constants.js +10 -10
- package/lib-esm/internals/InternalGraphQLAPI.d.ts +1 -1
- package/lib-esm/internals/InternalGraphQLAPI.js +197 -256
- package/lib-esm/internals/index.d.ts +0 -1
- package/lib-esm/internals/index.js +0 -1
- package/lib-esm/internals/v6.js +1 -1
- package/lib-esm/tsconfig.tsbuildinfo +6798 -1
- package/lib-esm/types/PubSub.d.ts +2 -2
- package/lib-esm/types/index.d.ts +39 -28
- package/lib-esm/types/index.js +1 -1
- package/lib-esm/utils/ConnectionStateMonitor.d.ts +3 -3
- package/lib-esm/utils/ConnectionStateMonitor.js +49 -63
- package/lib-esm/utils/ReachabilityMonitor/index.d.ts +1 -1
- package/lib-esm/utils/ReachabilityMonitor/index.js +1 -1
- package/lib-esm/utils/ReachabilityMonitor/index.native.d.ts +1 -1
- package/lib-esm/utils/ReachabilityMonitor/index.native.js +1 -3
- package/lib-esm/utils/ReconnectionMonitor.js +19 -24
- package/lib-esm/utils/errors/APIError.js +6 -11
- package/lib-esm/utils/errors/assertValidationError.js +2 -2
- package/lib-esm/utils/errors/validation.js +5 -6
- package/lib-esm/utils/resolveConfig.d.ts +1 -1
- package/lib-esm/utils/resolveConfig.js +8 -9
- package/lib-esm/utils/resolveLibraryOptions.d.ts +1 -1
- package/lib-esm/utils/resolveLibraryOptions.js +4 -5
- package/package.json +6 -12
- package/src/GraphQLAPI.ts +6 -1
- package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +6 -5
- package/src/internals/InternalGraphQLAPI.ts +7 -8
- package/src/internals/index.ts +0 -1
- package/src/types/index.ts +39 -15
- package/lib/internals/APIClient.d.ts +0 -101
- package/lib/internals/APIClient.js +0 -443
- package/lib/internals/generateClient.d.ts +0 -12
- package/lib/internals/generateClient.js +0 -30
- package/lib/internals/generateModelsProperty.d.ts +0 -4
- package/lib/internals/generateModelsProperty.js +0 -169
- package/lib/internals/types.d.ts +0 -10
- package/lib/internals/types.js +0 -2
- package/lib/utils/resolveOwnerFields.d.ts +0 -12
- package/lib/utils/resolveOwnerFields.js +0 -63
- package/lib-esm/internals/APIClient.d.ts +0 -101
- package/lib-esm/internals/APIClient.js +0 -432
- package/lib-esm/internals/generateClient.d.ts +0 -12
- package/lib-esm/internals/generateClient.js +0 -26
- package/lib-esm/internals/generateModelsProperty.d.ts +0 -4
- package/lib-esm/internals/generateModelsProperty.js +0 -165
- package/lib-esm/internals/types.d.ts +0 -10
- package/lib-esm/internals/types.js +0 -1
- package/lib-esm/utils/resolveOwnerFields.d.ts +0 -12
- package/lib-esm/utils/resolveOwnerFields.js +0 -59
- package/src/internals/APIClient.ts +0 -583
- package/src/internals/generateClient.ts +0 -30
- package/src/internals/generateModelsProperty.ts +0 -192
- package/src/internals/types.ts +0 -15
- package/src/utils/resolveOwnerFields.ts +0 -65
|
@@ -1,443 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.normalizeMutationInput = exports.buildGraphQLVariables = exports.generateGraphQLDocument = exports.generateSelectionSet = exports.selectionSetIRToString = exports.customSelectionSetToIR = exports.graphQLOperationsInfo = exports.initializeModel = exports.flattenItems = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
6
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
7
|
-
var resolveOwnerFields_1 = require("../utils/resolveOwnerFields");
|
|
8
|
-
var connectionType = {
|
|
9
|
-
HAS_ONE: 'HAS_ONE',
|
|
10
|
-
HAS_MANY: 'HAS_MANY',
|
|
11
|
-
BELONGS_TO: 'BELONGS_TO',
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
* @param GraphQL response object
|
|
16
|
-
* @returns response object with `items` properties flattened
|
|
17
|
-
*/
|
|
18
|
-
var flattenItems = function (obj) {
|
|
19
|
-
var res = {};
|
|
20
|
-
Object.entries(obj).forEach(function (_a) {
|
|
21
|
-
var _b = tslib_1.__read(_a, 2), prop = _b[0], value = _b[1];
|
|
22
|
-
if (typeof value === 'object' && value !== null) {
|
|
23
|
-
if (value.items !== undefined) {
|
|
24
|
-
res[prop] = value.items.map(function (item) {
|
|
25
|
-
return (0, exports.flattenItems)(item);
|
|
26
|
-
});
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
res[prop] = (0, exports.flattenItems)(value);
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
res[prop] = value;
|
|
33
|
-
});
|
|
34
|
-
return res;
|
|
35
|
-
};
|
|
36
|
-
exports.flattenItems = flattenItems;
|
|
37
|
-
// TODO: this should accept single result to support CRUD methods; create helper for array/list
|
|
38
|
-
function initializeModel(client, modelName, result, modelIntrospection) {
|
|
39
|
-
var introModel = modelIntrospection.models[modelName];
|
|
40
|
-
var introModelFields = introModel.fields;
|
|
41
|
-
var modelFields = Object.entries(introModelFields)
|
|
42
|
-
.filter(function (_a) {
|
|
43
|
-
var _b;
|
|
44
|
-
var _c = tslib_1.__read(_a, 2), _ = _c[0], field = _c[1];
|
|
45
|
-
return ((_b = field === null || field === void 0 ? void 0 : field.type) === null || _b === void 0 ? void 0 : _b.model) !== undefined;
|
|
46
|
-
})
|
|
47
|
-
.map(function (_a) {
|
|
48
|
-
var _b = tslib_1.__read(_a, 1), fieldName = _b[0];
|
|
49
|
-
return fieldName;
|
|
50
|
-
});
|
|
51
|
-
return result.map(function (record) {
|
|
52
|
-
var e_1, _a;
|
|
53
|
-
var _b, _c;
|
|
54
|
-
var initializedRelationalFields = {};
|
|
55
|
-
var _loop_1 = function (field) {
|
|
56
|
-
var relatedModelName = introModelFields[field].type.model;
|
|
57
|
-
var relatedModel = modelIntrospection.models[relatedModelName];
|
|
58
|
-
var relatedModelPKFieldName = relatedModel.primaryKeyInfo.primaryKeyFieldName;
|
|
59
|
-
var relatedModelSKFieldNames = relatedModel.primaryKeyInfo.sortKeyFieldNames;
|
|
60
|
-
var relationType = introModelFields[field].association.connectionType;
|
|
61
|
-
var connectionFields = introModelFields[field].association.associatedWith;
|
|
62
|
-
var targetNames = ((_b = introModelFields[field].association) === null || _b === void 0 ? void 0 : _b.targetNames) || [];
|
|
63
|
-
switch (relationType) {
|
|
64
|
-
case connectionType.HAS_ONE:
|
|
65
|
-
case connectionType.BELONGS_TO:
|
|
66
|
-
var sortKeyValues_1 = relatedModelSKFieldNames.reduce(function (acc, curVal) {
|
|
67
|
-
if (record[curVal]) {
|
|
68
|
-
return (acc[curVal] = record[curVal]);
|
|
69
|
-
}
|
|
70
|
-
}, {});
|
|
71
|
-
initializedRelationalFields[field] = function () {
|
|
72
|
-
var _a;
|
|
73
|
-
if (record[targetNames[0]]) {
|
|
74
|
-
return client.models[relatedModelName].get(tslib_1.__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1));
|
|
75
|
-
}
|
|
76
|
-
return undefined;
|
|
77
|
-
};
|
|
78
|
-
break;
|
|
79
|
-
case connectionType.HAS_MANY:
|
|
80
|
-
var parentPk_1 = introModel.primaryKeyInfo.primaryKeyFieldName;
|
|
81
|
-
var parentSK_1 = introModel.primaryKeyInfo.sortKeyFieldNames;
|
|
82
|
-
// M:N check - TODO: refactor
|
|
83
|
-
if ((_c = relatedModel.fields[connectionFields[0]]) === null || _c === void 0 ? void 0 : _c.type.model) {
|
|
84
|
-
var relatedTargetNames = relatedModel.fields[connectionFields[0]].association.targetNames;
|
|
85
|
-
var hasManyFilter_1 = relatedTargetNames.map(function (field, idx) {
|
|
86
|
-
var _a, _b;
|
|
87
|
-
if (idx === 0) {
|
|
88
|
-
return _a = {}, _a[field] = { eq: record[parentPk_1] }, _a;
|
|
89
|
-
}
|
|
90
|
-
return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
|
|
91
|
-
});
|
|
92
|
-
initializedRelationalFields[field] = function () {
|
|
93
|
-
if (record[parentPk_1]) {
|
|
94
|
-
return client.models[relatedModelName].list({
|
|
95
|
-
filter: { and: hasManyFilter_1 },
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
return [];
|
|
99
|
-
};
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
var hasManyFilter_2 = connectionFields.map(function (field, idx) {
|
|
103
|
-
var _a, _b;
|
|
104
|
-
if (idx === 0) {
|
|
105
|
-
return _a = {}, _a[field] = { eq: record[parentPk_1] }, _a;
|
|
106
|
-
}
|
|
107
|
-
return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
|
|
108
|
-
});
|
|
109
|
-
initializedRelationalFields[field] = function () {
|
|
110
|
-
if (record[parentPk_1]) {
|
|
111
|
-
return client.models[relatedModelName].list({
|
|
112
|
-
filter: { and: hasManyFilter_2 },
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
return [];
|
|
116
|
-
};
|
|
117
|
-
break;
|
|
118
|
-
default:
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
try {
|
|
123
|
-
for (var modelFields_1 = tslib_1.__values(modelFields), modelFields_1_1 = modelFields_1.next(); !modelFields_1_1.done; modelFields_1_1 = modelFields_1.next()) {
|
|
124
|
-
var field = modelFields_1_1.value;
|
|
125
|
-
_loop_1(field);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
129
|
-
finally {
|
|
130
|
-
try {
|
|
131
|
-
if (modelFields_1_1 && !modelFields_1_1.done && (_a = modelFields_1.return)) _a.call(modelFields_1);
|
|
132
|
-
}
|
|
133
|
-
finally { if (e_1) throw e_1.error; }
|
|
134
|
-
}
|
|
135
|
-
return tslib_1.__assign(tslib_1.__assign({}, record), initializedRelationalFields);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
exports.initializeModel = initializeModel;
|
|
139
|
-
exports.graphQLOperationsInfo = {
|
|
140
|
-
CREATE: { operationPrefix: 'create', usePlural: false },
|
|
141
|
-
READ: { operationPrefix: 'get', usePlural: false },
|
|
142
|
-
UPDATE: { operationPrefix: 'update', usePlural: false },
|
|
143
|
-
DELETE: { operationPrefix: 'delete', usePlural: false },
|
|
144
|
-
LIST: { operationPrefix: 'list', usePlural: true },
|
|
145
|
-
ONCREATE: { operationPrefix: 'onCreate', usePlural: false },
|
|
146
|
-
ONUPDATE: { operationPrefix: 'onUpdate', usePlural: false },
|
|
147
|
-
ONDELETE: { operationPrefix: 'onDelete', usePlural: false },
|
|
148
|
-
};
|
|
149
|
-
var graphQLDocumentsCache = new Map();
|
|
150
|
-
var SELECTION_SET_WILDCARD = '*';
|
|
151
|
-
function defaultSelectionSetForModel(modelDefinition) {
|
|
152
|
-
// fields that are explicitly part of the graphql schema; not
|
|
153
|
-
// inferred from owner auth rules.
|
|
154
|
-
var fields = modelDefinition.fields;
|
|
155
|
-
var explicitFields = Object.values(fields)
|
|
156
|
-
.map(function (_a) {
|
|
157
|
-
var type = _a.type, name = _a.name;
|
|
158
|
-
return typeof type === 'string' && name;
|
|
159
|
-
}) // Default selection set omits model fields
|
|
160
|
-
.filter(Boolean);
|
|
161
|
-
// fields used for owner auth rules that may or may not also
|
|
162
|
-
// be explicit on the model.
|
|
163
|
-
var ownerFields = (0, resolveOwnerFields_1.resolveOwnerFields)(modelDefinition);
|
|
164
|
-
return Array.from(new Set(explicitFields.concat(ownerFields)));
|
|
165
|
-
}
|
|
166
|
-
var FIELD_IR = '';
|
|
167
|
-
/**
|
|
168
|
-
* Generates nested Custom Selection Set IR from path
|
|
169
|
-
*
|
|
170
|
-
* @param modelIntrospection
|
|
171
|
-
* @param modelName
|
|
172
|
-
* @param selectionSet - array of object paths
|
|
173
|
-
* @example
|
|
174
|
-
* ### Given
|
|
175
|
-
* `selectionSet = ['id', 'comments.post.id']`
|
|
176
|
-
* ### Returns
|
|
177
|
-
* ```ts
|
|
178
|
-
* {
|
|
179
|
-
* id: '',
|
|
180
|
-
* comments: {
|
|
181
|
-
* items: { post: { id: '' } }
|
|
182
|
-
* }
|
|
183
|
-
* }
|
|
184
|
-
* ```
|
|
185
|
-
*/
|
|
186
|
-
function customSelectionSetToIR(modelIntrospection, modelName, selectionSet) {
|
|
187
|
-
var modelDefinition = modelIntrospection[modelName];
|
|
188
|
-
var modelFields = modelDefinition.fields;
|
|
189
|
-
return selectionSet.reduce(function (resultObj, path) {
|
|
190
|
-
var _a, _b, _c;
|
|
191
|
-
var _d = tslib_1.__read(path.split('.')), fieldName = _d[0], nested = _d[1], rest = _d.slice(2);
|
|
192
|
-
if (nested) {
|
|
193
|
-
var relatedModel = (_b = (_a = modelFields[fieldName]) === null || _a === void 0 ? void 0 : _a.type) === null || _b === void 0 ? void 0 : _b.model;
|
|
194
|
-
if (!relatedModel) {
|
|
195
|
-
// TODO: may need to change this to support custom types
|
|
196
|
-
throw Error("".concat(fieldName, " is not a model field"));
|
|
197
|
-
}
|
|
198
|
-
var relatedModelDefinition = modelIntrospection[relatedModel];
|
|
199
|
-
var selectionSet_1 = nested === SELECTION_SET_WILDCARD
|
|
200
|
-
? defaultSelectionSetIR(relatedModelDefinition)
|
|
201
|
-
: // if we have a path like 'field.anotherField' recursively build up selection set IR
|
|
202
|
-
customSelectionSetToIR(modelIntrospection, relatedModel, [
|
|
203
|
-
tslib_1.__spreadArray([nested], tslib_1.__read(rest), false).join('.'),
|
|
204
|
-
]);
|
|
205
|
-
if ((_c = modelFields[fieldName]) === null || _c === void 0 ? void 0 : _c.isArray) {
|
|
206
|
-
var existing = resultObj[fieldName] || {
|
|
207
|
-
items: {},
|
|
208
|
-
};
|
|
209
|
-
var merged_1 = tslib_1.__assign(tslib_1.__assign({}, existing.items), selectionSet_1);
|
|
210
|
-
resultObj[fieldName] = { items: merged_1 };
|
|
211
|
-
return resultObj;
|
|
212
|
-
}
|
|
213
|
-
var existingItems = resultObj[fieldName] || {};
|
|
214
|
-
var merged = tslib_1.__assign(tslib_1.__assign({}, existingItems), selectionSet_1);
|
|
215
|
-
resultObj[fieldName] = merged;
|
|
216
|
-
return resultObj;
|
|
217
|
-
}
|
|
218
|
-
var exists = Boolean(modelFields[fieldName]);
|
|
219
|
-
if (!exists) {
|
|
220
|
-
throw Error("".concat(fieldName, " is not a field of model ").concat(modelName));
|
|
221
|
-
}
|
|
222
|
-
resultObj[fieldName] = FIELD_IR;
|
|
223
|
-
return resultObj;
|
|
224
|
-
}, {});
|
|
225
|
-
}
|
|
226
|
-
exports.customSelectionSetToIR = customSelectionSetToIR;
|
|
227
|
-
var defaultSelectionSetIR = function (relatedModelDefinition) {
|
|
228
|
-
var defaultSelectionSet = defaultSelectionSetForModel(relatedModelDefinition);
|
|
229
|
-
var reduced = defaultSelectionSet.reduce(function (acc, curVal) {
|
|
230
|
-
acc[curVal] = FIELD_IR;
|
|
231
|
-
return acc;
|
|
232
|
-
}, {});
|
|
233
|
-
return reduced;
|
|
234
|
-
};
|
|
235
|
-
/**
|
|
236
|
-
* Stringifies selection set IR
|
|
237
|
-
* * @example
|
|
238
|
-
* ### Given
|
|
239
|
-
* ```ts
|
|
240
|
-
* {
|
|
241
|
-
* id: '',
|
|
242
|
-
* comments: {
|
|
243
|
-
* items: { post: { id: '' } }
|
|
244
|
-
* }
|
|
245
|
-
* }
|
|
246
|
-
* ```
|
|
247
|
-
* ### Returns
|
|
248
|
-
* `'id comments { items { post { id } } }'`
|
|
249
|
-
*/
|
|
250
|
-
function selectionSetIRToString(obj) {
|
|
251
|
-
var res = [];
|
|
252
|
-
Object.entries(obj).forEach(function (_a) {
|
|
253
|
-
var _b = tslib_1.__read(_a, 2), fieldName = _b[0], value = _b[1];
|
|
254
|
-
if (value === FIELD_IR) {
|
|
255
|
-
res.push(fieldName);
|
|
256
|
-
}
|
|
257
|
-
else if (typeof value === 'object' && value !== null) {
|
|
258
|
-
if (value === null || value === void 0 ? void 0 : value.items) {
|
|
259
|
-
res.push(fieldName, '{', 'items', '{', selectionSetIRToString(value.items), '}', '}');
|
|
260
|
-
}
|
|
261
|
-
else {
|
|
262
|
-
res.push(fieldName, '{', selectionSetIRToString(value), '}');
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
return res.join(' ');
|
|
267
|
-
}
|
|
268
|
-
exports.selectionSetIRToString = selectionSetIRToString;
|
|
269
|
-
function generateSelectionSet(modelIntrospection, modelName, selectionSet) {
|
|
270
|
-
var modelDefinition = modelIntrospection[modelName];
|
|
271
|
-
if (!selectionSet) {
|
|
272
|
-
return defaultSelectionSetForModel(modelDefinition).join(' ');
|
|
273
|
-
}
|
|
274
|
-
var selSetIr = customSelectionSetToIR(modelIntrospection, modelName, selectionSet);
|
|
275
|
-
var selSetString = selectionSetIRToString(selSetIr);
|
|
276
|
-
return selSetString;
|
|
277
|
-
}
|
|
278
|
-
exports.generateSelectionSet = generateSelectionSet;
|
|
279
|
-
function generateGraphQLDocument(modelIntrospection, modelName, modelOperation, listArgs) {
|
|
280
|
-
var _a;
|
|
281
|
-
var _b, _c;
|
|
282
|
-
var modelDefinition = modelIntrospection[modelName];
|
|
283
|
-
var name = modelDefinition.name, pluralName = modelDefinition.pluralName, fields = modelDefinition.fields, _d = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _d.isCustomPrimaryKey, primaryKeyFieldName = _d.primaryKeyFieldName, sortKeyFieldNames = _d.sortKeyFieldNames;
|
|
284
|
-
var _e = exports.graphQLOperationsInfo[modelOperation], operationPrefix = _e.operationPrefix, usePlural = _e.usePlural;
|
|
285
|
-
var selectionSet = (listArgs || {}).selectionSet;
|
|
286
|
-
var fromCache = (_b = graphQLDocumentsCache.get(name)) === null || _b === void 0 ? void 0 : _b.get(modelOperation);
|
|
287
|
-
if (fromCache !== undefined) {
|
|
288
|
-
return fromCache;
|
|
289
|
-
}
|
|
290
|
-
if (!graphQLDocumentsCache.has(name)) {
|
|
291
|
-
graphQLDocumentsCache.set(name, new Map());
|
|
292
|
-
}
|
|
293
|
-
var graphQLFieldName = "".concat(operationPrefix).concat(usePlural ? pluralName : name);
|
|
294
|
-
var graphQLOperationType;
|
|
295
|
-
var graphQLSelectionSet;
|
|
296
|
-
var graphQLArguments;
|
|
297
|
-
var selectionSetFields = generateSelectionSet(modelIntrospection, modelName, selectionSet);
|
|
298
|
-
switch (modelOperation) {
|
|
299
|
-
case 'CREATE':
|
|
300
|
-
case 'UPDATE':
|
|
301
|
-
case 'DELETE':
|
|
302
|
-
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
|
|
303
|
-
input: "".concat(operationPrefix.charAt(0).toLocaleUpperCase() +
|
|
304
|
-
operationPrefix.slice(1)).concat(name, "Input!"),
|
|
305
|
-
});
|
|
306
|
-
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'mutation');
|
|
307
|
-
case 'READ':
|
|
308
|
-
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = isCustomPrimaryKey
|
|
309
|
-
? tslib_1.__spreadArray([primaryKeyFieldName], tslib_1.__read(sortKeyFieldNames), false).reduce(function (acc, fieldName) {
|
|
310
|
-
acc[fieldName] = fields[fieldName].type;
|
|
311
|
-
return acc;
|
|
312
|
-
}, {})
|
|
313
|
-
: (_a = {},
|
|
314
|
-
_a[primaryKeyFieldName] = "".concat(fields[primaryKeyFieldName].type, "!"),
|
|
315
|
-
_a));
|
|
316
|
-
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
|
|
317
|
-
case 'LIST':
|
|
318
|
-
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
|
|
319
|
-
filter: "Model".concat(name, "FilterInput"),
|
|
320
|
-
});
|
|
321
|
-
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'query');
|
|
322
|
-
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { ".concat(selectionSetFields, " }"));
|
|
323
|
-
case 'ONCREATE':
|
|
324
|
-
case 'ONUPDATE':
|
|
325
|
-
case 'ONDELETE':
|
|
326
|
-
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
|
|
327
|
-
filter: "ModelSubscription".concat(name, "FilterInput"),
|
|
328
|
-
});
|
|
329
|
-
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'subscription');
|
|
330
|
-
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
|
|
331
|
-
}
|
|
332
|
-
var graphQLDocument = "".concat(graphQLOperationType).concat(graphQLArguments
|
|
333
|
-
? "(".concat(Object.entries(graphQLArguments).map(function (_a) {
|
|
334
|
-
var _b = tslib_1.__read(_a, 2), fieldName = _b[0], type = _b[1];
|
|
335
|
-
return "$".concat(fieldName, ": ").concat(type);
|
|
336
|
-
}), ")")
|
|
337
|
-
: '', " { ").concat(graphQLFieldName).concat(graphQLArguments
|
|
338
|
-
? "(".concat(Object.keys(graphQLArguments).map(function (fieldName) { return "".concat(fieldName, ": $").concat(fieldName); }), ")")
|
|
339
|
-
: '', " { ").concat(graphQLSelectionSet, " } }");
|
|
340
|
-
(_c = graphQLDocumentsCache.get(name)) === null || _c === void 0 ? void 0 : _c.set(modelOperation, graphQLDocument);
|
|
341
|
-
return graphQLDocument;
|
|
342
|
-
}
|
|
343
|
-
exports.generateGraphQLDocument = generateGraphQLDocument;
|
|
344
|
-
function buildGraphQLVariables(modelDefinition, operation, arg, modelIntrospection) {
|
|
345
|
-
var _a;
|
|
346
|
-
var fields = modelDefinition.fields, _b = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _b.isCustomPrimaryKey, primaryKeyFieldName = _b.primaryKeyFieldName, sortKeyFieldNames = _b.sortKeyFieldNames;
|
|
347
|
-
var variables = {};
|
|
348
|
-
// TODO: process input
|
|
349
|
-
switch (operation) {
|
|
350
|
-
case 'CREATE':
|
|
351
|
-
variables = {
|
|
352
|
-
input: normalizeMutationInput(arg, modelDefinition, modelIntrospection),
|
|
353
|
-
};
|
|
354
|
-
break;
|
|
355
|
-
case 'UPDATE':
|
|
356
|
-
// readonly fields are not updated
|
|
357
|
-
variables = {
|
|
358
|
-
input: Object.fromEntries(Object.entries(normalizeMutationInput(arg, modelDefinition, modelIntrospection)).filter(function (_a) {
|
|
359
|
-
var _b = tslib_1.__read(_a, 1), fieldName = _b[0];
|
|
360
|
-
var isReadOnly = fields[fieldName].isReadOnly;
|
|
361
|
-
return !isReadOnly;
|
|
362
|
-
})),
|
|
363
|
-
};
|
|
364
|
-
break;
|
|
365
|
-
case 'READ':
|
|
366
|
-
case 'DELETE':
|
|
367
|
-
// only identifiers are sent
|
|
368
|
-
variables = isCustomPrimaryKey
|
|
369
|
-
? tslib_1.__spreadArray([primaryKeyFieldName], tslib_1.__read(sortKeyFieldNames), false).reduce(function (acc, fieldName) {
|
|
370
|
-
acc[fieldName] = arg[fieldName];
|
|
371
|
-
return acc;
|
|
372
|
-
}, {})
|
|
373
|
-
: (_a = {}, _a[primaryKeyFieldName] = arg[primaryKeyFieldName], _a);
|
|
374
|
-
if (operation === 'DELETE') {
|
|
375
|
-
variables = { input: variables };
|
|
376
|
-
}
|
|
377
|
-
break;
|
|
378
|
-
case 'LIST':
|
|
379
|
-
case 'ONCREATE':
|
|
380
|
-
case 'ONUPDATE':
|
|
381
|
-
case 'ONDELETE':
|
|
382
|
-
if (arg === null || arg === void 0 ? void 0 : arg.filter) {
|
|
383
|
-
variables = { filter: arg.filter };
|
|
384
|
-
}
|
|
385
|
-
break;
|
|
386
|
-
default:
|
|
387
|
-
var exhaustiveCheck = operation;
|
|
388
|
-
throw new Error("Unhandled operation case: ".concat(exhaustiveCheck));
|
|
389
|
-
}
|
|
390
|
-
return variables;
|
|
391
|
-
}
|
|
392
|
-
exports.buildGraphQLVariables = buildGraphQLVariables;
|
|
393
|
-
/**
|
|
394
|
-
* Iterates over mutation input values and resolves any model inputs to their corresponding join fields/values
|
|
395
|
-
*
|
|
396
|
-
* @example
|
|
397
|
-
* ### Usage
|
|
398
|
-
* ```ts
|
|
399
|
-
* const result = normalizeMutationInput({ post: post }, model, modelDefinition);
|
|
400
|
-
* ```
|
|
401
|
-
* ### Result
|
|
402
|
-
* ```ts
|
|
403
|
-
* { postId: "abc123" }
|
|
404
|
-
* ```
|
|
405
|
-
*
|
|
406
|
-
*/
|
|
407
|
-
function normalizeMutationInput(mutationInput, model, modelDefinition) {
|
|
408
|
-
var fields = model.fields;
|
|
409
|
-
var normalized = {};
|
|
410
|
-
Object.entries(mutationInput).forEach(function (_a) {
|
|
411
|
-
var _b, _c, _d;
|
|
412
|
-
var _e = tslib_1.__read(_a, 2), inputFieldName = _e[0], inputValue = _e[1];
|
|
413
|
-
var relatedModelName = (_c = (_b = fields[inputFieldName]) === null || _b === void 0 ? void 0 : _b.type) === null || _c === void 0 ? void 0 : _c.model;
|
|
414
|
-
if (relatedModelName) {
|
|
415
|
-
var association_1 = (_d = fields[inputFieldName]) === null || _d === void 0 ? void 0 : _d.association;
|
|
416
|
-
var relatedModelDef = modelDefinition.models[relatedModelName];
|
|
417
|
-
var relatedModelPkInfo_1 = relatedModelDef.primaryKeyInfo;
|
|
418
|
-
if (association_1.connectionType === connectionType.HAS_ONE) {
|
|
419
|
-
association_1.targetNames.forEach(function (targetName, idx) {
|
|
420
|
-
var associatedFieldName = association_1.associatedWith[idx];
|
|
421
|
-
normalized[targetName] = inputValue[associatedFieldName];
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
if (association_1.connectionType === connectionType.BELONGS_TO) {
|
|
425
|
-
association_1.targetNames.forEach(function (targetName, idx) {
|
|
426
|
-
if (idx === 0) {
|
|
427
|
-
var associatedFieldName = relatedModelPkInfo_1.primaryKeyFieldName;
|
|
428
|
-
normalized[targetName] = inputValue[associatedFieldName];
|
|
429
|
-
}
|
|
430
|
-
else {
|
|
431
|
-
var associatedFieldName = relatedModelPkInfo_1.sortKeyFieldNames[idx - 1];
|
|
432
|
-
normalized[targetName] = inputValue[associatedFieldName];
|
|
433
|
-
}
|
|
434
|
-
});
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
else {
|
|
438
|
-
normalized[inputFieldName] = inputValue;
|
|
439
|
-
}
|
|
440
|
-
});
|
|
441
|
-
return normalized;
|
|
442
|
-
}
|
|
443
|
-
exports.normalizeMutationInput = normalizeMutationInput;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { V6Client } from '../types';
|
|
2
|
-
import { ClientGenerationParams } from './types';
|
|
3
|
-
/**
|
|
4
|
-
* @private
|
|
5
|
-
*
|
|
6
|
-
* Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`
|
|
7
|
-
* compatible context object for config and auth fetching.
|
|
8
|
-
*
|
|
9
|
-
* @param params
|
|
10
|
-
* @returns
|
|
11
|
-
*/
|
|
12
|
-
export declare function generateClient<T extends Record<any, any> = never>(params: ClientGenerationParams): V6Client<T>;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateClient = void 0;
|
|
4
|
-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
6
|
-
var v6_1 = require("./v6");
|
|
7
|
-
var generateModelsProperty_1 = require("./generateModelsProperty");
|
|
8
|
-
var types_1 = require("../types");
|
|
9
|
-
/**
|
|
10
|
-
* @private
|
|
11
|
-
*
|
|
12
|
-
* Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`
|
|
13
|
-
* compatible context object for config and auth fetching.
|
|
14
|
-
*
|
|
15
|
-
* @param params
|
|
16
|
-
* @returns
|
|
17
|
-
*/
|
|
18
|
-
function generateClient(params) {
|
|
19
|
-
var _a;
|
|
20
|
-
var client = (_a = {},
|
|
21
|
-
_a[types_1.__amplify] = params.amplify,
|
|
22
|
-
_a.graphql = v6_1.graphql,
|
|
23
|
-
_a.cancel = v6_1.cancel,
|
|
24
|
-
_a.isCancelError = v6_1.isCancelError,
|
|
25
|
-
_a.models = {},
|
|
26
|
-
_a);
|
|
27
|
-
client.models = (0, generateModelsProperty_1.generateModelsProperty)(client, params);
|
|
28
|
-
return client;
|
|
29
|
-
}
|
|
30
|
-
exports.generateClient = generateClient;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { ModelTypes } from '@aws-amplify/amplify-api-next-types-alpha';
|
|
2
|
-
import { ClientGenerationParams } from './types';
|
|
3
|
-
import { V6Client } from '../types';
|
|
4
|
-
export declare function generateModelsProperty<T extends Record<any, any> = never>(client: V6Client, params: ClientGenerationParams): ModelTypes<T>;
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateModelsProperty = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
var APIClient_1 = require("./APIClient");
|
|
6
|
-
var rxjs_1 = require("rxjs");
|
|
7
|
-
var core_1 = require("@aws-amplify/core");
|
|
8
|
-
function generateModelsProperty(client, params) {
|
|
9
|
-
var e_1, _a;
|
|
10
|
-
var _this = this;
|
|
11
|
-
var _b, _c, _d;
|
|
12
|
-
var models = {};
|
|
13
|
-
// params.config gets populated in SSR only; otherwise we get it from the singleton
|
|
14
|
-
var config = (_b = params.config) !== null && _b !== void 0 ? _b : core_1.Amplify.getConfig();
|
|
15
|
-
var modelIntrospection = (_d = (_c = config.API) === null || _c === void 0 ? void 0 : _c.GraphQL) === null || _d === void 0 ? void 0 : _d.modelIntrospection;
|
|
16
|
-
if (!modelIntrospection) {
|
|
17
|
-
return {};
|
|
18
|
-
}
|
|
19
|
-
var _loop_1 = function (model) {
|
|
20
|
-
var name_1 = model.name;
|
|
21
|
-
models[name_1] = {};
|
|
22
|
-
Object.entries(APIClient_1.graphQLOperationsInfo).forEach(function (_a) {
|
|
23
|
-
var _b = tslib_1.__read(_a, 2), key = _b[0], operationPrefix = _b[1].operationPrefix;
|
|
24
|
-
var operation = key;
|
|
25
|
-
if (operation === 'LIST') {
|
|
26
|
-
models[name_1][operationPrefix] = function (args) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
27
|
-
var query, variables, _a, data, nextToken, extensions, _b, key_1, flattenedResult, initialized, error_1;
|
|
28
|
-
return tslib_1.__generator(this, function (_c) {
|
|
29
|
-
switch (_c.label) {
|
|
30
|
-
case 0:
|
|
31
|
-
query = (0, APIClient_1.generateGraphQLDocument)(modelIntrospection.models, name_1, 'LIST', args);
|
|
32
|
-
variables = (0, APIClient_1.buildGraphQLVariables)(model, 'LIST', args, modelIntrospection);
|
|
33
|
-
_c.label = 1;
|
|
34
|
-
case 1:
|
|
35
|
-
_c.trys.push([1, 3, , 4]);
|
|
36
|
-
return [4 /*yield*/, client.graphql({
|
|
37
|
-
query: query,
|
|
38
|
-
variables: variables,
|
|
39
|
-
})];
|
|
40
|
-
case 2:
|
|
41
|
-
_a = (_c.sent()), data = _a.data, nextToken = _a.nextToken, extensions = _a.extensions;
|
|
42
|
-
// flatten response
|
|
43
|
-
if (data !== undefined) {
|
|
44
|
-
_b = tslib_1.__read(Object.keys(data), 1), key_1 = _b[0];
|
|
45
|
-
if (data[key_1].items) {
|
|
46
|
-
flattenedResult = (0, APIClient_1.flattenItems)(data)[key_1];
|
|
47
|
-
// don't init if custom selection set
|
|
48
|
-
if (args === null || args === void 0 ? void 0 : args.selectionSet) {
|
|
49
|
-
return [2 /*return*/, {
|
|
50
|
-
data: flattenedResult,
|
|
51
|
-
nextToken: nextToken,
|
|
52
|
-
extensions: extensions,
|
|
53
|
-
}];
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
initialized = (0, APIClient_1.initializeModel)(client, name_1, flattenedResult, modelIntrospection);
|
|
57
|
-
return [2 /*return*/, {
|
|
58
|
-
data: initialized,
|
|
59
|
-
nextToken: nextToken,
|
|
60
|
-
extensions: extensions,
|
|
61
|
-
}];
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return [2 /*return*/, {
|
|
65
|
-
data: data[key_1],
|
|
66
|
-
nextToken: nextToken,
|
|
67
|
-
extensions: extensions,
|
|
68
|
-
}];
|
|
69
|
-
}
|
|
70
|
-
return [3 /*break*/, 4];
|
|
71
|
-
case 3:
|
|
72
|
-
error_1 = _c.sent();
|
|
73
|
-
if (error_1.errors) {
|
|
74
|
-
// graphql errors pass through
|
|
75
|
-
return [2 /*return*/, error_1];
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
// non-graphql errors re re-thrown
|
|
79
|
-
throw error_1;
|
|
80
|
-
}
|
|
81
|
-
return [3 /*break*/, 4];
|
|
82
|
-
case 4: return [2 /*return*/];
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}); };
|
|
86
|
-
}
|
|
87
|
-
else if (['ONCREATE', 'ONUPDATE', 'ONDELETE'].includes(operation)) {
|
|
88
|
-
models[name_1][operationPrefix] = function (arg, options) {
|
|
89
|
-
var query = (0, APIClient_1.generateGraphQLDocument)(modelIntrospection.models, name_1, operation);
|
|
90
|
-
var variables = (0, APIClient_1.buildGraphQLVariables)(model, operation, arg, modelIntrospection);
|
|
91
|
-
var observable = client.graphql({
|
|
92
|
-
query: query,
|
|
93
|
-
variables: variables,
|
|
94
|
-
});
|
|
95
|
-
return observable.pipe((0, rxjs_1.map)(function (value) {
|
|
96
|
-
var _a = tslib_1.__read(Object.keys(value.data), 1), key = _a[0];
|
|
97
|
-
return value.data[key];
|
|
98
|
-
}));
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
models[name_1][operationPrefix] = function (arg, options) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
103
|
-
var query, variables, _a, data, extensions, _b, key_2, flattenedResult, _c, initialized, error_2;
|
|
104
|
-
return tslib_1.__generator(this, function (_d) {
|
|
105
|
-
switch (_d.label) {
|
|
106
|
-
case 0:
|
|
107
|
-
query = (0, APIClient_1.generateGraphQLDocument)(modelIntrospection.models, name_1, operation, options);
|
|
108
|
-
variables = (0, APIClient_1.buildGraphQLVariables)(model, operation, arg, modelIntrospection);
|
|
109
|
-
_d.label = 1;
|
|
110
|
-
case 1:
|
|
111
|
-
_d.trys.push([1, 3, , 4]);
|
|
112
|
-
return [4 /*yield*/, client.graphql({
|
|
113
|
-
query: query,
|
|
114
|
-
variables: variables,
|
|
115
|
-
})];
|
|
116
|
-
case 2:
|
|
117
|
-
_a = (_d.sent()), data = _a.data, extensions = _a.extensions;
|
|
118
|
-
// flatten response
|
|
119
|
-
if (data !== undefined) {
|
|
120
|
-
_b = tslib_1.__read(Object.keys(data), 1), key_2 = _b[0];
|
|
121
|
-
flattenedResult = (0, APIClient_1.flattenItems)(data)[key_2];
|
|
122
|
-
if (options === null || options === void 0 ? void 0 : options.selectionSet) {
|
|
123
|
-
return [2 /*return*/, { data: flattenedResult, extensions: extensions }];
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
_c = tslib_1.__read((0, APIClient_1.initializeModel)(client, name_1, [flattenedResult], modelIntrospection), 1), initialized = _c[0];
|
|
127
|
-
return [2 /*return*/, { data: initialized, extensions: extensions }];
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
return [2 /*return*/, { data: null, extensions: extensions }];
|
|
132
|
-
}
|
|
133
|
-
return [3 /*break*/, 4];
|
|
134
|
-
case 3:
|
|
135
|
-
error_2 = _d.sent();
|
|
136
|
-
if (error_2.errors) {
|
|
137
|
-
// graphql errors pass through
|
|
138
|
-
return [2 /*return*/, error_2];
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
// non-graphql errors re re-thrown
|
|
142
|
-
throw error_2;
|
|
143
|
-
}
|
|
144
|
-
return [3 /*break*/, 4];
|
|
145
|
-
case 4: return [2 /*return*/];
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
}); };
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
};
|
|
152
|
-
try {
|
|
153
|
-
// TODO: refactor this to use separate methods for each CRUDL.
|
|
154
|
-
// Doesn't make sense to gen the methods dynamically given the different args and return values
|
|
155
|
-
for (var _e = tslib_1.__values(Object.values(modelIntrospection.models)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
156
|
-
var model = _f.value;
|
|
157
|
-
_loop_1(model);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
161
|
-
finally {
|
|
162
|
-
try {
|
|
163
|
-
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
|
|
164
|
-
}
|
|
165
|
-
finally { if (e_1) throw e_1.error; }
|
|
166
|
-
}
|
|
167
|
-
return models;
|
|
168
|
-
}
|
|
169
|
-
exports.generateModelsProperty = generateModelsProperty;
|
package/lib/internals/types.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
2
|
-
/**
|
|
3
|
-
* @private
|
|
4
|
-
*
|
|
5
|
-
* The knobs available for configuring `generateClient` internally.
|
|
6
|
-
*/
|
|
7
|
-
export type ClientGenerationParams = {
|
|
8
|
-
amplify: AmplifyClassV6 | ((fn: (amplify: any) => Promise<any>) => Promise<AmplifyClassV6>);
|
|
9
|
-
config?: object;
|
|
10
|
-
};
|
package/lib/internals/types.js
DELETED