UNPKG

@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.

30 lines (27 loc) 1.25 kB
import glob from 'fast-glob'; import { readFile } from 'fs/promises'; import { buildSchema } from 'graphql'; async function parseGraphqlSchema(options) { const rawSchema = await readFullSchema(options.globPaths); try { return buildSchema(rawSchema); } catch (error) { throw new Error(`Unable to extract a GraphQL introspection from provided schema: ${String(error)}`, { cause: error, }); } } async function readFullSchema(globPaths) { const paths = new Set((await Promise.all(globPaths.map((path) => readGlobPaths(path)))).flat()); if (paths.size === 0) { throw new Error(`No paths found matching target glob patterns: [${globPaths.toString()}].\nIf you used relative paths, make sure the paths are relative to where the node command was launched or use absolute paths.`); } return (await Promise.all(Array.from(paths.values()).map((path) => readFile(path).then((buff) => buff.toString())))).join('\n\n'); } async function readGlobPaths(globPath) { // Backslashes in windows paths are not supported by fast-glob return [...new Set(await glob(globPath.replaceAll('\\', '/'), { dot: true }))]; } export { parseGraphqlSchema }; //# sourceMappingURL=parse.js.map