@graphql-tools/utils
Version:
Common package containing utils and types for GraphQL tools
64 lines (63 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNamedStub = createNamedStub;
exports.createStub = createStub;
exports.isNamedStub = isNamedStub;
exports.getBuiltInForStub = getBuiltInForStub;
const graphql_1 = require("graphql");
function createNamedStub(name, type) {
let constructor;
if (type === 'object') {
constructor = graphql_1.GraphQLObjectType;
}
else if (type === 'interface') {
constructor = graphql_1.GraphQLInterfaceType;
}
else {
constructor = graphql_1.GraphQLInputObjectType;
}
return new constructor({
name,
fields: {
_fake: {
type: graphql_1.GraphQLString,
},
},
});
}
function getSpecifiedScalar(name) {
return graphql_1.specifiedScalarTypes.find(type => type.name === name);
}
function createStub(node, type) {
switch (node.kind) {
case graphql_1.Kind.LIST_TYPE:
return new graphql_1.GraphQLList(createStub(node.type, type));
case graphql_1.Kind.NON_NULL_TYPE:
// FIXME: graphql@17 type issue
return new graphql_1.GraphQLNonNull(createStub(node.type, type));
default: {
const specified = getSpecifiedScalar(node.name.value);
if (specified) {
return specified;
}
if (type === 'output') {
return createNamedStub(node.name.value, 'object');
}
return createNamedStub(node.name.value, 'input');
}
}
}
function isNamedStub(type) {
if ('getFields' in type) {
const fields = type.getFields();
// eslint-disable-next-line no-unreachable-loop
for (const fieldName in fields) {
const field = fields[fieldName];
return field.name === '_fake';
}
}
return false;
}
function getBuiltInForStub(type) {
return getSpecifiedScalar(type.name) ?? type;
}