@grapi/server
Version:
Grapi Schema Generator For GraphQL Server
101 lines (100 loc) • 4.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSdlField = exports.parseWrappedType = exports.findTypeInDocumentAst = exports.parseDirectiveNode = exports.parseDirectiveInput = void 0;
const graphql_1 = require("graphql");
const lodash_1 = require("../lodash");
const field_1 = require("./field");
const inputValue_1 = require("./inputValue");
const isSpecifiedScalar = (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);
};
const parseDirectiveInput = (node) => {
const inputValues = {
[graphql_1.Kind.INT]: () => new inputValue_1.IntValue({ value: parseInt(node.value) }),
[graphql_1.Kind.FLOAT]: () => new inputValue_1.FloatValue({ value: parseFloat(node.value) }),
[graphql_1.Kind.STRING]: () => new inputValue_1.StringValue({ value: node.value }),
[graphql_1.Kind.BOOLEAN]: () => new inputValue_1.BooleanValue({ value: node.value }),
[graphql_1.Kind.ENUM]: () => new inputValue_1.EnumValue({ value: node.value }),
[graphql_1.Kind.OBJECT]: () => new inputValue_1.ObjectValue({
fields: (0, lodash_1.reduce)(node.fields, (result, field) => {
result[field.name.value] = (0, exports.parseDirectiveInput)(field.value);
return result;
}, {})
}),
[graphql_1.Kind.LIST]: () => new inputValue_1.ListValue({
values: node.values.map(nestedNode => (0, exports.parseDirectiveInput)(nestedNode))
}),
[graphql_1.Kind.NULL]: () => new inputValue_1.NullValue()
};
const inputValue = inputValues[node.kind];
if (!inputValue) {
throw new Error(`not supported type in directive parsing: ${node.kind}`);
}
return inputValue();
};
exports.parseDirectiveInput = parseDirectiveInput;
const parseDirectiveNode = (node) => {
return {
args: (0, lodash_1.reduce)(node.arguments || [], (result, argNode) => {
result[argNode.name.value] = (0, exports.parseDirectiveInput)(argNode.value);
return result;
}, {}),
};
};
exports.parseDirectiveNode = parseDirectiveNode;
const findTypeInDocumentAst = (node, name) => {
const foundNode = node.definitions.find((defNode) => {
return defNode.name.value === name;
});
return foundNode ? foundNode.kind : null;
};
exports.findTypeInDocumentAst = findTypeInDocumentAst;
const parseWrappedType = (node, typeWrapped = []) => {
const wrappedTypes = {
[graphql_1.Kind.NON_NULL_TYPE]: () => (0, exports.parseWrappedType)(node.type, typeWrapped.concat(graphql_1.Kind.NON_NULL_TYPE)),
[graphql_1.Kind.LIST_TYPE]: () => (0, exports.parseWrappedType)(node.type, typeWrapped.concat(graphql_1.Kind.LIST_TYPE)),
[graphql_1.Kind.NAMED_TYPE]: () => ({ type: node.name.value, wrapped: typeWrapped })
};
return wrappedTypes[node.kind]();
};
exports.parseWrappedType = parseWrappedType;
const createSdlField = (documentNode, node, getSdlNamedType) => {
const typeNode = node.type;
const { type, wrapped } = (0, exports.parseWrappedType)(typeNode);
const nonNull = wrapped[0] === graphql_1.Kind.NON_NULL_TYPE;
const list = (wrapped[0] === graphql_1.Kind.LIST_TYPE || wrapped[1] === graphql_1.Kind.LIST_TYPE);
const itemNonNull = (list && (0, lodash_1.last)(wrapped) === graphql_1.Kind.NON_NULL_TYPE);
const directives = (0, lodash_1.reduce)(node.directives, (result, directiveNode) => {
result[directiveNode.name.value] = (0, exports.parseDirectiveNode)(directiveNode);
return result;
}, {});
const fieldConfigs = { typename: type, nonNull, list, itemNonNull, directives };
if (isSpecifiedScalar(type)) {
return new field_1.ScalarField(fieldConfigs);
}
const nodeType = (0, exports.findTypeInDocumentAst)(documentNode, type);
if (!nodeType) {
throw new Error(`type of "${type}" not found in document`);
}
const nodeTypes = {
[graphql_1.Kind.SCALAR_TYPE_DEFINITION]: () => new field_1.CustomScalarField(fieldConfigs),
[graphql_1.Kind.ENUM_TYPE_DEFINITION]: () => {
const enumField = new field_1.EnumField(fieldConfigs);
enumField.setEnumType(() => getSdlNamedType(enumField.getTypeName()));
return enumField;
},
[graphql_1.Kind.OBJECT_TYPE_DEFINITION]: () => {
const field = new field_1.ObjectField(fieldConfigs);
field.setObjectType(() => getSdlNamedType(field.getTypeName()));
return field;
}
};
return nodeTypes[nodeType]();
};
exports.createSdlField = createSdlField;