@aws-amplify/api 5.3.4-api-v6.2 → 5.3.4-api-v6.13
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 +43 -0
- package/lib/API.js +151 -0
- package/lib/API.js.map +1 -1
- package/lib-esm/API.d.ts +43 -0
- package/lib-esm/API.js +152 -1
- package/lib-esm/API.js.map +1 -1
- package/package.json +6 -6
- package/src/API.ts +217 -0
package/lib/API.d.ts
CHANGED
|
@@ -23,5 +23,48 @@ export declare class APIClass extends InternalAPIClass {
|
|
|
23
23
|
provider: AWSAppSyncRealTimeProvider;
|
|
24
24
|
value: GraphQLResult<T>;
|
|
25
25
|
}> : Promise<GraphQLResult<any>> | Observable<object>;
|
|
26
|
+
/**
|
|
27
|
+
* Generates an API client that can work with models or raw GraphQL
|
|
28
|
+
*/
|
|
29
|
+
generateClient<T = never>(): V6Client<T>;
|
|
26
30
|
}
|
|
31
|
+
declare const graphQLOperationsInfo: {
|
|
32
|
+
CREATE: {
|
|
33
|
+
operationPrefix: "create";
|
|
34
|
+
usePlural: boolean;
|
|
35
|
+
};
|
|
36
|
+
READ: {
|
|
37
|
+
operationPrefix: "get";
|
|
38
|
+
usePlural: boolean;
|
|
39
|
+
};
|
|
40
|
+
UPDATE: {
|
|
41
|
+
operationPrefix: "update";
|
|
42
|
+
usePlural: boolean;
|
|
43
|
+
};
|
|
44
|
+
DELETE: {
|
|
45
|
+
operationPrefix: "delete";
|
|
46
|
+
usePlural: boolean;
|
|
47
|
+
};
|
|
48
|
+
LIST: {
|
|
49
|
+
operationPrefix: "list";
|
|
50
|
+
usePlural: boolean;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
declare type ModelOperation = keyof typeof graphQLOperationsInfo;
|
|
54
|
+
declare type OperationPrefix = (typeof graphQLOperationsInfo)[ModelOperation]['operationPrefix'];
|
|
55
|
+
declare type FilteredKeys<T> = {
|
|
56
|
+
[P in keyof T]: T[P] extends never ? never : P;
|
|
57
|
+
}[keyof T];
|
|
58
|
+
declare type ExcludeNeverFields<O> = {
|
|
59
|
+
[K in FilteredKeys<O>]: O[K];
|
|
60
|
+
};
|
|
61
|
+
declare type V6Client<T = never> = ExcludeNeverFields<{
|
|
62
|
+
graphql: typeof APIClass.prototype.graphql;
|
|
63
|
+
models: {
|
|
64
|
+
[K in keyof T]: {
|
|
65
|
+
[K in OperationPrefix]: (...args: any[]) => Promise<any>;
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
}>;
|
|
27
69
|
export declare const API: APIClass;
|
|
70
|
+
export {};
|
package/lib/API.js
CHANGED
|
@@ -20,9 +20,160 @@ var APIClass = /** @class */ (function (_super) {
|
|
|
20
20
|
APIClass.prototype.graphql = function (options, additionalHeaders) {
|
|
21
21
|
return _super.prototype.graphql.call(this, options, additionalHeaders);
|
|
22
22
|
};
|
|
23
|
+
/**
|
|
24
|
+
* Generates an API client that can work with models or raw GraphQL
|
|
25
|
+
*/
|
|
26
|
+
APIClass.prototype.generateClient = function () {
|
|
27
|
+
var e_1, _a;
|
|
28
|
+
var _this = this;
|
|
29
|
+
var config = _super.prototype.configure.call(this, {});
|
|
30
|
+
var modelIntrospection = config.modelIntrospection;
|
|
31
|
+
var client = {
|
|
32
|
+
graphql: this.graphql.bind(this),
|
|
33
|
+
models: {},
|
|
34
|
+
};
|
|
35
|
+
var _loop_1 = function (model) {
|
|
36
|
+
var name_1 = model.name;
|
|
37
|
+
client.models[name_1] = {};
|
|
38
|
+
Object.entries(graphQLOperationsInfo).forEach(function (_a) {
|
|
39
|
+
var _b = tslib_1.__read(_a, 2), key = _b[0], operationPrefix = _b[1].operationPrefix;
|
|
40
|
+
var operation = key;
|
|
41
|
+
// e.g. clients.models.Todo.update
|
|
42
|
+
client.models[name_1][operationPrefix] = function (arg) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
43
|
+
var query, variables;
|
|
44
|
+
return tslib_1.__generator(this, function (_a) {
|
|
45
|
+
query = generateGraphQLDocument(model, operation);
|
|
46
|
+
variables = buildGraphQLVariables(model, operation, arg);
|
|
47
|
+
return [2 /*return*/, this.graphql({
|
|
48
|
+
query: query,
|
|
49
|
+
variables: variables,
|
|
50
|
+
})];
|
|
51
|
+
});
|
|
52
|
+
}); };
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
try {
|
|
56
|
+
for (var _b = tslib_1.__values(Object.values(modelIntrospection.models)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
57
|
+
var model = _c.value;
|
|
58
|
+
_loop_1(model);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
62
|
+
finally {
|
|
63
|
+
try {
|
|
64
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
65
|
+
}
|
|
66
|
+
finally { if (e_1) throw e_1.error; }
|
|
67
|
+
}
|
|
68
|
+
return client;
|
|
69
|
+
};
|
|
23
70
|
return APIClass;
|
|
24
71
|
}(InternalAPI_1.InternalAPIClass));
|
|
25
72
|
exports.APIClass = APIClass;
|
|
73
|
+
var graphQLOperationsInfo = {
|
|
74
|
+
CREATE: { operationPrefix: 'create', usePlural: false },
|
|
75
|
+
READ: { operationPrefix: 'get', usePlural: false },
|
|
76
|
+
UPDATE: { operationPrefix: 'update', usePlural: false },
|
|
77
|
+
DELETE: { operationPrefix: 'delete', usePlural: false },
|
|
78
|
+
LIST: { operationPrefix: 'list', usePlural: true },
|
|
79
|
+
};
|
|
80
|
+
var graphQLDocumentsCache = new Map();
|
|
81
|
+
function generateGraphQLDocument(modelDefinition, modelOperation) {
|
|
82
|
+
var _a;
|
|
83
|
+
var _b, _c;
|
|
84
|
+
var name = modelDefinition.name, pluralName = modelDefinition.pluralName, fields = modelDefinition.fields, _d = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _d.isCustomPrimaryKey, primaryKeyFieldName = _d.primaryKeyFieldName, sortKeyFieldNames = _d.sortKeyFieldNames;
|
|
85
|
+
var _e = graphQLOperationsInfo[modelOperation], operationPrefix = _e.operationPrefix, usePlural = _e.usePlural;
|
|
86
|
+
var fromCache = (_b = graphQLDocumentsCache.get(name)) === null || _b === void 0 ? void 0 : _b.get(modelOperation);
|
|
87
|
+
if (fromCache !== undefined) {
|
|
88
|
+
return fromCache;
|
|
89
|
+
}
|
|
90
|
+
if (!graphQLDocumentsCache.has(name)) {
|
|
91
|
+
graphQLDocumentsCache.set(name, new Map());
|
|
92
|
+
}
|
|
93
|
+
var graphQLFieldName = "" + operationPrefix + (usePlural ? pluralName : name);
|
|
94
|
+
var graphQLOperationType;
|
|
95
|
+
var graphQLSelectionSet;
|
|
96
|
+
var graphQLArguments;
|
|
97
|
+
var selectionSetFields = Object.values(fields)
|
|
98
|
+
.map(function (_a) {
|
|
99
|
+
var type = _a.type, name = _a.name;
|
|
100
|
+
return typeof type === 'string' && name;
|
|
101
|
+
}) // Only scalars for now
|
|
102
|
+
.filter(Boolean)
|
|
103
|
+
.join(' ');
|
|
104
|
+
switch (modelOperation) {
|
|
105
|
+
case 'CREATE':
|
|
106
|
+
case 'UPDATE':
|
|
107
|
+
case 'DELETE':
|
|
108
|
+
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
|
|
109
|
+
input: "" + (operationPrefix.charAt(0).toLocaleUpperCase() +
|
|
110
|
+
operationPrefix.slice(1)) + name + "Input!",
|
|
111
|
+
});
|
|
112
|
+
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'mutation');
|
|
113
|
+
case 'READ':
|
|
114
|
+
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = isCustomPrimaryKey
|
|
115
|
+
? tslib_1.__spread([primaryKeyFieldName], sortKeyFieldNames).reduce(function (acc, fieldName) {
|
|
116
|
+
acc[fieldName] = fields[fieldName].type;
|
|
117
|
+
return acc;
|
|
118
|
+
}, {})
|
|
119
|
+
: (_a = {},
|
|
120
|
+
_a[primaryKeyFieldName] = fields[primaryKeyFieldName].type + "!",
|
|
121
|
+
_a));
|
|
122
|
+
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
|
|
123
|
+
case 'LIST':
|
|
124
|
+
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'query');
|
|
125
|
+
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { " + selectionSetFields + " }");
|
|
126
|
+
}
|
|
127
|
+
var graphQLDocument = "" + graphQLOperationType + (graphQLArguments
|
|
128
|
+
? "(" + Object.entries(graphQLArguments).map(function (_a) {
|
|
129
|
+
var _b = tslib_1.__read(_a, 2), fieldName = _b[0], type = _b[1];
|
|
130
|
+
return "$" + fieldName + ": " + type;
|
|
131
|
+
}) + ")"
|
|
132
|
+
: '') + " { " + graphQLFieldName + (graphQLArguments
|
|
133
|
+
? "(" + Object.keys(graphQLArguments).map(function (fieldName) { return fieldName + ": $" + fieldName; }) + ")"
|
|
134
|
+
: '') + " { " + graphQLSelectionSet + " } }";
|
|
135
|
+
(_c = graphQLDocumentsCache.get(name)) === null || _c === void 0 ? void 0 : _c.set(modelOperation, graphQLDocument);
|
|
136
|
+
return graphQLDocument;
|
|
137
|
+
}
|
|
138
|
+
function buildGraphQLVariables(modelDefinition, operation, arg) {
|
|
139
|
+
var _a;
|
|
140
|
+
var fields = modelDefinition.fields, _b = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _b.isCustomPrimaryKey, primaryKeyFieldName = _b.primaryKeyFieldName, sortKeyFieldNames = _b.sortKeyFieldNames;
|
|
141
|
+
var variables = {};
|
|
142
|
+
switch (operation) {
|
|
143
|
+
case 'CREATE':
|
|
144
|
+
variables = { input: arg };
|
|
145
|
+
break;
|
|
146
|
+
case 'UPDATE':
|
|
147
|
+
// readonly fields are not updated
|
|
148
|
+
variables = {
|
|
149
|
+
input: Object.fromEntries(Object.entries(arg).filter(function (_a) {
|
|
150
|
+
var _b = tslib_1.__read(_a, 1), fieldName = _b[0];
|
|
151
|
+
var isReadOnly = fields[fieldName].isReadOnly;
|
|
152
|
+
return !isReadOnly;
|
|
153
|
+
})),
|
|
154
|
+
};
|
|
155
|
+
break;
|
|
156
|
+
case 'READ':
|
|
157
|
+
case 'DELETE':
|
|
158
|
+
// only identifiers are sent
|
|
159
|
+
variables = isCustomPrimaryKey
|
|
160
|
+
? tslib_1.__spread([primaryKeyFieldName], sortKeyFieldNames).reduce(function (acc, fieldName) {
|
|
161
|
+
acc[fieldName] = arg[fieldName];
|
|
162
|
+
return acc;
|
|
163
|
+
}, {})
|
|
164
|
+
: (_a = {}, _a[primaryKeyFieldName] = arg[primaryKeyFieldName], _a);
|
|
165
|
+
if (operation === 'DELETE') {
|
|
166
|
+
variables = { input: variables };
|
|
167
|
+
}
|
|
168
|
+
break;
|
|
169
|
+
case 'LIST':
|
|
170
|
+
break;
|
|
171
|
+
default:
|
|
172
|
+
var exhaustiveCheck = operation;
|
|
173
|
+
throw new Error("Unhandled operation case: " + exhaustiveCheck);
|
|
174
|
+
}
|
|
175
|
+
return variables;
|
|
176
|
+
}
|
|
26
177
|
exports.API = new APIClass(null);
|
|
27
178
|
core_1.Amplify.register(exports.API);
|
|
28
179
|
//# sourceMappingURL=API.js.map
|
package/lib/API.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"API.js","sourceRoot":"","sources":["../src/API.ts"],"names":[],"mappings":";;;AAIA,0CAAqE;AAGrE,uDAA2D;AAE3D,IAAM,MAAM,GAAG,IAAI,oBAAM,CAAC,KAAK,CAAC,CAAC;AACjC;;;;GAIG;AACH;IAA8B,oCAAgB;IAA9C;;
|
|
1
|
+
{"version":3,"file":"API.js","sourceRoot":"","sources":["../src/API.ts"],"names":[],"mappings":";;;AAIA,0CAAqE;AAGrE,uDAA2D;AAE3D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G,EAAE,CAAC;YAC3B,MAAM;QACP,KAAK,QAAQ;YACZ,mCAAmC;YACnC,SAAS,GAAG;gBACX,KAAK,EAAE,MAAM,CAAC,WAAW,CACxB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,UAAC,EAAW;wBAAX,0BAAW,EAAV,iBAAS;oBAC7B,IAAA,yCAAU,CAAuB;oBAEzC,OAAO,CAAC,UAAU,CAAC;gBACpB,CAAC,CAAC,CACF;aACD,CAAC;YACF,MAAM;QACP,KAAK,MAAM,CAAC;QACZ,KAAK,QAAQ;YACZ,4BAA4B;YAC5B,SAAS,GAAG,kBAAkB;gBAC7B,CAAC,CAAC,kBAAC,mBAAmB,GAAK,iBAAiB,EAAE,MAAM,CAClD,UAAC,GAAG,EAAE,SAAS;oBACd,GAAG,CAAC,SAAS,CAAC,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC;oBAEhC,OAAO,GAAG,CAAC;gBACZ,CAAC,EACD,EAAE,CACD;gBACH,CAAC,WAAG,GAAC,mBAAmB,IAAG,GAAG,CAAC,mBAAmB,CAAC,KAAE,CAAC;YAEvD,IAAI,SAAS,KAAK,QAAQ,EAAE;gBAC3B,SAAS,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;aACjC;YACD,MAAM;QACP,KAAK,MAAM;YACV,MAAM;QACP;YACC,IAAM,eAAe,GAAU,SAAS,CAAC;YACzC,MAAM,IAAI,KAAK,CAAC,+BAA6B,eAAiB,CAAC,CAAC;KACjE;IAED,OAAO,SAAS,CAAC;AAClB,CAAC;AAmBY,QAAA,GAAG,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC;AACtC,cAAO,CAAC,QAAQ,CAAC,WAAG,CAAC,CAAC"}
|
package/lib-esm/API.d.ts
CHANGED
|
@@ -23,5 +23,48 @@ export declare class APIClass extends InternalAPIClass {
|
|
|
23
23
|
provider: AWSAppSyncRealTimeProvider;
|
|
24
24
|
value: GraphQLResult<T>;
|
|
25
25
|
}> : Promise<GraphQLResult<any>> | Observable<object>;
|
|
26
|
+
/**
|
|
27
|
+
* Generates an API client that can work with models or raw GraphQL
|
|
28
|
+
*/
|
|
29
|
+
generateClient<T = never>(): V6Client<T>;
|
|
26
30
|
}
|
|
31
|
+
declare const graphQLOperationsInfo: {
|
|
32
|
+
CREATE: {
|
|
33
|
+
operationPrefix: "create";
|
|
34
|
+
usePlural: boolean;
|
|
35
|
+
};
|
|
36
|
+
READ: {
|
|
37
|
+
operationPrefix: "get";
|
|
38
|
+
usePlural: boolean;
|
|
39
|
+
};
|
|
40
|
+
UPDATE: {
|
|
41
|
+
operationPrefix: "update";
|
|
42
|
+
usePlural: boolean;
|
|
43
|
+
};
|
|
44
|
+
DELETE: {
|
|
45
|
+
operationPrefix: "delete";
|
|
46
|
+
usePlural: boolean;
|
|
47
|
+
};
|
|
48
|
+
LIST: {
|
|
49
|
+
operationPrefix: "list";
|
|
50
|
+
usePlural: boolean;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
declare type ModelOperation = keyof typeof graphQLOperationsInfo;
|
|
54
|
+
declare type OperationPrefix = (typeof graphQLOperationsInfo)[ModelOperation]['operationPrefix'];
|
|
55
|
+
declare type FilteredKeys<T> = {
|
|
56
|
+
[P in keyof T]: T[P] extends never ? never : P;
|
|
57
|
+
}[keyof T];
|
|
58
|
+
declare type ExcludeNeverFields<O> = {
|
|
59
|
+
[K in FilteredKeys<O>]: O[K];
|
|
60
|
+
};
|
|
61
|
+
declare type V6Client<T = never> = ExcludeNeverFields<{
|
|
62
|
+
graphql: typeof APIClass.prototype.graphql;
|
|
63
|
+
models: {
|
|
64
|
+
[K in keyof T]: {
|
|
65
|
+
[K in OperationPrefix]: (...args: any[]) => Promise<any>;
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
}>;
|
|
27
69
|
export declare const API: APIClass;
|
|
70
|
+
export {};
|
package/lib-esm/API.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { __extends } from "tslib";
|
|
1
|
+
import { __awaiter, __extends, __generator, __read, __spread, __values } from "tslib";
|
|
2
2
|
import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
|
|
3
3
|
import { InternalAPIClass } from './internals/InternalAPI';
|
|
4
4
|
var logger = new Logger('API');
|
|
@@ -18,9 +18,160 @@ var APIClass = /** @class */ (function (_super) {
|
|
|
18
18
|
APIClass.prototype.graphql = function (options, additionalHeaders) {
|
|
19
19
|
return _super.prototype.graphql.call(this, options, additionalHeaders);
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Generates an API client that can work with models or raw GraphQL
|
|
23
|
+
*/
|
|
24
|
+
APIClass.prototype.generateClient = function () {
|
|
25
|
+
var e_1, _a;
|
|
26
|
+
var _this = this;
|
|
27
|
+
var config = _super.prototype.configure.call(this, {});
|
|
28
|
+
var modelIntrospection = config.modelIntrospection;
|
|
29
|
+
var client = {
|
|
30
|
+
graphql: this.graphql.bind(this),
|
|
31
|
+
models: {},
|
|
32
|
+
};
|
|
33
|
+
var _loop_1 = function (model) {
|
|
34
|
+
var name_1 = model.name;
|
|
35
|
+
client.models[name_1] = {};
|
|
36
|
+
Object.entries(graphQLOperationsInfo).forEach(function (_a) {
|
|
37
|
+
var _b = __read(_a, 2), key = _b[0], operationPrefix = _b[1].operationPrefix;
|
|
38
|
+
var operation = key;
|
|
39
|
+
// e.g. clients.models.Todo.update
|
|
40
|
+
client.models[name_1][operationPrefix] = function (arg) { return __awaiter(_this, void 0, void 0, function () {
|
|
41
|
+
var query, variables;
|
|
42
|
+
return __generator(this, function (_a) {
|
|
43
|
+
query = generateGraphQLDocument(model, operation);
|
|
44
|
+
variables = buildGraphQLVariables(model, operation, arg);
|
|
45
|
+
return [2 /*return*/, this.graphql({
|
|
46
|
+
query: query,
|
|
47
|
+
variables: variables,
|
|
48
|
+
})];
|
|
49
|
+
});
|
|
50
|
+
}); };
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
try {
|
|
54
|
+
for (var _b = __values(Object.values(modelIntrospection.models)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
55
|
+
var model = _c.value;
|
|
56
|
+
_loop_1(model);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
60
|
+
finally {
|
|
61
|
+
try {
|
|
62
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
63
|
+
}
|
|
64
|
+
finally { if (e_1) throw e_1.error; }
|
|
65
|
+
}
|
|
66
|
+
return client;
|
|
67
|
+
};
|
|
21
68
|
return APIClass;
|
|
22
69
|
}(InternalAPIClass));
|
|
23
70
|
export { APIClass };
|
|
71
|
+
var graphQLOperationsInfo = {
|
|
72
|
+
CREATE: { operationPrefix: 'create', usePlural: false },
|
|
73
|
+
READ: { operationPrefix: 'get', usePlural: false },
|
|
74
|
+
UPDATE: { operationPrefix: 'update', usePlural: false },
|
|
75
|
+
DELETE: { operationPrefix: 'delete', usePlural: false },
|
|
76
|
+
LIST: { operationPrefix: 'list', usePlural: true },
|
|
77
|
+
};
|
|
78
|
+
var graphQLDocumentsCache = new Map();
|
|
79
|
+
function generateGraphQLDocument(modelDefinition, modelOperation) {
|
|
80
|
+
var _a;
|
|
81
|
+
var _b, _c;
|
|
82
|
+
var name = modelDefinition.name, pluralName = modelDefinition.pluralName, fields = modelDefinition.fields, _d = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _d.isCustomPrimaryKey, primaryKeyFieldName = _d.primaryKeyFieldName, sortKeyFieldNames = _d.sortKeyFieldNames;
|
|
83
|
+
var _e = graphQLOperationsInfo[modelOperation], operationPrefix = _e.operationPrefix, usePlural = _e.usePlural;
|
|
84
|
+
var fromCache = (_b = graphQLDocumentsCache.get(name)) === null || _b === void 0 ? void 0 : _b.get(modelOperation);
|
|
85
|
+
if (fromCache !== undefined) {
|
|
86
|
+
return fromCache;
|
|
87
|
+
}
|
|
88
|
+
if (!graphQLDocumentsCache.has(name)) {
|
|
89
|
+
graphQLDocumentsCache.set(name, new Map());
|
|
90
|
+
}
|
|
91
|
+
var graphQLFieldName = "" + operationPrefix + (usePlural ? pluralName : name);
|
|
92
|
+
var graphQLOperationType;
|
|
93
|
+
var graphQLSelectionSet;
|
|
94
|
+
var graphQLArguments;
|
|
95
|
+
var selectionSetFields = Object.values(fields)
|
|
96
|
+
.map(function (_a) {
|
|
97
|
+
var type = _a.type, name = _a.name;
|
|
98
|
+
return typeof type === 'string' && name;
|
|
99
|
+
}) // Only scalars for now
|
|
100
|
+
.filter(Boolean)
|
|
101
|
+
.join(' ');
|
|
102
|
+
switch (modelOperation) {
|
|
103
|
+
case 'CREATE':
|
|
104
|
+
case 'UPDATE':
|
|
105
|
+
case 'DELETE':
|
|
106
|
+
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
|
|
107
|
+
input: "" + (operationPrefix.charAt(0).toLocaleUpperCase() +
|
|
108
|
+
operationPrefix.slice(1)) + name + "Input!",
|
|
109
|
+
});
|
|
110
|
+
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'mutation');
|
|
111
|
+
case 'READ':
|
|
112
|
+
graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = isCustomPrimaryKey
|
|
113
|
+
? __spread([primaryKeyFieldName], sortKeyFieldNames).reduce(function (acc, fieldName) {
|
|
114
|
+
acc[fieldName] = fields[fieldName].type;
|
|
115
|
+
return acc;
|
|
116
|
+
}, {})
|
|
117
|
+
: (_a = {},
|
|
118
|
+
_a[primaryKeyFieldName] = fields[primaryKeyFieldName].type + "!",
|
|
119
|
+
_a));
|
|
120
|
+
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
|
|
121
|
+
case 'LIST':
|
|
122
|
+
graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'query');
|
|
123
|
+
graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { " + selectionSetFields + " }");
|
|
124
|
+
}
|
|
125
|
+
var graphQLDocument = "" + graphQLOperationType + (graphQLArguments
|
|
126
|
+
? "(" + Object.entries(graphQLArguments).map(function (_a) {
|
|
127
|
+
var _b = __read(_a, 2), fieldName = _b[0], type = _b[1];
|
|
128
|
+
return "$" + fieldName + ": " + type;
|
|
129
|
+
}) + ")"
|
|
130
|
+
: '') + " { " + graphQLFieldName + (graphQLArguments
|
|
131
|
+
? "(" + Object.keys(graphQLArguments).map(function (fieldName) { return fieldName + ": $" + fieldName; }) + ")"
|
|
132
|
+
: '') + " { " + graphQLSelectionSet + " } }";
|
|
133
|
+
(_c = graphQLDocumentsCache.get(name)) === null || _c === void 0 ? void 0 : _c.set(modelOperation, graphQLDocument);
|
|
134
|
+
return graphQLDocument;
|
|
135
|
+
}
|
|
136
|
+
function buildGraphQLVariables(modelDefinition, operation, arg) {
|
|
137
|
+
var _a;
|
|
138
|
+
var fields = modelDefinition.fields, _b = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _b.isCustomPrimaryKey, primaryKeyFieldName = _b.primaryKeyFieldName, sortKeyFieldNames = _b.sortKeyFieldNames;
|
|
139
|
+
var variables = {};
|
|
140
|
+
switch (operation) {
|
|
141
|
+
case 'CREATE':
|
|
142
|
+
variables = { input: arg };
|
|
143
|
+
break;
|
|
144
|
+
case 'UPDATE':
|
|
145
|
+
// readonly fields are not updated
|
|
146
|
+
variables = {
|
|
147
|
+
input: Object.fromEntries(Object.entries(arg).filter(function (_a) {
|
|
148
|
+
var _b = __read(_a, 1), fieldName = _b[0];
|
|
149
|
+
var isReadOnly = fields[fieldName].isReadOnly;
|
|
150
|
+
return !isReadOnly;
|
|
151
|
+
})),
|
|
152
|
+
};
|
|
153
|
+
break;
|
|
154
|
+
case 'READ':
|
|
155
|
+
case 'DELETE':
|
|
156
|
+
// only identifiers are sent
|
|
157
|
+
variables = isCustomPrimaryKey
|
|
158
|
+
? __spread([primaryKeyFieldName], sortKeyFieldNames).reduce(function (acc, fieldName) {
|
|
159
|
+
acc[fieldName] = arg[fieldName];
|
|
160
|
+
return acc;
|
|
161
|
+
}, {})
|
|
162
|
+
: (_a = {}, _a[primaryKeyFieldName] = arg[primaryKeyFieldName], _a);
|
|
163
|
+
if (operation === 'DELETE') {
|
|
164
|
+
variables = { input: variables };
|
|
165
|
+
}
|
|
166
|
+
break;
|
|
167
|
+
case 'LIST':
|
|
168
|
+
break;
|
|
169
|
+
default:
|
|
170
|
+
var exhaustiveCheck = operation;
|
|
171
|
+
throw new Error("Unhandled operation case: " + exhaustiveCheck);
|
|
172
|
+
}
|
|
173
|
+
return variables;
|
|
174
|
+
}
|
|
24
175
|
export var API = new APIClass(null);
|
|
25
176
|
Amplify.register(API);
|
|
26
177
|
//# sourceMappingURL=API.js.map
|
package/lib-esm/API.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"API.js","sourceRoot":"","sources":["../src/API.ts"],"names":[],"mappings":";AAIA,OAAO,EAAE,OAAO,EAAE,aAAa,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAGrE,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,IAAM,MAAM,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;AACjC;;;;GAIG;AACH;IAA8B,4BAAgB;IAA9C;;
|
|
1
|
+
{"version":3,"file":"API.js","sourceRoot":"","sources":["../src/API.ts"],"names":[],"mappings":";AAIA,OAAO,EAAE,OAAO,EAAE,aAAa,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAGrE,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,IAAM,MAAM,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;AACjC;;;;GAIG;AACH;IAA8B,4BAAgB;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AW,CACxB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,UAAC,EAAW;wBAAX,kBAAW,EAAV,iBAAS;oBAC7B,IAAA,yCAAU,CAAuB;oBAEzC,OAAO,CAAC,UAAU,CAAC;gBACpB,CAAC,CAAC,CACF;aACD,CAAC;YACF,MAAM;QACP,KAAK,MAAM,CAAC;QACZ,KAAK,QAAQ;YACZ,4BAA4B;YAC5B,SAAS,GAAG,kBAAkB;gBAC7B,CAAC,CAAC,UAAC,mBAAmB,GAAK,iBAAiB,EAAE,MAAM,CAClD,UAAC,GAAG,EAAE,SAAS;oBACd,GAAG,CAAC,SAAS,CAAC,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC;oBAEhC,OAAO,GAAG,CAAC;gBACZ,CAAC,EACD,EAAE,CACD;gBACH,CAAC,WAAG,GAAC,mBAAmB,IAAG,GAAG,CAAC,mBAAmB,CAAC,KAAE,CAAC;YAEvD,IAAI,SAAS,KAAK,QAAQ,EAAE;gBAC3B,SAAS,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;aACjC;YACD,MAAM;QACP,KAAK,MAAM;YACV,MAAM;QACP;YACC,IAAM,eAAe,GAAU,SAAS,CAAC;YACzC,MAAM,IAAI,KAAK,CAAC,+BAA6B,eAAiB,CAAC,CAAC;KACjE;IAED,OAAO,SAAS,CAAC;AAClB,CAAC;AAmBD,MAAM,CAAC,IAAM,GAAG,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC;AACtC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/api",
|
|
3
|
-
"version": "5.3.4-api-v6.
|
|
3
|
+
"version": "5.3.4-api-v6.13+5927d05d2",
|
|
4
4
|
"description": "Api category of aws-amplify",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib-esm/index.js",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"clean:size": "rimraf dual-publish-tmp tmp*",
|
|
36
36
|
"format": "echo \"Not implemented\"",
|
|
37
37
|
"lint": "tslint 'src/**/*.ts' && npm run ts-coverage",
|
|
38
|
-
"ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 91.
|
|
38
|
+
"ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 91.16"
|
|
39
39
|
},
|
|
40
40
|
"repository": {
|
|
41
41
|
"type": "git",
|
|
@@ -58,8 +58,8 @@
|
|
|
58
58
|
"internals"
|
|
59
59
|
],
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@aws-amplify/api-graphql": "3.4.4-api-v6.
|
|
62
|
-
"@aws-amplify/api-rest": "3.3.3-api-v6.
|
|
61
|
+
"@aws-amplify/api-graphql": "3.4.4-api-v6.13+5927d05d2",
|
|
62
|
+
"@aws-amplify/api-rest": "3.3.3-api-v6.13+5927d05d2",
|
|
63
63
|
"tslib": "^1.8.0"
|
|
64
64
|
},
|
|
65
65
|
"size-limit": [
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"name": "API (top-level class)",
|
|
68
68
|
"path": "./lib-esm/index.js",
|
|
69
69
|
"import": "{ Amplify, API }",
|
|
70
|
-
"limit": "
|
|
70
|
+
"limit": "95 kB"
|
|
71
71
|
}
|
|
72
72
|
],
|
|
73
73
|
"jest": {
|
|
@@ -114,5 +114,5 @@
|
|
|
114
114
|
"lib-esm"
|
|
115
115
|
]
|
|
116
116
|
},
|
|
117
|
-
"gitHead": "
|
|
117
|
+
"gitHead": "5927d05d267ab4d14badbb8133ee08596f1918d5"
|
|
118
118
|
}
|
package/src/API.ts
CHANGED
|
@@ -42,7 +42,224 @@ export class APIClass extends InternalAPIClass {
|
|
|
42
42
|
): Promise<GraphQLResult<any>> | Observable<object> {
|
|
43
43
|
return super.graphql(options, additionalHeaders);
|
|
44
44
|
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Generates an API client that can work with models or raw GraphQL
|
|
48
|
+
*/
|
|
49
|
+
generateClient<T = never>(): V6Client<T> {
|
|
50
|
+
const config = super.configure({});
|
|
51
|
+
|
|
52
|
+
const { modelIntrospection } = config;
|
|
53
|
+
|
|
54
|
+
const client: V6Client<any> = {
|
|
55
|
+
graphql: this.graphql.bind(this),
|
|
56
|
+
models: {},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
for (const model of Object.values(modelIntrospection.models)) {
|
|
60
|
+
const { name } = model as any;
|
|
61
|
+
|
|
62
|
+
client.models[name] = {} as any;
|
|
63
|
+
|
|
64
|
+
Object.entries(graphQLOperationsInfo).forEach(
|
|
65
|
+
([key, { operationPrefix }]) => {
|
|
66
|
+
const operation = key as ModelOperation;
|
|
67
|
+
|
|
68
|
+
// e.g. clients.models.Todo.update
|
|
69
|
+
client.models[name][operationPrefix] = async (arg: any) => {
|
|
70
|
+
const query = generateGraphQLDocument(model, operation);
|
|
71
|
+
const variables = buildGraphQLVariables(model, operation, arg);
|
|
72
|
+
|
|
73
|
+
return this.graphql({
|
|
74
|
+
query,
|
|
75
|
+
variables,
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return client as V6Client<T>;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const graphQLOperationsInfo = {
|
|
87
|
+
CREATE: { operationPrefix: 'create' as const, usePlural: false },
|
|
88
|
+
READ: { operationPrefix: 'get' as const, usePlural: false },
|
|
89
|
+
UPDATE: { operationPrefix: 'update' as const, usePlural: false },
|
|
90
|
+
DELETE: { operationPrefix: 'delete' as const, usePlural: false },
|
|
91
|
+
LIST: { operationPrefix: 'list' as const, usePlural: true },
|
|
92
|
+
};
|
|
93
|
+
type ModelOperation = keyof typeof graphQLOperationsInfo;
|
|
94
|
+
type OperationPrefix =
|
|
95
|
+
(typeof graphQLOperationsInfo)[ModelOperation]['operationPrefix'];
|
|
96
|
+
|
|
97
|
+
const graphQLDocumentsCache = new Map<string, Map<ModelOperation, string>>();
|
|
98
|
+
|
|
99
|
+
function generateGraphQLDocument(
|
|
100
|
+
modelDefinition: any,
|
|
101
|
+
modelOperation: ModelOperation
|
|
102
|
+
): string {
|
|
103
|
+
const {
|
|
104
|
+
name,
|
|
105
|
+
pluralName,
|
|
106
|
+
fields,
|
|
107
|
+
primaryKeyInfo: {
|
|
108
|
+
isCustomPrimaryKey,
|
|
109
|
+
primaryKeyFieldName,
|
|
110
|
+
sortKeyFieldNames,
|
|
111
|
+
},
|
|
112
|
+
} = modelDefinition;
|
|
113
|
+
const { operationPrefix, usePlural } = graphQLOperationsInfo[modelOperation];
|
|
114
|
+
|
|
115
|
+
const fromCache = graphQLDocumentsCache.get(name)?.get(modelOperation);
|
|
116
|
+
|
|
117
|
+
if (fromCache !== undefined) {
|
|
118
|
+
return fromCache;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!graphQLDocumentsCache.has(name)) {
|
|
122
|
+
graphQLDocumentsCache.set(name, new Map());
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const graphQLFieldName = `${operationPrefix}${usePlural ? pluralName : name}`;
|
|
126
|
+
let graphQLOperationType: 'mutation' | 'query' | undefined;
|
|
127
|
+
let graphQLSelectionSet: string | undefined;
|
|
128
|
+
let graphQLArguments: Record<string, any> | undefined;
|
|
129
|
+
|
|
130
|
+
const selectionSetFields = Object.values<any>(fields)
|
|
131
|
+
.map(({ type, name }) => typeof type === 'string' && name) // Only scalars for now
|
|
132
|
+
.filter(Boolean)
|
|
133
|
+
.join(' ');
|
|
134
|
+
|
|
135
|
+
switch (modelOperation) {
|
|
136
|
+
case 'CREATE':
|
|
137
|
+
case 'UPDATE':
|
|
138
|
+
case 'DELETE':
|
|
139
|
+
graphQLArguments ??
|
|
140
|
+
(graphQLArguments = {
|
|
141
|
+
input: `${
|
|
142
|
+
operationPrefix.charAt(0).toLocaleUpperCase() +
|
|
143
|
+
operationPrefix.slice(1)
|
|
144
|
+
}${name}Input!`,
|
|
145
|
+
});
|
|
146
|
+
graphQLOperationType ?? (graphQLOperationType = 'mutation');
|
|
147
|
+
case 'READ':
|
|
148
|
+
graphQLArguments ??
|
|
149
|
+
(graphQLArguments = isCustomPrimaryKey
|
|
150
|
+
? [primaryKeyFieldName, ...sortKeyFieldNames].reduce(
|
|
151
|
+
(acc, fieldName) => {
|
|
152
|
+
acc[fieldName] = fields[fieldName].type;
|
|
153
|
+
|
|
154
|
+
return acc;
|
|
155
|
+
},
|
|
156
|
+
{}
|
|
157
|
+
)
|
|
158
|
+
: {
|
|
159
|
+
[primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}!`,
|
|
160
|
+
});
|
|
161
|
+
graphQLSelectionSet ?? (graphQLSelectionSet = selectionSetFields);
|
|
162
|
+
case 'LIST':
|
|
163
|
+
graphQLOperationType ?? (graphQLOperationType = 'query');
|
|
164
|
+
graphQLSelectionSet ??
|
|
165
|
+
(graphQLSelectionSet = `items { ${selectionSetFields} }`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const graphQLDocument = `${graphQLOperationType}${
|
|
169
|
+
graphQLArguments
|
|
170
|
+
? `(${Object.entries(graphQLArguments).map(
|
|
171
|
+
([fieldName, type]) => `\$${fieldName}: ${type}`
|
|
172
|
+
)})`
|
|
173
|
+
: ''
|
|
174
|
+
} { ${graphQLFieldName}${
|
|
175
|
+
graphQLArguments
|
|
176
|
+
? `(${Object.keys(graphQLArguments).map(
|
|
177
|
+
fieldName => `${fieldName}: \$${fieldName}`
|
|
178
|
+
)})`
|
|
179
|
+
: ''
|
|
180
|
+
} { ${graphQLSelectionSet} } }`;
|
|
181
|
+
|
|
182
|
+
graphQLDocumentsCache.get(name)?.set(modelOperation, graphQLDocument);
|
|
183
|
+
|
|
184
|
+
return graphQLDocument;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function buildGraphQLVariables(
|
|
188
|
+
modelDefinition: any,
|
|
189
|
+
operation: ModelOperation,
|
|
190
|
+
arg: any
|
|
191
|
+
): object {
|
|
192
|
+
const {
|
|
193
|
+
fields,
|
|
194
|
+
primaryKeyInfo: {
|
|
195
|
+
isCustomPrimaryKey,
|
|
196
|
+
primaryKeyFieldName,
|
|
197
|
+
sortKeyFieldNames,
|
|
198
|
+
},
|
|
199
|
+
} = modelDefinition;
|
|
200
|
+
|
|
201
|
+
let variables = {};
|
|
202
|
+
|
|
203
|
+
switch (operation) {
|
|
204
|
+
case 'CREATE':
|
|
205
|
+
variables = { input: arg };
|
|
206
|
+
break;
|
|
207
|
+
case 'UPDATE':
|
|
208
|
+
// readonly fields are not updated
|
|
209
|
+
variables = {
|
|
210
|
+
input: Object.fromEntries(
|
|
211
|
+
Object.entries(arg).filter(([fieldName]) => {
|
|
212
|
+
const { isReadOnly } = fields[fieldName];
|
|
213
|
+
|
|
214
|
+
return !isReadOnly;
|
|
215
|
+
})
|
|
216
|
+
),
|
|
217
|
+
};
|
|
218
|
+
break;
|
|
219
|
+
case 'READ':
|
|
220
|
+
case 'DELETE':
|
|
221
|
+
// only identifiers are sent
|
|
222
|
+
variables = isCustomPrimaryKey
|
|
223
|
+
? [primaryKeyFieldName, ...sortKeyFieldNames].reduce(
|
|
224
|
+
(acc, fieldName) => {
|
|
225
|
+
acc[fieldName] = arg[fieldName];
|
|
226
|
+
|
|
227
|
+
return acc;
|
|
228
|
+
},
|
|
229
|
+
{}
|
|
230
|
+
)
|
|
231
|
+
: { [primaryKeyFieldName]: arg[primaryKeyFieldName] };
|
|
232
|
+
|
|
233
|
+
if (operation === 'DELETE') {
|
|
234
|
+
variables = { input: variables };
|
|
235
|
+
}
|
|
236
|
+
break;
|
|
237
|
+
case 'LIST':
|
|
238
|
+
break;
|
|
239
|
+
default:
|
|
240
|
+
const exhaustiveCheck: never = operation;
|
|
241
|
+
throw new Error(`Unhandled operation case: ${exhaustiveCheck}`);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return variables;
|
|
45
245
|
}
|
|
46
246
|
|
|
247
|
+
type FilteredKeys<T> = {
|
|
248
|
+
[P in keyof T]: T[P] extends never ? never : P;
|
|
249
|
+
}[keyof T];
|
|
250
|
+
type ExcludeNeverFields<O> = {
|
|
251
|
+
[K in FilteredKeys<O>]: O[K];
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// If no T is passed, ExcludeNeverFields removes "models" from the client
|
|
255
|
+
type V6Client<T = never> = ExcludeNeverFields<{
|
|
256
|
+
graphql: typeof APIClass.prototype.graphql;
|
|
257
|
+
models: {
|
|
258
|
+
[K in keyof T]: {
|
|
259
|
+
[K in OperationPrefix]: (...args: any[]) => Promise<any>;
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
}>;
|
|
263
|
+
|
|
47
264
|
export const API = new APIClass(null);
|
|
48
265
|
Amplify.register(API);
|