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