@magidoc/rollup-plugin-gql-schema
Version:
A Rollup and ViteJS plugin that allows to parse a GraphQL Schema from a target URL and save it to a target output folder, or to parse it from the disk and convert it to a desired format.
29 lines (26 loc) • 1.21 kB
JavaScript
import { printSchema, isSpecifiedScalarType, print, isSpecifiedDirective } from 'graphql';
function printSchemaWithDirectives(schema) {
const sdlWithDirectives = printAstNodesWithDirectives(schema);
// Means that there are no AST nodes present
if (sdlWithDirectives.trim().length === 0)
return printSchema(schema);
return sdlWithDirectives;
}
function printAstNodesWithDirectives(schema) {
const str = Object.values(schema.getTypeMap())
.filter((k) => !k.name.match(/^__/))
.reduce((accum, type) => {
if (isSpecifiedScalarType(type))
return accum;
const typeString = type.astNode ? print(type.astNode) : '';
const extensionString = type.extensionASTNodes ? type.extensionASTNodes.map((ast) => print(ast)).join('\n') : '';
return `${accum}\n${typeString}\n${extensionString}\n`;
}, '');
return schema.getDirectives().reduce((accum, d) => {
if (!d.astNode)
return accum;
return !isSpecifiedDirective(d) ? `${accum}${print(d.astNode)}\n` : accum;
}, `${str}${schema.astNode ? print(schema.astNode) : ''}\n`);
}
export { printSchemaWithDirectives };
//# sourceMappingURL=print.js.map