gqlcheck
Version:
Performs additional checks on your GraphQL documents and operations to ensure they conform to your rules, whilst allow-listing existing operations and their constituent parts (and allowing overrides on a per-field basis). Rules include max selection set d
75 lines (72 loc) • 2.72 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.printResults = printResults;
exports.generateOutputAndCounts = generateOutputAndCounts;
function printGraphQLFormattedErrorLocations(error) {
if (error.locations?.length) {
const first = error.locations[0];
return `[${first.line}:${first.column}] `;
}
else {
return "";
}
}
function printGraphQLFormattedError(error) {
return `${printGraphQLFormattedErrorLocations(error)}${error.message}`;
}
function printRuleFormattedError(error) {
const opCoords = error.operations.flatMap((o) => o.operationCoordinates);
return `${printGraphQLFormattedErrorLocations(error)}${error.message}\nProblematic paths:\n- ${opCoords.slice(0, 10).join("\n- ")}${opCoords.length > 10 ? "\n- ..." : ""}`;
}
function printCounts(result) {
return `Scanned ${result.counts.Document ?? 0} documents consisting of ${result.counts.OperationDefinition ?? 0} operations (and ${result.counts.FragmentDefinition ?? 0} fragments). Visited ${result.counts.Field ?? 0} fields, ${result.counts.Argument ?? 0} arguments, ${result.counts.FragmentSpread ?? 0} named fragment spreads and ${result.counts.InlineFragment ?? 0} inline fragment spreads.`;
/*
return Object.entries(result.counts)
.sort((a, z) => a[0].localeCompare(z[0], "en-US"))
.map(([k, v]) => `- ${k}: ${v}`)
.join("\n");
*/
}
function printResults(result) {
return generateOutputAndCounts(result).output;
}
function generateOutputAndCounts(result) {
const results = result.resultsBySourceName;
let errors = 0;
let infractions = 0;
const parts = Object.entries(results)
.sort((a, z) => a[0].localeCompare(z[0], "en-US"))
.map(([sourceName, spec]) => {
const { output } = spec;
const items = [];
if (output.errors) {
for (const error of output.errors) {
if ("infraction" in error) {
infractions++;
items.push(printRuleFormattedError(error));
}
else {
errors++;
items.push(printGraphQLFormattedError(error));
}
}
}
if (items.length > 0) {
return `${sourceName}:\n${items.map((i) => `- ${i.replace(/\n/g, "\n ")}`).join("\n")}`;
}
else {
return null;
}
})
.filter(Boolean);
return {
output: `
${parts.join("\n\n")}
${printCounts(result)}
Errors: ${errors}
Infractions: ${infractions}${result.filtered > 0 ? ` (ignored: ${result.filtered})` : ``}
`.trim(),
errors,
infractions,
};
}
;