graphql-compose
Version:
GraphQL schema builder from different data sources with middleware extensions.
315 lines • 11.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getObjectTypeDefinitionNode = getObjectTypeDefinitionNode;
exports.getInputObjectTypeDefinitionNode = getInputObjectTypeDefinitionNode;
exports.getEnumTypeDefinitionNode = getEnumTypeDefinitionNode;
exports.getInterfaceTypeDefinitionNode = getInterfaceTypeDefinitionNode;
exports.getScalarTypeDefinitionNode = getScalarTypeDefinitionNode;
exports.getUnionTypeDefinitionNode = getUnionTypeDefinitionNode;
exports.getDescriptionNode = getDescriptionNode;
exports.getDirectiveNodes = getDirectiveNodes;
exports.getInterfaceNodes = getInterfaceNodes;
exports.getTypeNode = getTypeNode;
exports.getArgumentsDefinitionNodes = getArgumentsDefinitionNodes;
exports.getFieldDefinitionNodes = getFieldDefinitionNodes;
exports.getInputValueDefinitionNodes = getInputValueDefinitionNodes;
exports.getNameNode = getNameNode;
exports.getEnumValueDefinitionNodes = getEnumValueDefinitionNodes;
exports.parseValueNode = parseValueNode;
const ThunkComposer_1 = require("../ThunkComposer");
const NonNullComposer_1 = require("../NonNullComposer");
const ListComposer_1 = require("../ListComposer");
const misc_1 = require("./misc");
const graphql_1 = require("graphql");
function getObjectTypeDefinitionNode(tc) {
return {
kind: graphql_1.Kind.OBJECT_TYPE_DEFINITION,
name: getNameNode(tc.getTypeName()),
description: getDescriptionNode(tc.getDescription()),
directives: getDirectiveNodes(tc.getDirectives(), tc.schemaComposer),
interfaces: getInterfaceNodes(tc.getInterfaces()),
fields: getFieldDefinitionNodes(tc),
};
}
function getInputObjectTypeDefinitionNode(tc) {
return {
kind: graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
name: getNameNode(tc.getTypeName()),
directives: getDirectiveNodes(tc.getDirectives(), tc.schemaComposer),
description: getDescriptionNode(tc.getDescription()),
fields: getInputValueDefinitionNodes(tc),
};
}
function getEnumTypeDefinitionNode(tc) {
return {
kind: graphql_1.Kind.ENUM_TYPE_DEFINITION,
name: getNameNode(tc.getTypeName()),
description: getDescriptionNode(tc.getDescription()),
directives: getDirectiveNodes(tc.getDirectives(), tc.schemaComposer),
values: getEnumValueDefinitionNodes(tc) || [],
};
}
function getInterfaceTypeDefinitionNode(tc) {
return {
kind: graphql_1.Kind.INTERFACE_TYPE_DEFINITION,
name: getNameNode(tc.getTypeName()),
description: getDescriptionNode(tc.getDescription()),
directives: getDirectiveNodes(tc.getDirectives(), tc.schemaComposer),
fields: getFieldDefinitionNodes(tc),
};
}
function getScalarTypeDefinitionNode(tc) {
return {
kind: graphql_1.Kind.SCALAR_TYPE_DEFINITION,
name: getNameNode(tc.getTypeName()),
description: getDescriptionNode(tc.getDescription()),
directives: getDirectiveNodes(tc.getDirectives(), tc.schemaComposer),
};
}
function getUnionTypeDefinitionNode(tc) {
return {
kind: graphql_1.Kind.UNION_TYPE_DEFINITION,
name: getNameNode(tc.getTypeName()),
description: getDescriptionNode(tc.getDescription()),
directives: getDirectiveNodes(tc.getDirectives(), tc.schemaComposer),
types: tc.getTypeNames().map((value) => ({
kind: graphql_1.Kind.NAMED_TYPE,
name: getNameNode(value),
})),
};
}
function getDescriptionNode(value) {
if (!value)
return;
return {
kind: graphql_1.Kind.STRING,
value,
};
}
function toValueNode(value) {
switch (typeof value) {
case 'string':
return { kind: graphql_1.Kind.STRING, value };
case 'number':
if (Number.isInteger(value))
return { kind: graphql_1.Kind.INT, value: value.toString() };
return { kind: graphql_1.Kind.FLOAT, value: value.toString() };
case 'boolean':
return { kind: graphql_1.Kind.BOOLEAN, value };
case 'object':
if (value === null) {
return { kind: graphql_1.Kind.NULL };
}
else if (Array.isArray(value)) {
return {
kind: graphql_1.Kind.LIST,
values: value.map((v) => toValueNode(v)),
};
}
else {
return {
kind: graphql_1.Kind.OBJECT,
fields: Object.keys(value).map((k) => ({
kind: graphql_1.Kind.OBJECT_FIELD,
name: getNameNode(k),
value: toValueNode(value[k]),
})),
};
}
default:
console.log(`Cannot determine astNode in toValueNode() method: ${(0, misc_1.inspect)(value)}`);
return { kind: graphql_1.Kind.NULL };
}
}
function getDirectiveArgumentNodes(data, directive) {
const keys = Object.keys(data);
if (!keys.length)
return;
const args = [];
keys.forEach((k) => {
var _a;
let argumentType;
if (directive) {
argumentType = (_a = directive.args.find((d) => d.name === k)) === null || _a === void 0 ? void 0 : _a.type;
}
const argNode = {
kind: graphql_1.Kind.ARGUMENT,
name: getNameNode(k),
value: argumentType
?
(0, graphql_1.astFromValue)(data[k], argumentType) || { kind: graphql_1.Kind.NULL }
:
toValueNode(data[k]),
};
args.push(argNode);
});
return args;
}
function getDirectiveNodes(values, sc) {
if (!values || !values.length)
return;
return values.map((v) => ({
kind: graphql_1.Kind.DIRECTIVE,
name: getNameNode(v.name),
arguments: v.args && getDirectiveArgumentNodes(v.args, sc._getDirective(v.name)),
}));
}
function getInterfaceNodes(ifaces) {
return ifaces
.map((iface) => {
if (!iface || !iface.getTypeName)
return;
return {
kind: graphql_1.Kind.NAMED_TYPE,
name: getNameNode(iface.getTypeName()),
};
})
.filter(Boolean);
}
function getTypeNode(atc) {
if (atc instanceof ThunkComposer_1.ThunkComposer) {
return getTypeNode(atc.ofType);
}
else if (atc instanceof ListComposer_1.ListComposer) {
const subType = getTypeNode(atc.ofType);
if (!subType)
return;
return {
kind: graphql_1.Kind.LIST_TYPE,
type: subType,
};
}
else if (atc instanceof NonNullComposer_1.NonNullComposer) {
const subType = getTypeNode(atc.ofType);
if (!subType)
return;
return {
kind: graphql_1.Kind.NON_NULL_TYPE,
type: subType,
};
}
else if (atc && atc.getTypeName) {
return {
kind: graphql_1.Kind.NAMED_TYPE,
name: getNameNode(atc.getTypeName()),
};
}
return undefined;
}
function getArgumentsDefinitionNodes(tc, fieldName) {
const argNames = tc.getFieldArgNames(fieldName);
if (!argNames.length)
return;
return argNames
.map((argName) => {
const ac = tc.getFieldArg(fieldName, argName);
const type = getTypeNode(ac.type);
if (!type)
return;
return {
kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
name: getNameNode(argName),
type,
description: getDescriptionNode(ac.description),
directives: getDirectiveNodes(tc.getFieldArgDirectives(fieldName, argName), tc.schemaComposer),
defaultValue: (ac.defaultValue !== undefined &&
astFromValueSafe(ac.defaultValue, tc.getFieldArgType(fieldName, argName))) ||
undefined,
};
})
.filter(Boolean);
}
function getFieldDefinitionNodes(tc) {
const fieldNames = tc.getFieldNames();
if (!fieldNames.length)
return;
return fieldNames
.map((fieldName) => {
const fc = tc.getField(fieldName);
const type = getTypeNode(fc.type);
if (!type)
return;
return {
kind: graphql_1.Kind.FIELD_DEFINITION,
name: getNameNode(fieldName),
type,
arguments: getArgumentsDefinitionNodes(tc, fieldName),
description: getDescriptionNode(fc.description),
directives: getDirectiveNodes(tc.getFieldDirectives(fieldName), tc.schemaComposer),
};
})
.filter(Boolean);
}
function getInputValueDefinitionNodes(tc) {
const fieldNames = tc.getFieldNames();
if (!fieldNames.length)
return;
return fieldNames
.map((fieldName) => {
const fc = tc.getField(fieldName);
const type = getTypeNode(fc.type);
if (!type)
return;
return {
kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
name: getNameNode(fieldName),
type,
description: getDescriptionNode(fc.description),
directives: getDirectiveNodes(tc.getFieldDirectives(fieldName), tc.schemaComposer),
defaultValue: (fc.defaultValue !== undefined &&
astFromValueSafe(fc.defaultValue, tc.getFieldType(fieldName))) ||
undefined,
};
})
.filter(Boolean);
}
function getNameNode(value) {
return { kind: graphql_1.Kind.NAME, value };
}
function getEnumValueDefinitionNodes(tc) {
const fieldNames = tc.getFieldNames();
if (!fieldNames.length)
return;
return fieldNames.map((fieldName) => {
const fc = tc.getField(fieldName);
return {
kind: graphql_1.Kind.ENUM_VALUE_DEFINITION,
name: getNameNode(fieldName),
description: getDescriptionNode(fc.description),
directives: getDirectiveNodes(tc.getFieldDirectives(fieldName), tc.schemaComposer),
};
});
}
function parseValueNode(ast, variables = {}, typeName) {
switch (ast.kind) {
case graphql_1.Kind.STRING:
case graphql_1.Kind.BOOLEAN:
return ast.value;
case graphql_1.Kind.INT:
case graphql_1.Kind.FLOAT:
return parseFloat(ast.value);
case graphql_1.Kind.OBJECT:
const value = Object.create(null);
ast.fields.forEach((field) => {
value[field.name.value] = parseValueNode(field.value, variables, typeName);
});
return value;
case graphql_1.Kind.LIST:
return ast.values.map((n) => parseValueNode(n, variables, typeName));
case graphql_1.Kind.NULL:
return null;
case graphql_1.Kind.VARIABLE:
return variables ? variables[ast.name.value] : undefined;
default:
throw new TypeError(`${typeName} cannot represent value: ${(0, misc_1.inspect)(ast)}`);
}
}
function astFromValueSafe(value, type) {
try {
return (0, graphql_1.astFromValue)(value, type);
}
catch (e) {
return toValueNode(value);
}
}
//# sourceMappingURL=definitionNode.js.map