@terabits/grapi
Version:
Grapi Schema Generator For GraphQL Server
107 lines (106 loc) • 5.01 kB
JavaScript
;
exports.__esModule = true;
exports.createSdlField = exports.parseWrappedType = exports.findTypeInDocumentAst = exports.parseDirectiveNode = exports.parseDirectiveInput = void 0;
var graphql_1 = require("graphql");
var lodash_1 = require("../lodash");
var field_1 = require("./field");
var inputValue_1 = require("./inputValue");
var isSpecifiedScalar = function (type) {
if (!type) {
return false;
}
return (type === graphql_1.GraphQLString.name ||
type === graphql_1.GraphQLInt.name ||
type === graphql_1.GraphQLFloat.name ||
type === graphql_1.GraphQLBoolean.name ||
type === graphql_1.GraphQLID.name);
};
var parseDirectiveInput = function (node) {
switch (node.kind) {
case graphql_1.Kind.INT:
return new inputValue_1.IntValue({ value: parseInt(node.value, 10) });
case graphql_1.Kind.FLOAT:
return new inputValue_1.FloatValue({ value: parseFloat(node.value) });
case graphql_1.Kind.STRING:
return new inputValue_1.StringValue({ value: node.value });
case graphql_1.Kind.BOOLEAN:
return new inputValue_1.BooleanValue({ value: node.value });
case graphql_1.Kind.ENUM:
return new inputValue_1.EnumValue({ value: node.value });
case graphql_1.Kind.NULL:
return new inputValue_1.NullValue();
case graphql_1.Kind.LIST:
return new inputValue_1.ListValue({
values: node.values.map(function (nestedNode) { return (0, exports.parseDirectiveInput)(nestedNode); })
});
case graphql_1.Kind.OBJECT:
return new inputValue_1.ObjectValue({
fields: (0, lodash_1.reduce)(node.fields, function (result, field) {
result[field.name.value] = (0, exports.parseDirectiveInput)(field.value);
return result;
}, {})
});
default:
throw new Error("not supported type in directive parsing: ".concat(node.kind));
}
};
exports.parseDirectiveInput = parseDirectiveInput;
var parseDirectiveNode = function (node) {
return {
args: (0, lodash_1.reduce)(node.arguments, function (result, argNode) {
result[argNode.name.value] = (0, exports.parseDirectiveInput)(argNode.value);
return result;
}, {})
};
};
exports.parseDirectiveNode = parseDirectiveNode;
var findTypeInDocumentAst = function (node, name) {
var foundNode = node.definitions.find(function (defNode) {
return defNode.name.value === name;
});
return foundNode ? foundNode.kind : null;
};
exports.findTypeInDocumentAst = findTypeInDocumentAst;
var parseWrappedType = function (node, typeWrapped) {
if (typeWrapped === void 0) { typeWrapped = []; }
if (node.kind === graphql_1.Kind.NON_NULL_TYPE) {
return (0, exports.parseWrappedType)(node.type, typeWrapped.concat(graphql_1.Kind.NON_NULL_TYPE));
}
if (node.kind === graphql_1.Kind.LIST_TYPE) {
return (0, exports.parseWrappedType)(node.type, typeWrapped.concat(graphql_1.Kind.LIST_TYPE));
}
return { type: node.name.value, wrapped: typeWrapped };
};
exports.parseWrappedType = parseWrappedType;
var createSdlField = function (documentNode, node, getSdlNamedType) {
var typeNode = node.type;
var _a = (0, exports.parseWrappedType)(typeNode), type = _a.type, wrapped = _a.wrapped;
var nonNull = wrapped[0] === graphql_1.Kind.NON_NULL_TYPE;
var list = (wrapped[0] === graphql_1.Kind.LIST_TYPE || wrapped[1] === graphql_1.Kind.LIST_TYPE);
var itemNonNull = (list && (0, lodash_1.last)(wrapped) === graphql_1.Kind.NON_NULL_TYPE);
var directives = (0, lodash_1.reduce)(node.directives, function (result, directiveNode) {
result[directiveNode.name.value] = (0, exports.parseDirectiveNode)(directiveNode);
return result;
}, {});
var fieldConfigs = { typename: type, nonNull: nonNull, list: list, itemNonNull: itemNonNull, directives: directives };
if (isSpecifiedScalar(type)) {
return new field_1.ScalarField(fieldConfigs);
}
var nodeType = (0, exports.findTypeInDocumentAst)(documentNode, type);
if (!nodeType) {
throw new Error("type of \"".concat(type, "\" not found in document"));
}
switch (nodeType) {
case graphql_1.Kind.SCALAR_TYPE_DEFINITION:
return new field_1.CustomScalarField(fieldConfigs);
case graphql_1.Kind.ENUM_TYPE_DEFINITION:
var enumField_1 = new field_1.EnumField(fieldConfigs);
enumField_1.setEnumType(function () { return getSdlNamedType(enumField_1.getTypeName()); });
return enumField_1;
case graphql_1.Kind.OBJECT_TYPE_DEFINITION:
var field_2 = new field_1.ObjectField(fieldConfigs);
field_2.setObjectType(function () { return getSdlNamedType(field_2.getTypeName()); });
return field_2;
}
};
exports.createSdlField = createSdlField;