@graphql-hive/cli
Version:
A CLI util to manage and control your GraphQL Hive
102 lines • 4.5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderWarnings = exports.renderChanges = exports.renderErrors = void 0;
exports.loadSchema = loadSchema;
exports.minifySchema = minifySchema;
const graphql_1 = require("graphql");
const code_file_loader_1 = require("@graphql-tools/code-file-loader");
const graphql_file_loader_1 = require("@graphql-tools/graphql-file-loader");
const json_file_loader_1 = require("@graphql-tools/json-file-loader");
const load_1 = require("@graphql-tools/load");
const url_loader_1 = require("@graphql-tools/url-loader");
const gql_1 = require("../gql");
const graphql_2 = require("../gql/graphql");
const texture_1 = require("./texture/texture");
const criticalityMap = {
[graphql_2.CriticalityLevel.Breaking]: texture_1.Texture.colors.red('-'),
[graphql_2.CriticalityLevel.Safe]: texture_1.Texture.colors.green('-'),
[graphql_2.CriticalityLevel.Dangerous]: texture_1.Texture.colors.green('-'),
};
const renderErrors = (errors) => {
const t = texture_1.Texture.createBuilder();
t.failure(`Detected ${errors.total} error${errors.total > 1 ? 's' : ''}`);
t.line();
errors.nodes.forEach(error => {
t.indent(texture_1.Texture.colors.red('-') + ' ' + texture_1.Texture.boldQuotedWords(error.message));
});
return t.state.value;
};
exports.renderErrors = renderErrors;
const RenderChanges_SchemaChanges = (0, gql_1.graphql)(`
fragment RenderChanges_schemaChanges on SchemaChangeConnection {
total
nodes {
criticality
isSafeBasedOnUsage
message(withSafeBasedOnUsageNote: false)
approval {
approvedBy {
displayName
}
}
}
}
`);
const renderChanges = (maskedChanges) => {
const t = texture_1.Texture.createBuilder();
const changes = (0, gql_1.useFragment)(RenderChanges_SchemaChanges, maskedChanges);
const writeChanges = (changes) => {
changes.forEach(change => {
var _a, _b;
const messageParts = [
criticalityMap[change.isSafeBasedOnUsage ? graphql_2.CriticalityLevel.Safe : change.criticality],
texture_1.Texture.boldQuotedWords(change.message),
];
if (change.isSafeBasedOnUsage) {
messageParts.push(texture_1.Texture.colors.green('(Safe based on usage ✓)'));
}
if (change.approval) {
messageParts.push(texture_1.Texture.colors.green(`(Approved by ${(_b = (_a = change.approval.approvedBy) === null || _a === void 0 ? void 0 : _a.displayName) !== null && _b !== void 0 ? _b : '<unknown>'} ✓)`));
}
t.indent(messageParts.join(' '));
});
};
t.info(`Detected ${changes.total} change${changes.total > 1 ? 's' : ''}`);
t.line();
const breakingChanges = changes.nodes.filter(change => change.criticality === graphql_2.CriticalityLevel.Breaking);
const safeChanges = changes.nodes.filter(change => change.criticality !== graphql_2.CriticalityLevel.Breaking);
if (breakingChanges.length) {
t.indent(`Breaking changes:`);
writeChanges(breakingChanges);
}
if (safeChanges.length) {
t.indent(`Safe changes:`);
writeChanges(safeChanges);
}
return t.state.value;
};
exports.renderChanges = renderChanges;
const renderWarnings = (warnings) => {
const t = texture_1.Texture.createBuilder();
t.line();
t.warning(`Detected ${warnings.total} warning${warnings.total > 1 ? 's' : ''}`);
t.line();
warnings.nodes.forEach(warning => {
const details = [
warning.source ? `source: ${texture_1.Texture.boldQuotedWords(warning.source)}` : undefined,
]
.filter(Boolean)
.join(', ');
t.indent(`- ${texture_1.Texture.boldQuotedWords(warning.message)}${details ? ` (${details})` : ''}`);
});
return t.state.value;
};
exports.renderWarnings = renderWarnings;
async function loadSchema(file, options) {
const sources = await (0, load_1.loadTypedefs)(file, Object.assign(Object.assign({}, options), { cwd: process.cwd(), loaders: [new code_file_loader_1.CodeFileLoader(), new graphql_file_loader_1.GraphQLFileLoader(), new json_file_loader_1.JsonFileLoader(), new url_loader_1.UrlLoader()] }));
return (0, graphql_1.print)((0, graphql_1.concatAST)(sources.map(s => s.document)));
}
function minifySchema(schema) {
return schema.replace(/\s+/g, ' ').trim();
}
//# sourceMappingURL=schema.js.map
;