UNPKG

polen

Version:

A framework for delightful GraphQL developer portals

69 lines 2.55 kB
import { Grafaid } from '#lib/grafaid/index'; import { GraphqlChange } from '#lib/graphql-change/index'; import { debugPolen } from '#singletons/debug'; import { Arr, Path } from '@wollybeard/kit'; import { glob } from 'tinyglobby'; import { FileNameExpression } from './file-name-expression/index.js'; // const debug = debugPolen.sub([`schema`, `data-source-schema-directory`]) const debug = debugPolen.sub(`schema:data-source-schema-directory`); const defaultPaths = { schemaDirectory: `./schema`, }; export const normalizeConfig = (configInput) => { const ensureAbsolute = Path.ensureAbsoluteWith(configInput.projectRoot); const config = { path: ensureAbsolute(configInput.path ?? defaultPaths.schemaDirectory), }; return config; }; export const readOrThrow = async (configInput) => { const config = normalizeConfig(configInput); debug(`will search`, config); const filePaths = await glob({ cwd: config.path, absolute: true, onlyFiles: true, patterns: [`*.graphql`], }); debug(`did find`, filePaths); if (!Arr.isntEmpty(filePaths)) { return null; } const fileNameExpressions = Arr.map(filePaths, FileNameExpression.parseOrThrow); debug(`parsed file names`, fileNameExpressions); const versions = await Promise.all(Arr.map(fileNameExpressions, async (fileNameExpression) => { const schemaFile = await Grafaid.Schema.read(fileNameExpression.filePath); // Should never happen since these paths come from the glob. if (!schemaFile) throw new Error(`Failed to read schema file: ${fileNameExpression.filePath}`); return { ...fileNameExpression, schema: schemaFile.content, }; })); debug(`read schemas`); versions.sort((a, b) => a.date.getTime() - b.date.getTime()); const changesets = await Promise.all(Arr.map(versions, async (version, index) => { const current = version; const previous = versions[index - 1]; const before = previous?.schema ?? Grafaid.Schema.empty; const after = current.schema; const changes = await GraphqlChange.calcChangeset({ before, after, }); return { date: current.date, before, after, changes, }; })); changesets.reverse(); const schema = { versions: changesets, }; debug(`computed schema`); return schema; }; //# sourceMappingURL=schema-directory.js.map