@aws-amplify/api-graphql 4.0.1-api-v6-models.9351bcf.0 → 4.0.1-api-v6-models.891fe0d.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/internals/server/package.json +8 -0
- package/lib/GraphQLAPI.d.ts +1 -1
- package/lib/GraphQLAPI.js +23 -28
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +467 -582
- package/lib/index.js +1 -1
- package/lib/internals/APIClient.d.ts +16 -1
- package/lib/internals/APIClient.js +232 -206
- package/lib/internals/InternalGraphQLAPI.d.ts +1 -1
- package/lib/internals/InternalGraphQLAPI.js +205 -247
- package/lib/internals/generateClient.js +14 -13
- package/lib/internals/generateModelsProperty.js +19 -312
- package/lib/internals/operations/get.d.ts +2 -0
- package/lib/internals/operations/get.js +59 -0
- package/lib/internals/operations/list.d.ts +2 -0
- package/lib/internals/operations/list.js +71 -0
- package/lib/internals/operations/observeQuery.d.ts +2 -0
- package/lib/internals/operations/observeQuery.js +119 -0
- package/lib/internals/operations/subscription.d.ts +1 -0
- package/lib/internals/operations/subscription.js +27 -0
- package/lib/internals/server/generateClient.d.ts +11 -0
- package/lib/internals/server/generateClient.js +30 -0
- package/lib/internals/server/generateModelsProperty.d.ts +3 -0
- package/lib/internals/server/generateModelsProperty.js +45 -0
- package/lib/internals/server/index.d.ts +1 -0
- package/lib/internals/server/index.js +7 -0
- package/lib/internals/v6.js +3 -3
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/index.d.ts +36 -4
- package/lib/utils/ConnectionStateMonitor.js +51 -64
- package/lib/utils/ReachabilityMonitor/index.js +2 -2
- package/lib/utils/ReachabilityMonitor/index.native.js +4 -6
- package/lib/utils/ReconnectionMonitor.js +20 -24
- package/lib/utils/errors/APIError.js +7 -11
- package/lib/utils/errors/assertValidationError.js +4 -4
- package/lib/utils/errors/validation.js +5 -6
- package/lib/utils/findIndexByFields.js +4 -9
- package/lib/utils/resolveConfig.js +9 -10
- package/lib/utils/resolveLibraryOptions.js +4 -5
- package/lib/utils/resolveOwnerFields.js +10 -33
- package/lib/utils/resolvePKFields.js +2 -3
- package/lib-esm/GraphQLAPI.d.ts +1 -1
- package/lib-esm/GraphQLAPI.js +23 -29
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +459 -575
- package/lib-esm/Providers/constants.js +10 -10
- package/lib-esm/internals/APIClient.d.ts +16 -1
- package/lib-esm/internals/APIClient.js +230 -205
- package/lib-esm/internals/InternalGraphQLAPI.d.ts +1 -1
- package/lib-esm/internals/InternalGraphQLAPI.js +201 -244
- package/lib-esm/internals/generateClient.js +11 -10
- package/lib-esm/internals/generateModelsProperty.js +19 -312
- package/lib-esm/internals/operations/get.d.ts +2 -0
- package/lib-esm/internals/operations/get.js +55 -0
- package/lib-esm/internals/operations/list.d.ts +2 -0
- package/lib-esm/internals/operations/list.js +67 -0
- package/lib-esm/internals/operations/observeQuery.d.ts +2 -0
- package/lib-esm/internals/operations/observeQuery.js +115 -0
- package/lib-esm/internals/operations/subscription.d.ts +1 -0
- package/lib-esm/internals/operations/subscription.js +23 -0
- package/lib-esm/internals/server/generateClient.d.ts +11 -0
- package/lib-esm/internals/server/generateClient.js +26 -0
- package/lib-esm/internals/server/generateModelsProperty.d.ts +3 -0
- package/lib-esm/internals/server/generateModelsProperty.js +41 -0
- package/lib-esm/internals/server/index.d.ts +1 -0
- package/lib-esm/internals/server/index.js +3 -0
- package/lib-esm/internals/v6.js +1 -1
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/index.d.ts +36 -4
- package/lib-esm/types/index.js +3 -3
- package/lib-esm/utils/ConnectionStateMonitor.js +49 -63
- package/lib-esm/utils/ReachabilityMonitor/index.js +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/findIndexByFields.js +4 -9
- package/lib-esm/utils/resolveConfig.js +8 -9
- package/lib-esm/utils/resolveLibraryOptions.js +4 -5
- package/lib-esm/utils/resolveOwnerFields.js +10 -33
- package/lib-esm/utils/resolvePKFields.js +2 -3
- package/package.json +8 -7
- package/src/GraphQLAPI.ts +6 -2
- package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +6 -5
- package/src/internals/APIClient.ts +122 -35
- package/src/internals/InternalGraphQLAPI.ts +35 -10
- package/src/internals/generateClient.ts +0 -1
- package/src/internals/generateModelsProperty.ts +27 -340
- package/src/internals/operations/get.ts +121 -0
- package/src/internals/operations/list.ts +111 -0
- package/src/internals/operations/observeQuery.ts +140 -0
- package/src/internals/operations/subscription.ts +59 -0
- package/src/internals/server/generateClient.ts +45 -0
- package/src/internals/server/generateModelsProperty.ts +70 -0
- package/src/internals/server/index.ts +4 -0
- package/src/types/index.ts +65 -9
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
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");
|
|
3
|
+
exports.authModeParams = exports.normalizeMutationInput = exports.buildGraphQLVariables = exports.generateGraphQLDocument = exports.generateSelectionSet = exports.selectionSetIRToString = exports.customSelectionSetToIR = exports.graphQLOperationsInfo = exports.initializeModel = exports.flattenItems = void 0;
|
|
5
4
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
6
5
|
// SPDX-License-Identifier: Apache-2.0
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const resolveOwnerFields_1 = require("../utils/resolveOwnerFields");
|
|
7
|
+
const types_1 = require("../types");
|
|
8
|
+
const connectionType = {
|
|
9
9
|
HAS_ONE: 'HAS_ONE',
|
|
10
10
|
HAS_MANY: 'HAS_MANY',
|
|
11
11
|
BELONGS_TO: 'BELONGS_TO',
|
|
@@ -15,15 +15,12 @@ var connectionType = {
|
|
|
15
15
|
* @param GraphQL response object
|
|
16
16
|
* @returns response object with `items` properties flattened
|
|
17
17
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
Object.entries(obj).forEach(
|
|
21
|
-
var _b = tslib_1.__read(_a, 2), prop = _b[0], value = _b[1];
|
|
18
|
+
const flattenItems = (obj) => {
|
|
19
|
+
const res = {};
|
|
20
|
+
Object.entries(obj).forEach(([prop, value]) => {
|
|
22
21
|
if (typeof value === 'object' && value !== null) {
|
|
23
22
|
if (value.items !== undefined) {
|
|
24
|
-
res[prop] = value.items.map(
|
|
25
|
-
return (0, exports.flattenItems)(item);
|
|
26
|
-
});
|
|
23
|
+
res[prop] = value.items.map((item) => (0, exports.flattenItems)(item));
|
|
27
24
|
return;
|
|
28
25
|
}
|
|
29
26
|
res[prop] = (0, exports.flattenItems)(value);
|
|
@@ -35,111 +32,133 @@ var flattenItems = function (obj) {
|
|
|
35
32
|
};
|
|
36
33
|
exports.flattenItems = flattenItems;
|
|
37
34
|
// TODO: this should accept single result to support CRUD methods; create helper for array/list
|
|
38
|
-
function initializeModel(client, modelName, result, modelIntrospection, authMode, authToken) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
.filter(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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) || [];
|
|
35
|
+
function initializeModel(client, modelName, result, modelIntrospection, authMode, authToken, context = false) {
|
|
36
|
+
const introModel = modelIntrospection.models[modelName];
|
|
37
|
+
const introModelFields = introModel.fields;
|
|
38
|
+
const modelFields = Object.entries(introModelFields)
|
|
39
|
+
.filter(([_, field]) => field?.type?.model !== undefined)
|
|
40
|
+
.map(([fieldName]) => fieldName);
|
|
41
|
+
return result.map(record => {
|
|
42
|
+
const initializedRelationalFields = {};
|
|
43
|
+
for (const field of modelFields) {
|
|
44
|
+
const relatedModelName = introModelFields[field].type.model;
|
|
45
|
+
const relatedModel = modelIntrospection.models[relatedModelName];
|
|
46
|
+
const relatedModelPKFieldName = relatedModel.primaryKeyInfo.primaryKeyFieldName;
|
|
47
|
+
const relatedModelSKFieldNames = relatedModel.primaryKeyInfo.sortKeyFieldNames;
|
|
48
|
+
const relationType = introModelFields[field].association.connectionType;
|
|
49
|
+
const connectionFields = introModelFields[field].association.associatedWith;
|
|
50
|
+
const targetNames = introModelFields[field].association?.targetNames || [];
|
|
63
51
|
switch (relationType) {
|
|
64
52
|
case connectionType.HAS_ONE:
|
|
65
53
|
case connectionType.BELONGS_TO:
|
|
66
|
-
|
|
54
|
+
const sortKeyValues = relatedModelSKFieldNames.reduce((acc, curVal) => {
|
|
67
55
|
if (record[curVal]) {
|
|
68
56
|
return (acc[curVal] = record[curVal]);
|
|
69
57
|
}
|
|
70
58
|
}, {});
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
59
|
+
if (context) {
|
|
60
|
+
initializedRelationalFields[field] = (contextSpec, options) => {
|
|
61
|
+
if (record[targetNames[0]]) {
|
|
62
|
+
return client.models[relatedModelName].get(contextSpec, {
|
|
63
|
+
[relatedModelPKFieldName]: record[targetNames[0]],
|
|
64
|
+
...sortKeyValues,
|
|
65
|
+
}, {
|
|
66
|
+
authMode: options?.authMode || authMode,
|
|
67
|
+
authToken: options?.authToken || authToken,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
initializedRelationalFields[field] = (options) => {
|
|
75
|
+
if (record[targetNames[0]]) {
|
|
76
|
+
return client.models[relatedModelName].get({
|
|
77
|
+
[relatedModelPKFieldName]: record[targetNames[0]],
|
|
78
|
+
...sortKeyValues,
|
|
79
|
+
}, {
|
|
80
|
+
authMode: options?.authMode || authMode,
|
|
81
|
+
authToken: options?.authToken || authToken,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
81
87
|
break;
|
|
82
88
|
case connectionType.HAS_MANY:
|
|
83
|
-
|
|
84
|
-
|
|
89
|
+
const parentPk = introModel.primaryKeyInfo.primaryKeyFieldName;
|
|
90
|
+
const parentSK = introModel.primaryKeyInfo.sortKeyFieldNames;
|
|
85
91
|
// M:N check - TODO: refactor
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
var _a, _b;
|
|
92
|
+
if (relatedModel.fields[connectionFields[0]]?.type.model) {
|
|
93
|
+
const relatedTargetNames = relatedModel.fields[connectionFields[0]].association.targetNames;
|
|
94
|
+
const hasManyFilter = relatedTargetNames.map((field, idx) => {
|
|
90
95
|
if (idx === 0) {
|
|
91
|
-
return
|
|
96
|
+
return { [field]: { eq: record[parentPk] } };
|
|
92
97
|
}
|
|
93
|
-
return
|
|
98
|
+
return { [field]: { eq: record[parentSK[idx - 1]] } };
|
|
94
99
|
});
|
|
95
|
-
|
|
96
|
-
|
|
100
|
+
if (context) {
|
|
101
|
+
initializedRelationalFields[field] = (contextSpec, options) => {
|
|
102
|
+
if (record[parentPk]) {
|
|
103
|
+
return client.models[relatedModelName].list(contextSpec, {
|
|
104
|
+
filter: { and: hasManyFilter },
|
|
105
|
+
authMode: options?.authMode || authMode,
|
|
106
|
+
authToken: options?.authToken || authToken,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return [];
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
initializedRelationalFields[field] = (options) => {
|
|
114
|
+
if (record[parentPk]) {
|
|
115
|
+
return client.models[relatedModelName].list({
|
|
116
|
+
filter: { and: hasManyFilter },
|
|
117
|
+
authMode: options?.authMode || authMode,
|
|
118
|
+
authToken: options?.authToken || authToken,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
return [];
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
const hasManyFilter = connectionFields.map((field, idx) => {
|
|
127
|
+
if (idx === 0) {
|
|
128
|
+
return { [field]: { eq: record[parentPk] } };
|
|
129
|
+
}
|
|
130
|
+
return { [field]: { eq: record[parentSK[idx - 1]] } };
|
|
131
|
+
});
|
|
132
|
+
if (context) {
|
|
133
|
+
initializedRelationalFields[field] = (contextSpec, options) => {
|
|
134
|
+
if (record[parentPk]) {
|
|
135
|
+
return client.models[relatedModelName].list(contextSpec, {
|
|
136
|
+
filter: { and: hasManyFilter },
|
|
137
|
+
authMode: options?.authMode || authMode,
|
|
138
|
+
authToken: options?.authToken || authToken,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return [];
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
initializedRelationalFields[field] = (options) => {
|
|
146
|
+
if (record[parentPk]) {
|
|
97
147
|
return client.models[relatedModelName].list({
|
|
98
|
-
filter: { and:
|
|
99
|
-
authMode:
|
|
100
|
-
authToken:
|
|
148
|
+
filter: { and: hasManyFilter },
|
|
149
|
+
authMode: options?.authMode || authMode,
|
|
150
|
+
authToken: options?.authToken || authToken,
|
|
101
151
|
});
|
|
102
152
|
}
|
|
103
153
|
return [];
|
|
104
154
|
};
|
|
105
|
-
break;
|
|
106
155
|
}
|
|
107
|
-
var hasManyFilter_2 = connectionFields.map(function (field, idx) {
|
|
108
|
-
var _a, _b;
|
|
109
|
-
if (idx === 0) {
|
|
110
|
-
return _a = {}, _a[field] = { eq: record[parentPk_1] }, _a;
|
|
111
|
-
}
|
|
112
|
-
return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
|
|
113
|
-
});
|
|
114
|
-
initializedRelationalFields[field] = function (options) {
|
|
115
|
-
if (record[parentPk_1]) {
|
|
116
|
-
return client.models[relatedModelName].list({
|
|
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,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
return [];
|
|
123
|
-
};
|
|
124
156
|
break;
|
|
125
157
|
default:
|
|
126
158
|
break;
|
|
127
159
|
}
|
|
128
|
-
};
|
|
129
|
-
try {
|
|
130
|
-
for (var modelFields_1 = tslib_1.__values(modelFields), modelFields_1_1 = modelFields_1.next(); !modelFields_1_1.done; modelFields_1_1 = modelFields_1.next()) {
|
|
131
|
-
var field = modelFields_1_1.value;
|
|
132
|
-
_loop_1(field);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
136
|
-
finally {
|
|
137
|
-
try {
|
|
138
|
-
if (modelFields_1_1 && !modelFields_1_1.done && (_a = modelFields_1.return)) _a.call(modelFields_1);
|
|
139
|
-
}
|
|
140
|
-
finally { if (e_1) throw e_1.error; }
|
|
141
160
|
}
|
|
142
|
-
return
|
|
161
|
+
return { ...record, ...initializedRelationalFields };
|
|
143
162
|
});
|
|
144
163
|
}
|
|
145
164
|
exports.initializeModel = initializeModel;
|
|
@@ -154,24 +173,21 @@ exports.graphQLOperationsInfo = {
|
|
|
154
173
|
ONDELETE: { operationPrefix: 'onDelete', usePlural: false },
|
|
155
174
|
OBSERVE_QUERY: { operationPrefix: 'observeQuery', usePlural: false },
|
|
156
175
|
};
|
|
157
|
-
|
|
158
|
-
|
|
176
|
+
const graphQLDocumentsCache = new Map();
|
|
177
|
+
const SELECTION_SET_WILDCARD = '*';
|
|
159
178
|
function defaultSelectionSetForModel(modelDefinition) {
|
|
160
179
|
// fields that are explicitly part of the graphql schema; not
|
|
161
180
|
// inferred from owner auth rules.
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
.map(
|
|
165
|
-
var type = _a.type, name = _a.name;
|
|
166
|
-
return typeof type === 'string' && name;
|
|
167
|
-
}) // Default selection set omits model fields
|
|
181
|
+
const { fields } = modelDefinition;
|
|
182
|
+
const explicitFields = Object.values(fields)
|
|
183
|
+
.map(({ type, name }) => typeof type === 'string' && name) // Default selection set omits model fields
|
|
168
184
|
.filter(Boolean);
|
|
169
185
|
// fields used for owner auth rules that may or may not also
|
|
170
186
|
// be explicit on the model.
|
|
171
|
-
|
|
187
|
+
const ownerFields = (0, resolveOwnerFields_1.resolveOwnerFields)(modelDefinition);
|
|
172
188
|
return Array.from(new Set(explicitFields.concat(ownerFields)));
|
|
173
189
|
}
|
|
174
|
-
|
|
190
|
+
const FIELD_IR = '';
|
|
175
191
|
/**
|
|
176
192
|
* Generates nested Custom Selection Set IR from path
|
|
177
193
|
*
|
|
@@ -192,49 +208,48 @@ var FIELD_IR = '';
|
|
|
192
208
|
* ```
|
|
193
209
|
*/
|
|
194
210
|
function customSelectionSetToIR(modelIntrospection, modelName, selectionSet) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
return selectionSet.reduce(
|
|
198
|
-
|
|
199
|
-
var _d = tslib_1.__read(path.split('.')), fieldName = _d[0], nested = _d[1], rest = _d.slice(2);
|
|
211
|
+
const modelDefinition = modelIntrospection[modelName];
|
|
212
|
+
const { fields: modelFields } = modelDefinition;
|
|
213
|
+
return selectionSet.reduce((resultObj, path) => {
|
|
214
|
+
const [fieldName, nested, ...rest] = path.split('.');
|
|
200
215
|
if (nested) {
|
|
201
|
-
|
|
216
|
+
const relatedModel = modelFields[fieldName]?.type?.model;
|
|
202
217
|
if (!relatedModel) {
|
|
203
218
|
// TODO: may need to change this to support custom types
|
|
204
|
-
throw Error(
|
|
219
|
+
throw Error(`${fieldName} is not a model field`);
|
|
205
220
|
}
|
|
206
|
-
|
|
207
|
-
|
|
221
|
+
const relatedModelDefinition = modelIntrospection[relatedModel];
|
|
222
|
+
const selectionSet = nested === SELECTION_SET_WILDCARD
|
|
208
223
|
? defaultSelectionSetIR(relatedModelDefinition)
|
|
209
224
|
: // if we have a path like 'field.anotherField' recursively build up selection set IR
|
|
210
225
|
customSelectionSetToIR(modelIntrospection, relatedModel, [
|
|
211
|
-
|
|
226
|
+
[nested, ...rest].join('.'),
|
|
212
227
|
]);
|
|
213
|
-
if (
|
|
214
|
-
|
|
228
|
+
if (modelFields[fieldName]?.isArray) {
|
|
229
|
+
const existing = resultObj[fieldName] || {
|
|
215
230
|
items: {},
|
|
216
231
|
};
|
|
217
|
-
|
|
218
|
-
resultObj[fieldName] = { items:
|
|
232
|
+
const merged = { ...existing.items, ...selectionSet };
|
|
233
|
+
resultObj[fieldName] = { items: merged };
|
|
219
234
|
return resultObj;
|
|
220
235
|
}
|
|
221
|
-
|
|
222
|
-
|
|
236
|
+
const existingItems = resultObj[fieldName] || {};
|
|
237
|
+
const merged = { ...existingItems, ...selectionSet };
|
|
223
238
|
resultObj[fieldName] = merged;
|
|
224
239
|
return resultObj;
|
|
225
240
|
}
|
|
226
|
-
|
|
241
|
+
const exists = Boolean(modelFields[fieldName]);
|
|
227
242
|
if (!exists) {
|
|
228
|
-
throw Error(
|
|
243
|
+
throw Error(`${fieldName} is not a field of model ${modelName}`);
|
|
229
244
|
}
|
|
230
245
|
resultObj[fieldName] = FIELD_IR;
|
|
231
246
|
return resultObj;
|
|
232
247
|
}, {});
|
|
233
248
|
}
|
|
234
249
|
exports.customSelectionSetToIR = customSelectionSetToIR;
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
250
|
+
const defaultSelectionSetIR = relatedModelDefinition => {
|
|
251
|
+
const defaultSelectionSet = defaultSelectionSetForModel(relatedModelDefinition);
|
|
252
|
+
const reduced = defaultSelectionSet.reduce((acc, curVal) => {
|
|
238
253
|
acc[curVal] = FIELD_IR;
|
|
239
254
|
return acc;
|
|
240
255
|
}, {});
|
|
@@ -256,14 +271,13 @@ var defaultSelectionSetIR = function (relatedModelDefinition) {
|
|
|
256
271
|
* `'id comments { items { post { id } } }'`
|
|
257
272
|
*/
|
|
258
273
|
function selectionSetIRToString(obj) {
|
|
259
|
-
|
|
260
|
-
Object.entries(obj).forEach(
|
|
261
|
-
var _b = tslib_1.__read(_a, 2), fieldName = _b[0], value = _b[1];
|
|
274
|
+
const res = [];
|
|
275
|
+
Object.entries(obj).forEach(([fieldName, value]) => {
|
|
262
276
|
if (value === FIELD_IR) {
|
|
263
277
|
res.push(fieldName);
|
|
264
278
|
}
|
|
265
279
|
else if (typeof value === 'object' && value !== null) {
|
|
266
|
-
if (value
|
|
280
|
+
if (value?.items) {
|
|
267
281
|
res.push(fieldName, '{', 'items', '{', selectionSetIRToString(value.items), '}', '}');
|
|
268
282
|
}
|
|
269
283
|
else {
|
|
@@ -275,90 +289,89 @@ function selectionSetIRToString(obj) {
|
|
|
275
289
|
}
|
|
276
290
|
exports.selectionSetIRToString = selectionSetIRToString;
|
|
277
291
|
function generateSelectionSet(modelIntrospection, modelName, selectionSet) {
|
|
278
|
-
|
|
292
|
+
const modelDefinition = modelIntrospection[modelName];
|
|
279
293
|
if (!selectionSet) {
|
|
280
294
|
return defaultSelectionSetForModel(modelDefinition).join(' ');
|
|
281
295
|
}
|
|
282
|
-
|
|
283
|
-
|
|
296
|
+
const selSetIr = customSelectionSetToIR(modelIntrospection, modelName, selectionSet);
|
|
297
|
+
const selSetString = selectionSetIRToString(selSetIr);
|
|
284
298
|
return selSetString;
|
|
285
299
|
}
|
|
286
300
|
exports.generateSelectionSet = generateSelectionSet;
|
|
287
301
|
function generateGraphQLDocument(modelIntrospection, modelName, modelOperation, listArgs) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
var selectionSet = (listArgs || {}).selectionSet;
|
|
294
|
-
var fromCache = (_b = graphQLDocumentsCache.get(name)) === null || _b === void 0 ? void 0 : _b.get(modelOperation);
|
|
302
|
+
const modelDefinition = modelIntrospection[modelName];
|
|
303
|
+
const { name, pluralName, fields, primaryKeyInfo: { isCustomPrimaryKey, primaryKeyFieldName, sortKeyFieldNames, }, } = modelDefinition;
|
|
304
|
+
const { operationPrefix, usePlural } = exports.graphQLOperationsInfo[modelOperation];
|
|
305
|
+
const { selectionSet } = listArgs || {};
|
|
306
|
+
const fromCache = graphQLDocumentsCache.get(name)?.get(modelOperation);
|
|
295
307
|
if (fromCache !== undefined) {
|
|
296
308
|
return fromCache;
|
|
297
309
|
}
|
|
298
310
|
if (!graphQLDocumentsCache.has(name)) {
|
|
299
311
|
graphQLDocumentsCache.set(name, new Map());
|
|
300
312
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
313
|
+
const graphQLFieldName = `${operationPrefix}${usePlural ? pluralName : name}`;
|
|
314
|
+
let graphQLOperationType;
|
|
315
|
+
let graphQLSelectionSet;
|
|
316
|
+
let graphQLArguments;
|
|
317
|
+
const selectionSetFields = generateSelectionSet(modelIntrospection, modelName, selectionSet);
|
|
306
318
|
switch (modelOperation) {
|
|
307
319
|
case 'CREATE':
|
|
308
320
|
case 'UPDATE':
|
|
309
321
|
case 'DELETE':
|
|
310
|
-
graphQLArguments
|
|
311
|
-
|
|
312
|
-
operationPrefix.
|
|
313
|
-
|
|
314
|
-
|
|
322
|
+
graphQLArguments ??
|
|
323
|
+
(graphQLArguments = {
|
|
324
|
+
input: `${operationPrefix.charAt(0).toLocaleUpperCase() +
|
|
325
|
+
operationPrefix.slice(1)}${name}Input!`,
|
|
326
|
+
});
|
|
327
|
+
graphQLOperationType ?? (graphQLOperationType = 'mutation');
|
|
315
328
|
case 'READ':
|
|
316
|
-
graphQLArguments
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
329
|
+
graphQLArguments ??
|
|
330
|
+
(graphQLArguments = isCustomPrimaryKey
|
|
331
|
+
? [primaryKeyFieldName, ...sortKeyFieldNames].reduce((acc, fieldName) => {
|
|
332
|
+
acc[fieldName] = fields[fieldName].type;
|
|
333
|
+
return acc;
|
|
334
|
+
}, {})
|
|
335
|
+
: {
|
|
336
|
+
[primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}!`,
|
|
337
|
+
});
|
|
338
|
+
graphQLSelectionSet ?? (graphQLSelectionSet = selectionSetFields);
|
|
325
339
|
case 'LIST':
|
|
326
|
-
graphQLArguments
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
340
|
+
graphQLArguments ??
|
|
341
|
+
(graphQLArguments = {
|
|
342
|
+
filter: `Model${name}FilterInput`,
|
|
343
|
+
limit: 'Int',
|
|
344
|
+
nextToken: 'String',
|
|
345
|
+
});
|
|
346
|
+
graphQLOperationType ?? (graphQLOperationType = 'query');
|
|
347
|
+
graphQLSelectionSet ??
|
|
348
|
+
(graphQLSelectionSet = `items { ${selectionSetFields} } nextToken __typename`);
|
|
333
349
|
case 'ONCREATE':
|
|
334
350
|
case 'ONUPDATE':
|
|
335
351
|
case 'ONDELETE':
|
|
336
|
-
graphQLArguments
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
352
|
+
graphQLArguments ??
|
|
353
|
+
(graphQLArguments = {
|
|
354
|
+
filter: `ModelSubscription${name}FilterInput`,
|
|
355
|
+
});
|
|
356
|
+
graphQLOperationType ?? (graphQLOperationType = 'subscription');
|
|
357
|
+
graphQLSelectionSet ?? (graphQLSelectionSet = selectionSetFields);
|
|
341
358
|
break;
|
|
342
359
|
case 'OBSERVE_QUERY':
|
|
343
360
|
default:
|
|
344
361
|
throw new Error('Internal error: Attempted to generate graphql document for observeQuery. Please report this error.');
|
|
345
362
|
}
|
|
346
|
-
|
|
347
|
-
?
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
? "(".concat(Object.keys(graphQLArguments).map(function (fieldName) { return "".concat(fieldName, ": $").concat(fieldName); }), ")")
|
|
353
|
-
: '', " { ").concat(graphQLSelectionSet, " } }");
|
|
354
|
-
(_c = graphQLDocumentsCache.get(name)) === null || _c === void 0 ? void 0 : _c.set(modelOperation, graphQLDocument);
|
|
363
|
+
const graphQLDocument = `${graphQLOperationType}${graphQLArguments
|
|
364
|
+
? `(${Object.entries(graphQLArguments).map(([fieldName, type]) => `\$${fieldName}: ${type}`)})`
|
|
365
|
+
: ''} { ${graphQLFieldName}${graphQLArguments
|
|
366
|
+
? `(${Object.keys(graphQLArguments).map(fieldName => `${fieldName}: \$${fieldName}`)})`
|
|
367
|
+
: ''} { ${graphQLSelectionSet} } }`;
|
|
368
|
+
graphQLDocumentsCache.get(name)?.set(modelOperation, graphQLDocument);
|
|
355
369
|
return graphQLDocument;
|
|
356
370
|
}
|
|
357
371
|
exports.generateGraphQLDocument = generateGraphQLDocument;
|
|
358
372
|
function buildGraphQLVariables(modelDefinition, operation, arg, modelIntrospection) {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
var variables = {};
|
|
373
|
+
const { fields, primaryKeyInfo: { isCustomPrimaryKey, primaryKeyFieldName, sortKeyFieldNames, }, } = modelDefinition;
|
|
374
|
+
let variables = {};
|
|
362
375
|
// TODO: process input
|
|
363
376
|
switch (operation) {
|
|
364
377
|
case 'CREATE':
|
|
@@ -369,9 +382,8 @@ function buildGraphQLVariables(modelDefinition, operation, arg, modelIntrospecti
|
|
|
369
382
|
case 'UPDATE':
|
|
370
383
|
// readonly fields are not updated
|
|
371
384
|
variables = {
|
|
372
|
-
input: Object.fromEntries(Object.entries(normalizeMutationInput(arg, modelDefinition, modelIntrospection)).filter(
|
|
373
|
-
|
|
374
|
-
var isReadOnly = fields[fieldName].isReadOnly;
|
|
385
|
+
input: Object.fromEntries(Object.entries(normalizeMutationInput(arg, modelDefinition, modelIntrospection)).filter(([fieldName]) => {
|
|
386
|
+
const { isReadOnly } = fields[fieldName];
|
|
375
387
|
return !isReadOnly;
|
|
376
388
|
})),
|
|
377
389
|
};
|
|
@@ -380,27 +392,27 @@ function buildGraphQLVariables(modelDefinition, operation, arg, modelIntrospecti
|
|
|
380
392
|
case 'DELETE':
|
|
381
393
|
// only identifiers are sent
|
|
382
394
|
variables = isCustomPrimaryKey
|
|
383
|
-
?
|
|
395
|
+
? [primaryKeyFieldName, ...sortKeyFieldNames].reduce((acc, fieldName) => {
|
|
384
396
|
acc[fieldName] = arg[fieldName];
|
|
385
397
|
return acc;
|
|
386
398
|
}, {})
|
|
387
|
-
:
|
|
399
|
+
: { [primaryKeyFieldName]: arg[primaryKeyFieldName] };
|
|
388
400
|
if (operation === 'DELETE') {
|
|
389
401
|
variables = { input: variables };
|
|
390
402
|
}
|
|
391
403
|
break;
|
|
392
404
|
case 'LIST':
|
|
393
|
-
if (arg
|
|
405
|
+
if (arg?.filter) {
|
|
394
406
|
variables.filter = arg.filter;
|
|
395
407
|
}
|
|
396
|
-
if (arg
|
|
408
|
+
if (arg?.nextToken) {
|
|
397
409
|
variables.nextToken = arg.nextToken;
|
|
398
410
|
}
|
|
399
411
|
break;
|
|
400
412
|
case 'ONCREATE':
|
|
401
413
|
case 'ONUPDATE':
|
|
402
414
|
case 'ONDELETE':
|
|
403
|
-
if (arg
|
|
415
|
+
if (arg?.filter) {
|
|
404
416
|
variables = { filter: arg.filter };
|
|
405
417
|
}
|
|
406
418
|
break;
|
|
@@ -408,8 +420,8 @@ function buildGraphQLVariables(modelDefinition, operation, arg, modelIntrospecti
|
|
|
408
420
|
throw new Error('Internal error: Attempted to build variables for observeQuery. Please report this error.');
|
|
409
421
|
break;
|
|
410
422
|
default:
|
|
411
|
-
|
|
412
|
-
throw new Error(
|
|
423
|
+
const exhaustiveCheck = operation;
|
|
424
|
+
throw new Error(`Unhandled operation case: ${exhaustiveCheck}`);
|
|
413
425
|
}
|
|
414
426
|
return variables;
|
|
415
427
|
}
|
|
@@ -429,30 +441,28 @@ exports.buildGraphQLVariables = buildGraphQLVariables;
|
|
|
429
441
|
*
|
|
430
442
|
*/
|
|
431
443
|
function normalizeMutationInput(mutationInput, model, modelDefinition) {
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
Object.entries(mutationInput).forEach(
|
|
435
|
-
|
|
436
|
-
var _e = tslib_1.__read(_a, 2), inputFieldName = _e[0], inputValue = _e[1];
|
|
437
|
-
var relatedModelName = (_c = (_b = fields[inputFieldName]) === null || _b === void 0 ? void 0 : _b.type) === null || _c === void 0 ? void 0 : _c.model;
|
|
444
|
+
const { fields } = model;
|
|
445
|
+
const normalized = {};
|
|
446
|
+
Object.entries(mutationInput).forEach(([inputFieldName, inputValue]) => {
|
|
447
|
+
const relatedModelName = fields[inputFieldName]?.type?.model;
|
|
438
448
|
if (relatedModelName) {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
if (
|
|
443
|
-
|
|
444
|
-
|
|
449
|
+
const association = fields[inputFieldName]?.association;
|
|
450
|
+
const relatedModelDef = modelDefinition.models[relatedModelName];
|
|
451
|
+
const relatedModelPkInfo = relatedModelDef.primaryKeyInfo;
|
|
452
|
+
if (association.connectionType === connectionType.HAS_ONE) {
|
|
453
|
+
association.targetNames.forEach((targetName, idx) => {
|
|
454
|
+
const associatedFieldName = association.associatedWith[idx];
|
|
445
455
|
normalized[targetName] = inputValue[associatedFieldName];
|
|
446
456
|
});
|
|
447
457
|
}
|
|
448
|
-
if (
|
|
449
|
-
|
|
458
|
+
if (association.connectionType === connectionType.BELONGS_TO) {
|
|
459
|
+
association.targetNames.forEach((targetName, idx) => {
|
|
450
460
|
if (idx === 0) {
|
|
451
|
-
|
|
461
|
+
const associatedFieldName = relatedModelPkInfo.primaryKeyFieldName;
|
|
452
462
|
normalized[targetName] = inputValue[associatedFieldName];
|
|
453
463
|
}
|
|
454
464
|
else {
|
|
455
|
-
|
|
465
|
+
const associatedFieldName = relatedModelPkInfo.sortKeyFieldNames[idx - 1];
|
|
456
466
|
normalized[targetName] = inputValue[associatedFieldName];
|
|
457
467
|
}
|
|
458
468
|
});
|
|
@@ -465,3 +475,19 @@ function normalizeMutationInput(mutationInput, model, modelDefinition) {
|
|
|
465
475
|
return normalized;
|
|
466
476
|
}
|
|
467
477
|
exports.normalizeMutationInput = normalizeMutationInput;
|
|
478
|
+
/**
|
|
479
|
+
* Produces a parameter object that can contains auth mode/token overrides
|
|
480
|
+
* only if present in either `options` (first) or configured on the `client`
|
|
481
|
+
* as a fallback.
|
|
482
|
+
*
|
|
483
|
+
* @param client Configured client from `generateClient`
|
|
484
|
+
* @param options Args/Options obect from call site.
|
|
485
|
+
* @returns
|
|
486
|
+
*/
|
|
487
|
+
function authModeParams(client, options = {}) {
|
|
488
|
+
return {
|
|
489
|
+
authMode: options.authMode || client[types_1.__authMode],
|
|
490
|
+
authToken: options.authToken || client[types_1.__authToken],
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
exports.authModeParams = authModeParams;
|