@aws-amplify/api 5.4.6-api-v6.2fd20b7.0 → 5.4.6-api-v6-models.5d6fe9b.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/API.d.ts +3 -3
- package/lib/API.js +67 -131
- package/lib/APIClient.d.ts +46 -0
- package/lib/APIClient.js +334 -0
- package/lib/index.js +6 -6
- package/lib/internals/InternalAPI.js +1 -1
- package/lib/internals/index.js +3 -3
- package/lib/tsconfig.build.tsbuildinfo +1 -0
- package/lib/types/index.js +4 -4
- package/lib-esm/API.d.ts +3 -3
- package/lib-esm/API.js +66 -131
- package/lib-esm/APIClient.d.ts +46 -0
- package/lib-esm/APIClient.js +327 -0
- package/lib-esm/index.js +0 -1
- package/lib-esm/internals/InternalAPI.js +0 -1
- package/lib-esm/internals/index.js +0 -1
- package/lib-esm/tsconfig.build.tsbuildinfo +1 -0
- package/lib-esm/types/index.js +0 -1
- package/package.json +13 -10
- package/src/API.ts +101 -182
- package/src/APIClient.ts +433 -0
- package/lib/.tsbuildinfo +0 -3
- package/lib/API.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/internals/InternalAPI.js.map +0 -1
- package/lib/internals/index.js.map +0 -1
- package/lib/types/index.js.map +0 -1
- package/lib-esm/.tsbuildinfo +0 -3
- package/lib-esm/API.js.map +0 -1
- package/lib-esm/index.js.map +0 -1
- package/lib-esm/internals/InternalAPI.js.map +0 -1
- package/lib-esm/internals/index.js.map +0 -1
- package/lib-esm/types/index.js.map +0 -1
package/lib/API.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { GraphQLOptions, GraphQLResult, GraphQLQuery, GraphQLSubscription } from
|
|
|
3
3
|
import { graphql as v6graphql } from '@aws-amplify/api-graphql/internals';
|
|
4
4
|
import Observable from 'zen-observable-ts';
|
|
5
5
|
import { InternalAPIClass } from './internals/InternalAPI';
|
|
6
|
-
import type { ModelTypes } from '@aws-amplify/types-
|
|
6
|
+
import type { ModelTypes } from '@aws-amplify/amplify-api-next-types-alpha';
|
|
7
7
|
/**
|
|
8
8
|
* @deprecated
|
|
9
9
|
* Use RestApi or GraphQLAPI to reduce your application bundle size
|
|
@@ -29,10 +29,10 @@ export declare class APIClass extends InternalAPIClass {
|
|
|
29
29
|
*/
|
|
30
30
|
generateClient<T extends Record<any, any> = never>(): V6Client<T>;
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
type FilteredKeys<T> = {
|
|
33
33
|
[P in keyof T]: T[P] extends never ? never : P;
|
|
34
34
|
}[keyof T];
|
|
35
|
-
|
|
35
|
+
type ExcludeNeverFields<O> = {
|
|
36
36
|
[K in FilteredKeys<O>]: O[K];
|
|
37
37
|
};
|
|
38
38
|
declare type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
|
package/lib/API.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.API = exports.APIClass = void 0;
|
|
3
4
|
var tslib_1 = require("tslib");
|
|
4
5
|
var internals_1 = require("@aws-amplify/api-graphql/internals");
|
|
5
6
|
var core_1 = require("@aws-amplify/core");
|
|
6
7
|
var InternalAPI_1 = require("./internals/InternalAPI");
|
|
8
|
+
var APIClient_1 = require("./APIClient");
|
|
7
9
|
var logger = new core_1.ConsoleLogger('API');
|
|
8
10
|
/**
|
|
9
11
|
* @deprecated
|
|
@@ -36,38 +38,77 @@ var APIClass = /** @class */ (function (_super) {
|
|
|
36
38
|
var _loop_1 = function (model) {
|
|
37
39
|
var name_1 = model.name;
|
|
38
40
|
client.models[name_1] = {};
|
|
39
|
-
Object.entries(graphQLOperationsInfo).forEach(function (_a) {
|
|
41
|
+
Object.entries(APIClient_1.graphQLOperationsInfo).forEach(function (_a) {
|
|
40
42
|
var _b = tslib_1.__read(_a, 2), key = _b[0], operationPrefix = _b[1].operationPrefix;
|
|
41
43
|
var operation = key;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
44
|
+
if (operation === 'LIST') {
|
|
45
|
+
client.models[name_1][operationPrefix] = function (args) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
46
|
+
var query, variables, res, _a, key_1, flattenedResult, initialized;
|
|
47
|
+
return tslib_1.__generator(this, function (_b) {
|
|
48
|
+
switch (_b.label) {
|
|
49
|
+
case 0:
|
|
50
|
+
query = (0, APIClient_1.generateGraphQLDocument)(modelIntrospection.models, name_1, 'LIST', args);
|
|
51
|
+
variables = (0, APIClient_1.buildGraphQLVariables)(model, 'LIST', args, modelIntrospection);
|
|
52
|
+
console.log('API list', query, variables);
|
|
53
|
+
return [4 /*yield*/, this.graphql({
|
|
54
|
+
query: query,
|
|
55
|
+
variables: variables,
|
|
56
|
+
})];
|
|
57
|
+
case 1:
|
|
58
|
+
res = (_b.sent());
|
|
59
|
+
// flatten response
|
|
60
|
+
if (res.data !== undefined) {
|
|
61
|
+
_a = tslib_1.__read(Object.keys(res.data), 1), key_1 = _a[0];
|
|
62
|
+
if (res.data[key_1].items) {
|
|
63
|
+
flattenedResult = res.data[key_1].items;
|
|
64
|
+
// don't init if custom selection set
|
|
65
|
+
if (args === null || args === void 0 ? void 0 : args.selectionSet) {
|
|
66
|
+
return [2 /*return*/, flattenedResult];
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
initialized = (0, APIClient_1.initializeModel)(client, name_1, flattenedResult, modelIntrospection);
|
|
70
|
+
console.log('initialized', initialized);
|
|
71
|
+
return [2 /*return*/, initialized];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return [2 /*return*/, res.data[key_1]];
|
|
61
75
|
}
|
|
62
|
-
return [2 /*return*/, res
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
76
|
+
return [2 /*return*/, res];
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}); };
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
client.models[name_1][operationPrefix] = function (arg, options) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
83
|
+
var query, variables, res, _a, key_2, _b, initialized;
|
|
84
|
+
return tslib_1.__generator(this, function (_c) {
|
|
85
|
+
switch (_c.label) {
|
|
86
|
+
case 0:
|
|
87
|
+
query = (0, APIClient_1.generateGraphQLDocument)(modelIntrospection.models, name_1, operation);
|
|
88
|
+
variables = (0, APIClient_1.buildGraphQLVariables)(model, operation, arg, modelIntrospection);
|
|
89
|
+
console.log("API ".concat(operationPrefix), query, variables);
|
|
90
|
+
return [4 /*yield*/, this.graphql({
|
|
91
|
+
query: query,
|
|
92
|
+
variables: variables,
|
|
93
|
+
})];
|
|
94
|
+
case 1:
|
|
95
|
+
res = (_c.sent());
|
|
96
|
+
// flatten response
|
|
97
|
+
if (res.data !== undefined) {
|
|
98
|
+
_a = tslib_1.__read(Object.keys(res.data), 1), key_2 = _a[0];
|
|
99
|
+
_b = tslib_1.__read((0, APIClient_1.initializeModel)(client, name_1, [res.data[key_2]], modelIntrospection), 1), initialized = _b[0];
|
|
100
|
+
return [2 /*return*/, initialized];
|
|
101
|
+
}
|
|
102
|
+
return [2 /*return*/, res];
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}); };
|
|
106
|
+
}
|
|
68
107
|
});
|
|
69
108
|
};
|
|
70
109
|
try {
|
|
110
|
+
// TODO: refactor this to use separate methods for each CRUDL.
|
|
111
|
+
// Doesn't make sense to gen the methods dynamically given the different args and return values
|
|
71
112
|
for (var _b = tslib_1.__values(Object.values(modelIntrospection.models)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
72
113
|
var model = _c.value;
|
|
73
114
|
_loop_1(model);
|
|
@@ -85,110 +126,5 @@ var APIClass = /** @class */ (function (_super) {
|
|
|
85
126
|
return APIClass;
|
|
86
127
|
}(InternalAPI_1.InternalAPIClass));
|
|
87
128
|
exports.APIClass = APIClass;
|
|
88
|
-
var graphQLOperationsInfo = {
|
|
89
|
-
CREATE: { operationPrefix: 'create', usePlural: false },
|
|
90
|
-
READ: { operationPrefix: 'get', usePlural: false },
|
|
91
|
-
UPDATE: { operationPrefix: 'update', usePlural: false },
|
|
92
|
-
DELETE: { operationPrefix: 'delete', usePlural: false },
|
|
93
|
-
LIST: { operationPrefix: 'list', usePlural: true },
|
|
94
|
-
};
|
|
95
|
-
var graphQLDocumentsCache = new Map();
|
|
96
|
-
function generateGraphQLDocument(modelDefinition, modelOperation) {
|
|
97
|
-
var _a;
|
|
98
|
-
var _b, _c;
|
|
99
|
-
var name = modelDefinition.name, pluralName = modelDefinition.pluralName, fields = modelDefinition.fields, _d = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _d.isCustomPrimaryKey, primaryKeyFieldName = _d.primaryKeyFieldName, sortKeyFieldNames = _d.sortKeyFieldNames;
|
|
100
|
-
var _e = graphQLOperationsInfo[modelOperation], operationPrefix = _e.operationPrefix, usePlural = _e.usePlural;
|
|
101
|
-
var fromCache = (_b = graphQLDocumentsCache.get(name)) === null || _b === void 0 ? void 0 : _b.get(modelOperation);
|
|
102
|
-
if (fromCache !== undefined) {
|
|
103
|
-
return fromCache;
|
|
104
|
-
}
|
|
105
|
-
if (!graphQLDocumentsCache.has(name)) {
|
|
106
|
-
graphQLDocumentsCache.set(name, new Map());
|
|
107
|
-
}
|
|
108
|
-
var graphQLFieldName = "" + operationPrefix + (usePlural ? pluralName : name);
|
|
109
|
-
var graphQLOperationType;
|
|
110
|
-
var graphQLSelectionSet;
|
|
111
|
-
var graphQLArguments;
|
|
112
|
-
var selectionSetFields = Object.values(fields)
|
|
113
|
-
.map(function (_a) {
|
|
114
|
-
var type = _a.type, name = _a.name;
|
|
115
|
-
return typeof type === 'string' && name;
|
|
116
|
-
}) // Only scalars for now
|
|
117
|
-
.filter(Boolean)
|
|
118
|
-
.join(' ');
|
|
119
|
-
switch (modelOperation) {
|
|
120
|
-
case 'CREATE':
|
|
121
|
-
case 'UPDATE':
|
|
122
|
-
case 'DELETE':
|
|
123
|
-
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
|
|
124
|
-
input: "" + (operationPrefix.charAt(0).toLocaleUpperCase() +
|
|
125
|
-
operationPrefix.slice(1)) + name + "Input!",
|
|
126
|
-
});
|
|
127
|
-
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'mutation');
|
|
128
|
-
case 'READ':
|
|
129
|
-
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = isCustomPrimaryKey
|
|
130
|
-
? tslib_1.__spread([primaryKeyFieldName], sortKeyFieldNames).reduce(function (acc, fieldName) {
|
|
131
|
-
acc[fieldName] = fields[fieldName].type;
|
|
132
|
-
return acc;
|
|
133
|
-
}, {})
|
|
134
|
-
: (_a = {},
|
|
135
|
-
_a[primaryKeyFieldName] = fields[primaryKeyFieldName].type + "!",
|
|
136
|
-
_a));
|
|
137
|
-
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
|
|
138
|
-
case 'LIST':
|
|
139
|
-
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'query');
|
|
140
|
-
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { " + selectionSetFields + " }");
|
|
141
|
-
}
|
|
142
|
-
var graphQLDocument = "" + graphQLOperationType + (graphQLArguments
|
|
143
|
-
? "(" + Object.entries(graphQLArguments).map(function (_a) {
|
|
144
|
-
var _b = tslib_1.__read(_a, 2), fieldName = _b[0], type = _b[1];
|
|
145
|
-
return "$" + fieldName + ": " + type;
|
|
146
|
-
}) + ")"
|
|
147
|
-
: '') + " { " + graphQLFieldName + (graphQLArguments
|
|
148
|
-
? "(" + Object.keys(graphQLArguments).map(function (fieldName) { return fieldName + ": $" + fieldName; }) + ")"
|
|
149
|
-
: '') + " { " + graphQLSelectionSet + " } }";
|
|
150
|
-
(_c = graphQLDocumentsCache.get(name)) === null || _c === void 0 ? void 0 : _c.set(modelOperation, graphQLDocument);
|
|
151
|
-
return graphQLDocument;
|
|
152
|
-
}
|
|
153
|
-
function buildGraphQLVariables(modelDefinition, operation, arg) {
|
|
154
|
-
var _a;
|
|
155
|
-
var fields = modelDefinition.fields, _b = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _b.isCustomPrimaryKey, primaryKeyFieldName = _b.primaryKeyFieldName, sortKeyFieldNames = _b.sortKeyFieldNames;
|
|
156
|
-
var variables = {};
|
|
157
|
-
switch (operation) {
|
|
158
|
-
case 'CREATE':
|
|
159
|
-
variables = { input: arg };
|
|
160
|
-
break;
|
|
161
|
-
case 'UPDATE':
|
|
162
|
-
// readonly fields are not updated
|
|
163
|
-
variables = {
|
|
164
|
-
input: Object.fromEntries(Object.entries(arg).filter(function (_a) {
|
|
165
|
-
var _b = tslib_1.__read(_a, 1), fieldName = _b[0];
|
|
166
|
-
var isReadOnly = fields[fieldName].isReadOnly;
|
|
167
|
-
return !isReadOnly;
|
|
168
|
-
})),
|
|
169
|
-
};
|
|
170
|
-
break;
|
|
171
|
-
case 'READ':
|
|
172
|
-
case 'DELETE':
|
|
173
|
-
// only identifiers are sent
|
|
174
|
-
variables = isCustomPrimaryKey
|
|
175
|
-
? tslib_1.__spread([primaryKeyFieldName], sortKeyFieldNames).reduce(function (acc, fieldName) {
|
|
176
|
-
acc[fieldName] = arg[fieldName];
|
|
177
|
-
return acc;
|
|
178
|
-
}, {})
|
|
179
|
-
: (_a = {}, _a[primaryKeyFieldName] = arg[primaryKeyFieldName], _a);
|
|
180
|
-
if (operation === 'DELETE') {
|
|
181
|
-
variables = { input: variables };
|
|
182
|
-
}
|
|
183
|
-
break;
|
|
184
|
-
case 'LIST':
|
|
185
|
-
break;
|
|
186
|
-
default:
|
|
187
|
-
var exhaustiveCheck = operation;
|
|
188
|
-
throw new Error("Unhandled operation case: " + exhaustiveCheck);
|
|
189
|
-
}
|
|
190
|
-
return variables;
|
|
191
|
-
}
|
|
192
129
|
exports.API = new APIClass(null);
|
|
193
130
|
core_1.Amplify.register(exports.API);
|
|
194
|
-
//# sourceMappingURL=API.js.map
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
type ListArgs = {
|
|
2
|
+
selectionSet?: string[];
|
|
3
|
+
filter?: {};
|
|
4
|
+
};
|
|
5
|
+
export declare function initializeModel(client: any, modelName: string, result: any[], modelIntrospection: any): any[];
|
|
6
|
+
export declare const graphQLOperationsInfo: {
|
|
7
|
+
CREATE: {
|
|
8
|
+
operationPrefix: "create";
|
|
9
|
+
usePlural: boolean;
|
|
10
|
+
};
|
|
11
|
+
READ: {
|
|
12
|
+
operationPrefix: "get";
|
|
13
|
+
usePlural: boolean;
|
|
14
|
+
};
|
|
15
|
+
UPDATE: {
|
|
16
|
+
operationPrefix: "update";
|
|
17
|
+
usePlural: boolean;
|
|
18
|
+
};
|
|
19
|
+
DELETE: {
|
|
20
|
+
operationPrefix: "delete";
|
|
21
|
+
usePlural: boolean;
|
|
22
|
+
};
|
|
23
|
+
LIST: {
|
|
24
|
+
operationPrefix: "list";
|
|
25
|
+
usePlural: boolean;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export type ModelOperation = keyof typeof graphQLOperationsInfo;
|
|
29
|
+
export declare function generateGraphQLDocument(modelIntrospection: any, modelName: string, modelOperation: ModelOperation, listArgs?: ListArgs): string;
|
|
30
|
+
export declare function buildGraphQLVariables(modelDefinition: any, operation: ModelOperation, arg: any, modelIntrospection: any): object;
|
|
31
|
+
/**
|
|
32
|
+
* Iterates over mutation input values and resolves any model inputs to their corresponding join fields/values
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ### Usage
|
|
36
|
+
* ```ts
|
|
37
|
+
* const result = normalizeMutationInput({ post: post }, model, modelDefinition);
|
|
38
|
+
* ```
|
|
39
|
+
* ### Result
|
|
40
|
+
* ```ts
|
|
41
|
+
* { postId: "abc123" }
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
*/
|
|
45
|
+
export declare function normalizeMutationInput(mutationInput: any, model: any, modelDefinition: any): Record<string, unknown>;
|
|
46
|
+
export {};
|
package/lib/APIClient.js
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeMutationInput = exports.buildGraphQLVariables = exports.generateGraphQLDocument = exports.graphQLOperationsInfo = exports.initializeModel = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var connectionType = {
|
|
6
|
+
HAS_ONE: 'HAS_ONE',
|
|
7
|
+
HAS_MANY: 'HAS_MANY',
|
|
8
|
+
BELONGS_TO: 'BELONGS_TO',
|
|
9
|
+
};
|
|
10
|
+
// TODO: this should accept single result to support CRUD methods; create helper for array/list
|
|
11
|
+
function initializeModel(client, modelName, result, modelIntrospection) {
|
|
12
|
+
var introModel = modelIntrospection.models[modelName];
|
|
13
|
+
var introModelFields = introModel.fields;
|
|
14
|
+
var modelFields = Object.entries(introModelFields)
|
|
15
|
+
.filter(function (_a) {
|
|
16
|
+
var _b;
|
|
17
|
+
var _c = tslib_1.__read(_a, 2), _ = _c[0], field = _c[1];
|
|
18
|
+
return ((_b = field === null || field === void 0 ? void 0 : field.type) === null || _b === void 0 ? void 0 : _b.model) !== undefined;
|
|
19
|
+
})
|
|
20
|
+
.map(function (_a) {
|
|
21
|
+
var _b = tslib_1.__read(_a, 1), fieldName = _b[0];
|
|
22
|
+
return fieldName;
|
|
23
|
+
});
|
|
24
|
+
return result.map(function (record) {
|
|
25
|
+
var e_1, _a;
|
|
26
|
+
var _b, _c;
|
|
27
|
+
var initializedRelationalFields = {};
|
|
28
|
+
var _loop_1 = function (field) {
|
|
29
|
+
var relatedModelName = introModelFields[field].type.model;
|
|
30
|
+
var relatedModel = modelIntrospection.models[relatedModelName];
|
|
31
|
+
var relatedModelPKFieldName = relatedModel.primaryKeyInfo.primaryKeyFieldName;
|
|
32
|
+
var relatedModelSKFieldNames = relatedModel.primaryKeyInfo.sortKeyFieldNames;
|
|
33
|
+
var relationType = introModelFields[field].association.connectionType;
|
|
34
|
+
var connectionFields = introModelFields[field].association.associatedWith;
|
|
35
|
+
var targetNames = ((_b = introModelFields[field].association) === null || _b === void 0 ? void 0 : _b.targetNames) || [];
|
|
36
|
+
switch (relationType) {
|
|
37
|
+
case connectionType.HAS_ONE:
|
|
38
|
+
case connectionType.BELONGS_TO:
|
|
39
|
+
var sortKeyValues_1 = relatedModelSKFieldNames.reduce(function (acc, curVal) {
|
|
40
|
+
if (record[curVal]) {
|
|
41
|
+
return (acc[curVal] = record[curVal]);
|
|
42
|
+
}
|
|
43
|
+
}, {});
|
|
44
|
+
initializedRelationalFields[field] = function () {
|
|
45
|
+
var _a;
|
|
46
|
+
if (record[targetNames[0]]) {
|
|
47
|
+
return client.models[relatedModelName].get(tslib_1.__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1));
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
};
|
|
51
|
+
break;
|
|
52
|
+
case connectionType.HAS_MANY:
|
|
53
|
+
var parentPk_1 = introModel.primaryKeyInfo.primaryKeyFieldName;
|
|
54
|
+
var parentSK_1 = introModel.primaryKeyInfo.sortKeyFieldNames;
|
|
55
|
+
// M:N check - TODO: refactor
|
|
56
|
+
if ((_c = relatedModel.fields[connectionFields[0]]) === null || _c === void 0 ? void 0 : _c.type.model) {
|
|
57
|
+
var relatedTargetNames = relatedModel.fields[connectionFields[0]].association.targetNames;
|
|
58
|
+
var hasManyFilter_1 = relatedTargetNames.map(function (field, idx) {
|
|
59
|
+
var _a, _b;
|
|
60
|
+
if (idx === 0) {
|
|
61
|
+
return _a = {}, _a[field] = { eq: record[parentPk_1] }, _a;
|
|
62
|
+
}
|
|
63
|
+
return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
|
|
64
|
+
});
|
|
65
|
+
initializedRelationalFields[field] = function () {
|
|
66
|
+
if (record[parentPk_1]) {
|
|
67
|
+
return client.models[relatedModelName].list({
|
|
68
|
+
filter: { and: hasManyFilter_1 },
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return [];
|
|
72
|
+
};
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
var hasManyFilter_2 = connectionFields.map(function (field, idx) {
|
|
76
|
+
var _a, _b;
|
|
77
|
+
if (idx === 0) {
|
|
78
|
+
return _a = {}, _a[field] = { eq: record[parentPk_1] }, _a;
|
|
79
|
+
}
|
|
80
|
+
return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
|
|
81
|
+
});
|
|
82
|
+
initializedRelationalFields[field] = function () {
|
|
83
|
+
if (record[parentPk_1]) {
|
|
84
|
+
return client.models[relatedModelName].list({
|
|
85
|
+
filter: { and: hasManyFilter_2 },
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return [];
|
|
89
|
+
};
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
try {
|
|
96
|
+
for (var modelFields_1 = tslib_1.__values(modelFields), modelFields_1_1 = modelFields_1.next(); !modelFields_1_1.done; modelFields_1_1 = modelFields_1.next()) {
|
|
97
|
+
var field = modelFields_1_1.value;
|
|
98
|
+
_loop_1(field);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
102
|
+
finally {
|
|
103
|
+
try {
|
|
104
|
+
if (modelFields_1_1 && !modelFields_1_1.done && (_a = modelFields_1.return)) _a.call(modelFields_1);
|
|
105
|
+
}
|
|
106
|
+
finally { if (e_1) throw e_1.error; }
|
|
107
|
+
}
|
|
108
|
+
return tslib_1.__assign(tslib_1.__assign({}, record), initializedRelationalFields);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
exports.initializeModel = initializeModel;
|
|
112
|
+
exports.graphQLOperationsInfo = {
|
|
113
|
+
CREATE: { operationPrefix: 'create', usePlural: false },
|
|
114
|
+
READ: { operationPrefix: 'get', usePlural: false },
|
|
115
|
+
UPDATE: { operationPrefix: 'update', usePlural: false },
|
|
116
|
+
DELETE: { operationPrefix: 'delete', usePlural: false },
|
|
117
|
+
LIST: { operationPrefix: 'list', usePlural: true },
|
|
118
|
+
};
|
|
119
|
+
var graphQLDocumentsCache = new Map();
|
|
120
|
+
var SELECTION_SET_ALL_NESTED = '*';
|
|
121
|
+
function defaultSelectionSetForModel(modelDefinition) {
|
|
122
|
+
var fields = modelDefinition.fields;
|
|
123
|
+
return Object.values(fields)
|
|
124
|
+
.map(function (_a) {
|
|
125
|
+
var type = _a.type, name = _a.name;
|
|
126
|
+
return typeof type === 'string' && name;
|
|
127
|
+
}) // Default selection set omits model fields
|
|
128
|
+
.filter(Boolean)
|
|
129
|
+
.join(' ');
|
|
130
|
+
}
|
|
131
|
+
function generateSelectionSet(modelIntrospection, modelName, selectionSet) {
|
|
132
|
+
var e_2, _a;
|
|
133
|
+
var _b, _c, _d;
|
|
134
|
+
var modelDefinition = modelIntrospection[modelName];
|
|
135
|
+
var fields = modelDefinition.fields;
|
|
136
|
+
if (!selectionSet) {
|
|
137
|
+
return defaultSelectionSetForModel(modelDefinition);
|
|
138
|
+
}
|
|
139
|
+
var selSet = [];
|
|
140
|
+
try {
|
|
141
|
+
for (var selectionSet_1 = tslib_1.__values(selectionSet), selectionSet_1_1 = selectionSet_1.next(); !selectionSet_1_1.done; selectionSet_1_1 = selectionSet_1.next()) {
|
|
142
|
+
var f = selectionSet_1_1.value;
|
|
143
|
+
var nested = f.includes('.');
|
|
144
|
+
if (nested) {
|
|
145
|
+
var _e = tslib_1.__read(f.split('.'), 2), modelFieldName = _e[0], selectedField = _e[1];
|
|
146
|
+
var relatedModel = (_c = (_b = fields[modelFieldName]) === null || _b === void 0 ? void 0 : _b.type) === null || _c === void 0 ? void 0 : _c.model;
|
|
147
|
+
if (!relatedModel) {
|
|
148
|
+
// TODO: may need to change this to support custom types
|
|
149
|
+
throw Error("".concat(modelFieldName, " is not a model field"));
|
|
150
|
+
}
|
|
151
|
+
if (selectedField === SELECTION_SET_ALL_NESTED) {
|
|
152
|
+
var relatedModelDefinition = modelIntrospection[relatedModel];
|
|
153
|
+
var defaultSelectionSet = defaultSelectionSetForModel(relatedModelDefinition);
|
|
154
|
+
if ((_d = fields[modelFieldName]) === null || _d === void 0 ? void 0 : _d.isArray) {
|
|
155
|
+
selSet.push("".concat(modelFieldName, " { items { ").concat(defaultSelectionSet, " } }"));
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
selSet.push("".concat(modelFieldName, " { ").concat(defaultSelectionSet, " }"));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
var exists = Boolean(fields[f]);
|
|
164
|
+
if (!exists) {
|
|
165
|
+
throw Error("".concat(f, " is not a field of model ").concat(modelName));
|
|
166
|
+
}
|
|
167
|
+
selSet.push(f);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
172
|
+
finally {
|
|
173
|
+
try {
|
|
174
|
+
if (selectionSet_1_1 && !selectionSet_1_1.done && (_a = selectionSet_1.return)) _a.call(selectionSet_1);
|
|
175
|
+
}
|
|
176
|
+
finally { if (e_2) throw e_2.error; }
|
|
177
|
+
}
|
|
178
|
+
return selSet.join(' ');
|
|
179
|
+
}
|
|
180
|
+
function generateGraphQLDocument(modelIntrospection, modelName, modelOperation, listArgs) {
|
|
181
|
+
var _a;
|
|
182
|
+
var _b, _c;
|
|
183
|
+
var modelDefinition = modelIntrospection[modelName];
|
|
184
|
+
var name = modelDefinition.name, pluralName = modelDefinition.pluralName, fields = modelDefinition.fields, _d = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _d.isCustomPrimaryKey, primaryKeyFieldName = _d.primaryKeyFieldName, sortKeyFieldNames = _d.sortKeyFieldNames;
|
|
185
|
+
var _e = exports.graphQLOperationsInfo[modelOperation], operationPrefix = _e.operationPrefix, usePlural = _e.usePlural;
|
|
186
|
+
var selectionSet = (listArgs || {}).selectionSet;
|
|
187
|
+
var fromCache = (_b = graphQLDocumentsCache.get(name)) === null || _b === void 0 ? void 0 : _b.get(modelOperation);
|
|
188
|
+
if (fromCache !== undefined) {
|
|
189
|
+
return fromCache;
|
|
190
|
+
}
|
|
191
|
+
if (!graphQLDocumentsCache.has(name)) {
|
|
192
|
+
graphQLDocumentsCache.set(name, new Map());
|
|
193
|
+
}
|
|
194
|
+
var graphQLFieldName = "".concat(operationPrefix).concat(usePlural ? pluralName : name);
|
|
195
|
+
var graphQLOperationType;
|
|
196
|
+
var graphQLSelectionSet;
|
|
197
|
+
var graphQLArguments;
|
|
198
|
+
var selectionSetFields = generateSelectionSet(modelIntrospection, modelName, selectionSet);
|
|
199
|
+
console.log('generated sel set', selectionSetFields);
|
|
200
|
+
switch (modelOperation) {
|
|
201
|
+
case 'CREATE':
|
|
202
|
+
case 'UPDATE':
|
|
203
|
+
case 'DELETE':
|
|
204
|
+
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
|
|
205
|
+
input: "".concat(operationPrefix.charAt(0).toLocaleUpperCase() +
|
|
206
|
+
operationPrefix.slice(1)).concat(name, "Input!"),
|
|
207
|
+
});
|
|
208
|
+
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'mutation');
|
|
209
|
+
case 'READ':
|
|
210
|
+
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = isCustomPrimaryKey
|
|
211
|
+
? tslib_1.__spreadArray([primaryKeyFieldName], tslib_1.__read(sortKeyFieldNames), false).reduce(function (acc, fieldName) {
|
|
212
|
+
acc[fieldName] = fields[fieldName].type;
|
|
213
|
+
return acc;
|
|
214
|
+
}, {})
|
|
215
|
+
: (_a = {},
|
|
216
|
+
_a[primaryKeyFieldName] = "".concat(fields[primaryKeyFieldName].type, "!"),
|
|
217
|
+
_a));
|
|
218
|
+
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
|
|
219
|
+
case 'LIST':
|
|
220
|
+
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
|
|
221
|
+
filter: "Model".concat(name, "FilterInput"),
|
|
222
|
+
});
|
|
223
|
+
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'query');
|
|
224
|
+
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { ".concat(selectionSetFields, " }"));
|
|
225
|
+
}
|
|
226
|
+
var graphQLDocument = "".concat(graphQLOperationType).concat(graphQLArguments
|
|
227
|
+
? "(".concat(Object.entries(graphQLArguments).map(function (_a) {
|
|
228
|
+
var _b = tslib_1.__read(_a, 2), fieldName = _b[0], type = _b[1];
|
|
229
|
+
return "$".concat(fieldName, ": ").concat(type);
|
|
230
|
+
}), ")")
|
|
231
|
+
: '', " { ").concat(graphQLFieldName).concat(graphQLArguments
|
|
232
|
+
? "(".concat(Object.keys(graphQLArguments).map(function (fieldName) { return "".concat(fieldName, ": $").concat(fieldName); }), ")")
|
|
233
|
+
: '', " { ").concat(graphQLSelectionSet, " } }");
|
|
234
|
+
(_c = graphQLDocumentsCache.get(name)) === null || _c === void 0 ? void 0 : _c.set(modelOperation, graphQLDocument);
|
|
235
|
+
return graphQLDocument;
|
|
236
|
+
}
|
|
237
|
+
exports.generateGraphQLDocument = generateGraphQLDocument;
|
|
238
|
+
function buildGraphQLVariables(modelDefinition, operation, arg, modelIntrospection) {
|
|
239
|
+
var _a;
|
|
240
|
+
var fields = modelDefinition.fields, _b = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _b.isCustomPrimaryKey, primaryKeyFieldName = _b.primaryKeyFieldName, sortKeyFieldNames = _b.sortKeyFieldNames;
|
|
241
|
+
var variables = {};
|
|
242
|
+
// TODO: process input
|
|
243
|
+
switch (operation) {
|
|
244
|
+
case 'CREATE':
|
|
245
|
+
variables = {
|
|
246
|
+
input: normalizeMutationInput(arg, modelDefinition, modelIntrospection),
|
|
247
|
+
};
|
|
248
|
+
break;
|
|
249
|
+
case 'UPDATE':
|
|
250
|
+
// readonly fields are not updated
|
|
251
|
+
variables = {
|
|
252
|
+
input: Object.fromEntries(Object.entries(normalizeMutationInput(arg, modelDefinition, modelIntrospection)).filter(function (_a) {
|
|
253
|
+
var _b = tslib_1.__read(_a, 1), fieldName = _b[0];
|
|
254
|
+
var isReadOnly = fields[fieldName].isReadOnly;
|
|
255
|
+
return !isReadOnly;
|
|
256
|
+
})),
|
|
257
|
+
};
|
|
258
|
+
break;
|
|
259
|
+
case 'READ':
|
|
260
|
+
case 'DELETE':
|
|
261
|
+
// only identifiers are sent
|
|
262
|
+
variables = isCustomPrimaryKey
|
|
263
|
+
? tslib_1.__spreadArray([primaryKeyFieldName], tslib_1.__read(sortKeyFieldNames), false).reduce(function (acc, fieldName) {
|
|
264
|
+
acc[fieldName] = arg[fieldName];
|
|
265
|
+
return acc;
|
|
266
|
+
}, {})
|
|
267
|
+
: (_a = {}, _a[primaryKeyFieldName] = arg[primaryKeyFieldName], _a);
|
|
268
|
+
if (operation === 'DELETE') {
|
|
269
|
+
variables = { input: variables };
|
|
270
|
+
}
|
|
271
|
+
break;
|
|
272
|
+
case 'LIST':
|
|
273
|
+
if (arg === null || arg === void 0 ? void 0 : arg.filter) {
|
|
274
|
+
variables = { filter: arg.filter };
|
|
275
|
+
}
|
|
276
|
+
break;
|
|
277
|
+
default:
|
|
278
|
+
var exhaustiveCheck = operation;
|
|
279
|
+
throw new Error("Unhandled operation case: ".concat(exhaustiveCheck));
|
|
280
|
+
}
|
|
281
|
+
return variables;
|
|
282
|
+
}
|
|
283
|
+
exports.buildGraphQLVariables = buildGraphQLVariables;
|
|
284
|
+
/**
|
|
285
|
+
* Iterates over mutation input values and resolves any model inputs to their corresponding join fields/values
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ### Usage
|
|
289
|
+
* ```ts
|
|
290
|
+
* const result = normalizeMutationInput({ post: post }, model, modelDefinition);
|
|
291
|
+
* ```
|
|
292
|
+
* ### Result
|
|
293
|
+
* ```ts
|
|
294
|
+
* { postId: "abc123" }
|
|
295
|
+
* ```
|
|
296
|
+
*
|
|
297
|
+
*/
|
|
298
|
+
function normalizeMutationInput(mutationInput, model, modelDefinition) {
|
|
299
|
+
var fields = model.fields;
|
|
300
|
+
var normalized = {};
|
|
301
|
+
Object.entries(mutationInput).forEach(function (_a) {
|
|
302
|
+
var _b, _c, _d;
|
|
303
|
+
var _e = tslib_1.__read(_a, 2), inputFieldName = _e[0], inputValue = _e[1];
|
|
304
|
+
var relatedModelName = (_c = (_b = fields[inputFieldName]) === null || _b === void 0 ? void 0 : _b.type) === null || _c === void 0 ? void 0 : _c.model;
|
|
305
|
+
if (relatedModelName) {
|
|
306
|
+
var association_1 = (_d = fields[inputFieldName]) === null || _d === void 0 ? void 0 : _d.association;
|
|
307
|
+
var relatedModelDef = modelDefinition.models[relatedModelName];
|
|
308
|
+
var relatedModelPkInfo_1 = relatedModelDef.primaryKeyInfo;
|
|
309
|
+
if (association_1.connectionType === connectionType.HAS_ONE) {
|
|
310
|
+
association_1.targetNames.forEach(function (targetName, idx) {
|
|
311
|
+
var associatedFieldName = association_1.associatedWith[idx];
|
|
312
|
+
normalized[targetName] = inputValue[associatedFieldName];
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
if (association_1.connectionType === connectionType.BELONGS_TO) {
|
|
316
|
+
association_1.targetNames.forEach(function (targetName, idx) {
|
|
317
|
+
if (idx === 0) {
|
|
318
|
+
var associatedFieldName = relatedModelPkInfo_1.primaryKeyFieldName;
|
|
319
|
+
normalized[targetName] = inputValue[associatedFieldName];
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
var associatedFieldName = relatedModelPkInfo_1.sortKeyFieldNames[idx - 1];
|
|
323
|
+
normalized[targetName] = inputValue[associatedFieldName];
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
normalized[inputFieldName] = inputValue;
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
return normalized;
|
|
333
|
+
}
|
|
334
|
+
exports.normalizeMutationInput = normalizeMutationInput;
|
package/lib/index.js
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.GRAPHQL_AUTH_MODE = exports.GraphQLAuthError = exports.graphqlOperation = exports.APIClass = exports.API = void 0;
|
|
5
6
|
var API_1 = require("./API");
|
|
6
|
-
exports
|
|
7
|
-
exports
|
|
7
|
+
Object.defineProperty(exports, "API", { enumerable: true, get: function () { return API_1.API; } });
|
|
8
|
+
Object.defineProperty(exports, "APIClass", { enumerable: true, get: function () { return API_1.APIClass; } });
|
|
8
9
|
var api_graphql_1 = require("@aws-amplify/api-graphql");
|
|
9
|
-
exports
|
|
10
|
-
exports
|
|
11
|
-
exports
|
|
12
|
-
//# sourceMappingURL=index.js.map
|
|
10
|
+
Object.defineProperty(exports, "graphqlOperation", { enumerable: true, get: function () { return api_graphql_1.graphqlOperation; } });
|
|
11
|
+
Object.defineProperty(exports, "GraphQLAuthError", { enumerable: true, get: function () { return api_graphql_1.GraphQLAuthError; } });
|
|
12
|
+
Object.defineProperty(exports, "GRAPHQL_AUTH_MODE", { enumerable: true, get: function () { return api_graphql_1.GRAPHQL_AUTH_MODE; } });
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InternalAPI = exports.InternalAPIClass = void 0;
|
|
3
4
|
var tslib_1 = require("tslib");
|
|
4
5
|
var internals_1 = require("@aws-amplify/api-graphql/internals");
|
|
5
6
|
var api_rest_1 = require("@aws-amplify/api-rest");
|
|
@@ -164,4 +165,3 @@ var InternalAPIClass = /** @class */ (function () {
|
|
|
164
165
|
exports.InternalAPIClass = InternalAPIClass;
|
|
165
166
|
exports.InternalAPI = new InternalAPIClass(null);
|
|
166
167
|
core_1.Amplify.register(exports.InternalAPI);
|
|
167
|
-
//# sourceMappingURL=InternalAPI.js.map
|