graphql-compose
Version:
GraphQL schema builder from different data sources with middleware extensions.
270 lines • 12.8 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printSchemaComposer = printSchemaComposer;
exports.printSchema = printSchema;
exports.printIntrospectionSchema = printIntrospectionSchema;
exports.isDefinedType = isDefinedType;
exports.printFilteredSchema = printFilteredSchema;
exports.printSchemaDefinition = printSchemaDefinition;
exports.isSchemaOfCommonNames = isSchemaOfCommonNames;
exports.printType = printType;
exports.printScalar = printScalar;
exports.printImplementedInterfaces = printImplementedInterfaces;
exports.printObject = printObject;
exports.printInterface = printInterface;
exports.printUnion = printUnion;
exports.printEnum = printEnum;
exports.printInputObject = printInputObject;
exports.printFields = printFields;
exports.printBlock = printBlock;
exports.printArgs = printArgs;
exports.printInputValue = printInputValue;
exports.printDirective = printDirective;
exports.printNodeDirectives = printNodeDirectives;
exports.printDescription = printDescription;
exports.printDescriptionWithComments = printDescriptionWithComments;
const misc_1 = require("./misc");
const printer_1 = require("graphql/language/printer");
const blockString_1 = require("graphql/language/blockString");
const introspection_1 = require("graphql/type/introspection");
const scalars_1 = require("graphql/type/scalars");
const directives_1 = require("graphql/type/directives");
const definition_1 = require("graphql/type/definition");
const astFromValue_1 = require("graphql/utilities/astFromValue");
const getFromSchema_1 = require("./getFromSchema");
const schemaPrinterSortTypes_1 = require("./schemaPrinterSortTypes");
const graphqlVersion_1 = require("./graphqlVersion");
let printBlockStringLegacy;
if (graphqlVersion_1.graphqlVersion >= 16) {
printBlockStringLegacy = blockString_1.printBlockString;
}
else {
printBlockStringLegacy = (value, preferMultipleLines) => blockString_1.printBlockString(value, '', preferMultipleLines);
}
function splitOptionsFilterPrinter(options) {
const _a = options || {}, { exclude = [], include, omitDirectiveDefinitions } = _a, optPrinter = __rest(_a, ["exclude", "include", "omitDirectiveDefinitions"]);
const optFilter = { exclude, include, omitDirectiveDefinitions };
return { optPrinter, optFilter };
}
function printSchemaComposer(sc, options) {
const { optPrinter, optFilter } = splitOptionsFilterPrinter(options);
const printTypes = Array.from((0, getFromSchema_1.getTypesFromSchema)(sc, optFilter));
const sortMethod = (0, schemaPrinterSortTypes_1.getSortMethodFromOption)(optPrinter === null || optPrinter === void 0 ? void 0 : optPrinter.sortTypes, optFilter);
if (sortMethod)
printTypes.sort(sortMethod);
const res = [];
if (!optFilter.omitDirectiveDefinitions) {
res.push(...(0, getFromSchema_1.getDirectivesFromSchema)(sc).map((d) => printDirective(d, optPrinter)));
}
res.push(...printTypes.map((tc) => tc.toSDL(optPrinter)));
return res.filter(Boolean).join('\n\n');
}
function printSchema(schema, options) {
return printFilteredSchema(schema, (n) => !(0, directives_1.isSpecifiedDirective)(n), isDefinedType, options);
}
function printIntrospectionSchema(schema, options) {
return printFilteredSchema(schema, directives_1.isSpecifiedDirective, introspection_1.isIntrospectionType, options);
}
function isDefinedType(type) {
return !(0, scalars_1.isSpecifiedScalarType)(type) && !(0, introspection_1.isIntrospectionType)(type);
}
function printFilteredSchema(schema, directiveFilter, typeFilter, options) {
const directives = schema.getDirectives().filter(directiveFilter);
const typeMap = schema.getTypeMap();
const types = Object.values(typeMap)
.sort((type1, type2) => type1.name.localeCompare(type2.name))
.filter(typeFilter);
return `${[printSchemaDefinition(schema)]
.concat(directives.map((directive) => printDirective(directive, options)), types.map((type) => printType(type, options)))
.filter(Boolean)
.join('\n\n')}\n`;
}
function printSchemaDefinition(schema) {
if (isSchemaOfCommonNames(schema)) {
return '';
}
const operationTypes = [];
const queryType = schema.getQueryType();
if (queryType) {
operationTypes.push(` query: ${queryType.name}`);
}
const mutationType = schema.getMutationType();
if (mutationType) {
operationTypes.push(` mutation: ${mutationType.name}`);
}
const subscriptionType = schema.getSubscriptionType();
if (subscriptionType) {
operationTypes.push(` subscription: ${subscriptionType.name}`);
}
return `schema {\n${operationTypes.join('\n')}\n}`;
}
function isSchemaOfCommonNames(schema) {
const queryType = schema.getQueryType();
if (queryType && queryType.name !== 'Query') {
return false;
}
const mutationType = schema.getMutationType();
if (mutationType && mutationType.name !== 'Mutation') {
return false;
}
const subscriptionType = schema.getSubscriptionType();
if (subscriptionType && subscriptionType.name !== 'Subscription') {
return false;
}
return true;
}
function printType(type, options) {
if ((0, definition_1.isScalarType)(type)) {
return printScalar(type, options);
}
else if ((0, definition_1.isObjectType)(type)) {
return printObject(type, options);
}
else if ((0, definition_1.isInterfaceType)(type)) {
return printInterface(type, options);
}
else if ((0, definition_1.isUnionType)(type)) {
return printUnion(type, options);
}
else if ((0, definition_1.isEnumType)(type)) {
return printEnum(type, options);
}
else if ((0, definition_1.isInputObjectType)(type)) {
return printInputObject(type, options);
}
(0, misc_1.invariant)(false, `Unexpected type: ${(0, misc_1.inspect)(type)}`);
return '';
}
function printScalar(type, options) {
if (options === null || options === void 0 ? void 0 : options.omitScalars)
return '';
return `${printDescription(type, options)}scalar ${type.name}${printNodeDirectives(type.astNode)}`;
}
function printImplementedInterfaces(type, options) {
const interfaces = (type.getInterfaces ? type.getInterfaces() : []);
if (!interfaces.length)
return '';
if ((options === null || options === void 0 ? void 0 : options.sortAll) || (options === null || options === void 0 ? void 0 : options.sortInterfaces)) {
return ` implements ${interfaces
.map((i) => i.name)
.sort()
.join(' & ')}`;
}
return ` implements ${interfaces.map((i) => i.name).join(' & ')}`;
}
function printObject(type, options) {
return `${printDescription(type, options)}type ${type.name}${printImplementedInterfaces(type, options)}${printNodeDirectives(type.astNode)}${printFields(type, options)}`;
}
function printInterface(type, options) {
return `${printDescription(type, options)}interface ${type.name}${printImplementedInterfaces(type, options)}${printNodeDirectives(type.astNode)}${printFields(type, options)}`;
}
function printUnion(type, options) {
let types = type.getTypes();
if ((options === null || options === void 0 ? void 0 : options.sortAll) || (options === null || options === void 0 ? void 0 : options.sortUnions)) {
types = [...types].sort();
}
const possibleTypes = types.length ? ` = ${types.join(' | ')}` : '';
return `${printDescription(type, options)}union ${type.name}${printNodeDirectives(type.astNode)}${possibleTypes}`;
}
function printEnum(type, options) {
let values = type.getValues();
if ((options === null || options === void 0 ? void 0 : options.sortAll) || (options === null || options === void 0 ? void 0 : options.sortEnums)) {
values = [...values].sort((a, b) => a.name.localeCompare(b.name));
}
const valuesList = values.map((value, i) => `${printDescription(value, options, ' ', !i)} ${value.name}${printNodeDirectives(value.astNode)}`);
return `${printDescription(type, options)}enum ${type.name}${printNodeDirectives(type.astNode)}${printBlock(valuesList)}`;
}
function printInputObject(type, options) {
let fields = Object.values(type.getFields());
if ((options === null || options === void 0 ? void 0 : options.sortAll) || (options === null || options === void 0 ? void 0 : options.sortFields)) {
fields = fields.sort((a, b) => a.name.localeCompare(b.name));
}
const fieldsList = fields.map((f, i) => `${printDescription(f, options, ' ', !i)} ${printInputValue(f)}`);
return `${printDescription(type, options)}input ${type.name}${printNodeDirectives(type.astNode)}${printBlock(fieldsList)}`;
}
function printFields(type, options) {
let fields = Object.values(type.getFields());
if ((options === null || options === void 0 ? void 0 : options.sortAll) || (options === null || options === void 0 ? void 0 : options.sortFields)) {
fields = fields.sort((a, b) => a.name.localeCompare(b.name));
}
const fieldsList = fields.map((f, i) => `${printDescription(f, options, ' ', !i)} ${f.name}${printArgs(f.args, options, ' ')}: ${String(f.type)}${printNodeDirectives(f.astNode)}`);
return printBlock(fieldsList);
}
function printBlock(items) {
return items.length !== 0 ? ` {\n${items.join('\n')}\n}` : '';
}
function printArgs(_args, options, indentation = '') {
if (_args.length === 0) {
return '';
}
const args = (options === null || options === void 0 ? void 0 : options.sortAll) || (options === null || options === void 0 ? void 0 : options.sortArgs)
? [..._args].sort((a, b) => a.name.localeCompare(b.name))
: _args;
if (args.every((arg) => !arg.description)) {
return `(${args.map(printInputValue).join(', ')})`;
}
return `(\n${args
.map((arg, i) => `${printDescription(arg, options, ` ${indentation}`, !i)} ${indentation}${printInputValue(arg)}`)
.join('\n')}\n${indentation})`;
}
function printInputValue(arg) {
const defaultInput = arg.default;
const defaultAST = defaultInput
? defaultInput.literal || require('graphql').valueToLiteral(defaultInput.value, arg.type)
: (0, astFromValue_1.astFromValue)(arg.defaultValue, arg.type);
let argDecl = `${arg.name}: ${String(arg.type)}`;
if (defaultAST) {
argDecl += ` = ${(0, printer_1.print)(defaultAST)}`;
}
return `${argDecl}${printNodeDirectives(arg.astNode)}`;
}
function printDirective(directive, options) {
return `${printDescription(directive, options)}directive @${directive.name}${printArgs(directive.args, options)}${directive.isRepeatable ? ' repeatable' : ''} on ${directive.locations.join(' | ')}`;
}
function printNodeDirectives(node) {
if (!node || !node.directives || !node.directives.length)
return '';
return ` ${node.directives
.map((d) => {
let args = '';
if (d.arguments && d.arguments.length) {
args = `(${d.arguments.map((a) => `${a.name.value}: ${(0, printer_1.print)(a.value)}`).join(', ')})`;
}
return `@${d.name.value}${args}`;
})
.join(' ')}`;
}
function printDescription(def, options, indentation = '', firstInBlock = true) {
let { description } = def;
if (description == null || (options === null || options === void 0 ? void 0 : options.omitDescriptions)) {
return '';
}
description = description.trimRight();
if (options && options.commentDescriptions) {
return printDescriptionWithComments(description, indentation, firstInBlock);
}
const preferMultipleLines = description.length > 70;
const blockString = printBlockStringLegacy(description, preferMultipleLines);
const prefix = indentation && !firstInBlock ? `\n${indentation}` : indentation;
return `${prefix + blockString.replace(/\n/g, `\n${indentation}`)}\n`;
}
function printDescriptionWithComments(description, indentation, firstInBlock) {
const prefix = indentation && !firstInBlock ? '\n' : '';
const comment = description
.split('\n')
.map((line) => indentation + (line !== '' ? `# ${line}` : '#'))
.join('\n');
return `${prefix + comment}\n`;
}
//# sourceMappingURL=schemaPrinter.js.map