UNPKG

@graphql-inspector/diff-command

Version:
192 lines (191 loc) • 6.55 kB
import { existsSync } from 'fs'; import { createCommand, ensureAbsolute, parseGlobalArgs, } from '@graphql-inspector/commands'; import { CriticalityLevel, DiffRule, diff as diffSchema, } from '@graphql-inspector/core'; import { bolderize, Logger, symbols } from '@graphql-inspector/logger'; export async function handler(input) { const onComplete = input.onComplete ? resolveCompletionHandler(input.onComplete) : failOnBreakingChanges; const rules = input.rules ? input.rules .filter(isString) .map((name) => { const rule = resolveRule(name); if (!rule) { throw new Error(`Rule '${name}' does not exist!\n`); } return rule; }) .filter(f => f) : []; const changes = await diffSchema(input.oldSchema, input.newSchema, rules, { checkUsage: input.onUsage ? resolveUsageHandler(input.onUsage) : undefined, }); if (changes.length === 0) { Logger.success('No changes detected'); return; } Logger.log(`\nDetected the following changes (${changes.length}) between schemas:\n`); const breakingChanges = changes.filter(change => change.criticality.level === CriticalityLevel.Breaking); const dangerousChanges = changes.filter(change => change.criticality.level === CriticalityLevel.Dangerous); const nonBreakingChanges = changes.filter(change => change.criticality.level === CriticalityLevel.NonBreaking); if (breakingChanges.length) { reportBreakingChanges(breakingChanges); } if (dangerousChanges.length) { reportDangerousChanges(dangerousChanges); } if (nonBreakingChanges.length) { reportNonBreakingChanges(nonBreakingChanges); } onComplete({ breakingChanges, dangerousChanges, nonBreakingChanges }); } export default createCommand(api => { const { loaders } = api; return { command: 'diff <oldSchema> <newSchema>', describe: 'Compare two GraphQL Schemas', builder(yargs) { return yargs .positional('oldSchema', { describe: 'Point to an old schema', type: 'string', demandOption: true, }) .positional('newSchema', { describe: 'Point to a new schema', type: 'string', demandOption: true, }) .options({ rule: { describe: 'Add rules', array: true, }, onComplete: { describe: 'Handle Completion', type: 'string', }, onUsage: { describe: 'Checks usage of schema', type: 'string', }, }); }, async handler(args) { try { const oldSchemaPointer = args.oldSchema; const newSchemaPointer = args.newSchema; const apolloFederation = args.federation || false; const aws = args.aws || false; const method = args.method?.toUpperCase() || 'POST'; const { headers, leftHeaders, rightHeaders, token } = parseGlobalArgs(args); const oldSchemaHeaders = { ...headers, ...leftHeaders, }; const newSchemaHeaders = { ...headers, ...rightHeaders, }; const oldSchema = await loaders.loadSchema(oldSchemaPointer, { headers: oldSchemaHeaders, token, method, }, apolloFederation, aws); const newSchema = await loaders.loadSchema(newSchemaPointer, { headers: newSchemaHeaders, token, method, }, apolloFederation, aws); await handler({ oldSchema, newSchema, rules: args.rule, onComplete: args.onComplete, onUsage: args.onUsage, }); } catch (error) { Logger.error(error); throw error; } }, }; }); function sortChanges(changes) { return changes.slice().sort((a, b) => { const aPath = a.path || ''; const bPath = b.path || ''; if (aPath > bPath) { return 1; } if (bPath > aPath) { return -1; } return 0; }); } function reportBreakingChanges(changes) { const label = symbols.error; const sorted = sortChanges(changes); for (const change of sorted) { Logger.log(`${label} ${bolderize(change.message)}`); } } function reportDangerousChanges(changes) { const label = symbols.warning; const sorted = sortChanges(changes); for (const change of sorted) { Logger.log(`${label} ${bolderize(change.message)}`); } } function reportNonBreakingChanges(changes) { const label = symbols.success; const sorted = sortChanges(changes); for (const change of sorted) { Logger.log(`${label} ${bolderize(change.message)}`); } } function resolveRule(name) { const filepath = ensureAbsolute(name); if (existsSync(filepath)) { return require(filepath); } return DiffRule[name]; } function resolveCompletionHandler(name) { const filepath = ensureAbsolute(name); try { require.resolve(filepath); } catch (error) { throw new Error(`CompletionHandler '${name}' does not exist!`); } const mod = require(filepath); return mod?.default || mod; } function resolveUsageHandler(name) { const filepath = ensureAbsolute(name); try { require.resolve(filepath); } catch (error) { throw new Error(`UsageHandler '${name}' does not exist!`); } const mod = require(filepath); return mod?.default || mod; } function failOnBreakingChanges({ breakingChanges }) { const breakingCount = breakingChanges.length; if (breakingCount) { Logger.error(`Detected ${breakingCount} breaking change${breakingCount > 1 ? 's' : ''}`); process.exit(1); } else { Logger.success('No breaking changes detected'); } } function isString(val) { return typeof val === 'string'; }