UNPKG

1.91 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const graphql_tools_fork_1 = require("graphql-tools-fork");
4exports.renameTransform = async ({ schema, config }) => {
5 var _a, _b, _c;
6 const rootTypes = [
7 (_a = schema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name,
8 (_b = schema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name,
9 (_c = schema.getSubscriptionType()) === null || _c === void 0 ? void 0 : _c.name
10 ].filter(Boolean);
11 const transforms = [];
12 for (const change of config) {
13 const [fromTypeName, fromFieldName] = change.from.split('.');
14 const [toTypeName, toFieldName] = change.to.split('.');
15 const isRootType = rootTypes.includes(fromTypeName);
16 if (fromTypeName !== toTypeName) {
17 if (isRootType) {
18 transforms.push(new graphql_tools_fork_1.RenameRootTypes(t => (t === fromTypeName ? toTypeName : t)));
19 }
20 else {
21 transforms.push(new graphql_tools_fork_1.RenameTypes(t => (t === fromTypeName ? toTypeName : t)));
22 }
23 }
24 if (fromFieldName && toFieldName && fromFieldName !== toFieldName) {
25 if (isRootType) {
26 transforms.push(new graphql_tools_fork_1.RenameRootFields((typeName, fieldName) => typeName === toTypeName && fieldName === fromFieldName
27 ? toFieldName
28 : fieldName));
29 }
30 else {
31 transforms.push(new graphql_tools_fork_1.RenameObjectFields((typeName, fieldName) => typeName === toTypeName && fieldName === fromFieldName
32 ? toFieldName
33 : fieldName));
34 }
35 }
36 }
37 return graphql_tools_fork_1.transformSchema(schema, transforms);
38};
39exports.default = exports.renameTransform;
40//# sourceMappingURL=index.js.map
\No newline at end of file