@graphql-tools/utils
Version:
Common package containing utils and types for GraphQL tools
152 lines (151 loc) • 5.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renameType = renameType;
const graphql_1 = require("graphql");
function renameType(type, newTypeName) {
if ((0, graphql_1.isObjectType)(type)) {
return new graphql_1.GraphQLObjectType({
...type.toConfig(),
name: newTypeName,
astNode: type.astNode == null
? type.astNode
: {
...type.astNode,
name: {
...type.astNode.name,
value: newTypeName,
},
},
extensionASTNodes: type.extensionASTNodes == null
? type.extensionASTNodes
: type.extensionASTNodes.map(node => ({
...node,
name: {
...node.name,
value: newTypeName,
},
})),
});
}
else if ((0, graphql_1.isInterfaceType)(type)) {
return new graphql_1.GraphQLInterfaceType({
...type.toConfig(),
name: newTypeName,
astNode: type.astNode == null
? type.astNode
: {
...type.astNode,
name: {
...type.astNode.name,
value: newTypeName,
},
},
extensionASTNodes: type.extensionASTNodes == null
? type.extensionASTNodes
: type.extensionASTNodes.map(node => ({
...node,
name: {
...node.name,
value: newTypeName,
},
})),
});
}
else if ((0, graphql_1.isUnionType)(type)) {
return new graphql_1.GraphQLUnionType({
...type.toConfig(),
name: newTypeName,
astNode: type.astNode == null
? type.astNode
: {
...type.astNode,
name: {
...type.astNode.name,
value: newTypeName,
},
},
extensionASTNodes: type.extensionASTNodes == null
? type.extensionASTNodes
: type.extensionASTNodes.map(node => ({
...node,
name: {
...node.name,
value: newTypeName,
},
})),
});
}
else if ((0, graphql_1.isInputObjectType)(type)) {
return new graphql_1.GraphQLInputObjectType({
...type.toConfig(),
name: newTypeName,
astNode: type.astNode == null
? type.astNode
: {
...type.astNode,
name: {
...type.astNode.name,
value: newTypeName,
},
},
extensionASTNodes: type.extensionASTNodes == null
? type.extensionASTNodes
: type.extensionASTNodes.map(node => ({
...node,
name: {
...node.name,
value: newTypeName,
},
})),
});
}
else if ((0, graphql_1.isEnumType)(type)) {
return new graphql_1.GraphQLEnumType({
...type.toConfig(),
name: newTypeName,
astNode: type.astNode == null
? type.astNode
: {
...type.astNode,
name: {
...type.astNode.name,
value: newTypeName,
},
},
extensionASTNodes: type.extensionASTNodes == null
? type.extensionASTNodes
: type.extensionASTNodes.map(node => ({
...node,
name: {
...node.name,
value: newTypeName,
},
})),
});
}
else if ((0, graphql_1.isScalarType)(type)) {
return new graphql_1.GraphQLScalarType({
...type.toConfig(),
name: newTypeName,
astNode: type.astNode == null
? type.astNode
: {
...type.astNode,
name: {
...type.astNode.name,
value: newTypeName,
},
},
extensionASTNodes: type.extensionASTNodes == null
? type.extensionASTNodes
: type.extensionASTNodes.map(node => ({
...node,
name: {
...node.name,
value: newTypeName,
},
})),
});
}
throw new Error(`Unknown type ${type}.`);
}