@graphql-inspector/validate-command
Advanced tools
Comparing version 2.7.0 to 2.8.0
123
index.cjs.js
@@ -9,5 +9,7 @@ 'use strict'; | ||
const core = require('@graphql-inspector/core'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const graphql = require('graphql'); | ||
function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, filter, }) { | ||
function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, filter, onlyErrors, relativePaths, output, silent, }) { | ||
let invalidDocuments = core.validate(schema, documents.map((doc) => new graphql.Source(graphql.print(doc.document), doc.location)), { | ||
@@ -26,28 +28,35 @@ strictFragments, | ||
else { | ||
if (failOnDeprecated) { | ||
invalidDocuments = moveDeprecatedToErrors(invalidDocuments); | ||
} | ||
if (relativePaths) { | ||
invalidDocuments = useRelativePaths(invalidDocuments); | ||
} | ||
const errorsCount = countErrors(invalidDocuments); | ||
const deprecated = countDeprecated(invalidDocuments); | ||
const shouldFailProcess = errorsCount > 0; | ||
if (errorsCount) { | ||
logger.Logger.log(`\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\n`); | ||
invalidDocuments.forEach((doc) => { | ||
if (doc.errors.length) { | ||
renderInvalidDocument(doc).forEach((line) => { | ||
logger.Logger.log(line); | ||
}); | ||
} | ||
}); | ||
if (!silent) { | ||
logger.Logger.log(`\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\n`); | ||
} | ||
printInvalidDocuments(invalidDocuments, 'errors', true, silent); | ||
} | ||
else if (!failOnDeprecated) { | ||
else { | ||
logger.Logger.success('All documents are valid'); | ||
} | ||
if (deprecated) { | ||
logger.Logger.info(`\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\n`); | ||
invalidDocuments.forEach((doc) => { | ||
if (doc.deprecated.length) { | ||
renderDeprecatedUsageInDocument(doc, failOnDeprecated).forEach((line) => { | ||
logger.Logger.log(line); | ||
}); | ||
} | ||
if (deprecated && !onlyErrors) { | ||
if (!silent) { | ||
logger.Logger.info(`\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\n`); | ||
} | ||
printInvalidDocuments(invalidDocuments, 'deprecated', false, silent); | ||
} | ||
if (output) { | ||
fs.writeFileSync(output, JSON.stringify({ | ||
status: !shouldFailProcess, | ||
documents: invalidDocuments, | ||
}, null, 2), { | ||
encoding: 'utf-8', | ||
}); | ||
} | ||
if (errorsCount || (deprecated && failOnDeprecated)) { | ||
if (shouldFailProcess) { | ||
process.exit(1); | ||
@@ -57,2 +66,18 @@ } | ||
} | ||
function moveDeprecatedToErrors(docs) { | ||
return docs.map((doc) => { | ||
var _a, _b; | ||
return ({ | ||
source: doc.source, | ||
errors: [...((_a = doc.errors) !== null && _a !== void 0 ? _a : []), ...((_b = doc.deprecated) !== null && _b !== void 0 ? _b : [])], | ||
deprecated: [], | ||
}); | ||
}); | ||
} | ||
function useRelativePaths(docs) { | ||
return docs.map((doc) => { | ||
doc.source.name = path.relative(process.cwd(), doc.source.name); | ||
return doc; | ||
}); | ||
} | ||
const index = commands.createCommand((api) => { | ||
@@ -104,4 +129,23 @@ const { loaders } = api; | ||
array: true, | ||
type: 'string' | ||
type: 'string', | ||
}, | ||
onlyErrors: { | ||
describe: 'Show only errors', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
relativePaths: { | ||
describe: 'Show relative paths', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
silent: { | ||
describe: 'Do not print results', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
output: { | ||
describe: 'Output JSON file', | ||
type: 'string', | ||
}, | ||
}); | ||
@@ -121,2 +165,6 @@ }, | ||
const failOnDeprecated = args.deprecated; | ||
const output = args.output; | ||
const silent = args.silent || false; | ||
const relativePaths = args.relativePaths || false; | ||
const onlyErrors = args.onlyErrors || false; | ||
const schema = yield loaders.loadSchema(args.schema, { | ||
@@ -137,2 +185,6 @@ headers, | ||
filter: args.filter, | ||
silent, | ||
output, | ||
relativePaths, | ||
onlyErrors, | ||
}); | ||
@@ -156,21 +208,22 @@ }); | ||
} | ||
function renderInvalidDocument(invalidDoc) { | ||
const errors = invalidDoc.errors | ||
.map((e) => ` - ${logger.bolderize(e.message)}`) | ||
.join('\n'); | ||
return [ | ||
logger.chalk.redBright('error'), | ||
`in ${invalidDoc.source.name}:\n\n`, | ||
errors, | ||
'\n\n', | ||
]; | ||
function printInvalidDocuments(invalidDocuments, listKey, isError = false, silent = false) { | ||
if (silent) { | ||
return; | ||
} | ||
invalidDocuments.forEach((doc) => { | ||
if (doc.errors.length) { | ||
renderErrors(doc.source.name, doc[listKey], isError).forEach((line) => { | ||
logger.Logger.log(line); | ||
}); | ||
} | ||
}); | ||
} | ||
function renderDeprecatedUsageInDocument(invalidDoc, isCritical = false) { | ||
const deprecated = invalidDoc.deprecated | ||
function renderErrors(sourceName, errors, isError = false) { | ||
const errorsAsString = errors | ||
.map((e) => ` - ${logger.bolderize(e.message)}`) | ||
.join('\n'); | ||
return [ | ||
isCritical ? logger.chalk.redBright('error') : logger.chalk.yellowBright('warn'), | ||
`in ${invalidDoc.source.name}:\n\n`, | ||
deprecated, | ||
isError ? logger.chalk.redBright('error') : logger.chalk.yellowBright('warn'), | ||
`in ${sourceName}:\n\n`, | ||
errorsAsString, | ||
'\n\n', | ||
@@ -177,0 +230,0 @@ ]; |
@@ -5,3 +5,3 @@ import { GlobalArgs, CommandFactory } from '@graphql-inspector/commands'; | ||
export { CommandFactory }; | ||
export declare function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, filter, }: { | ||
export declare function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, filter, onlyErrors, relativePaths, output, silent, }: { | ||
schema: GraphQLSchema; | ||
@@ -15,2 +15,6 @@ documents: DocumentSource[]; | ||
filter?: string[]; | ||
onlyErrors?: boolean; | ||
relativePaths?: boolean; | ||
output?: string; | ||
silent?: boolean; | ||
}): void; | ||
@@ -26,3 +30,7 @@ declare const _default: CommandFactory<{}, { | ||
filter?: string[] | undefined; | ||
onlyErrors?: boolean | undefined; | ||
relativePaths?: boolean | undefined; | ||
output?: string | undefined; | ||
silent?: boolean | undefined; | ||
} & GlobalArgs>; | ||
export default _default; |
123
index.esm.js
@@ -5,5 +5,7 @@ import { __awaiter } from 'tslib'; | ||
import { validate } from '@graphql-inspector/core'; | ||
import { relative } from 'path'; | ||
import { writeFileSync } from 'fs'; | ||
import { Source, print } from 'graphql'; | ||
function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, filter, }) { | ||
function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, filter, onlyErrors, relativePaths, output, silent, }) { | ||
let invalidDocuments = validate(schema, documents.map((doc) => new Source(print(doc.document), doc.location)), { | ||
@@ -22,28 +24,35 @@ strictFragments, | ||
else { | ||
if (failOnDeprecated) { | ||
invalidDocuments = moveDeprecatedToErrors(invalidDocuments); | ||
} | ||
if (relativePaths) { | ||
invalidDocuments = useRelativePaths(invalidDocuments); | ||
} | ||
const errorsCount = countErrors(invalidDocuments); | ||
const deprecated = countDeprecated(invalidDocuments); | ||
const shouldFailProcess = errorsCount > 0; | ||
if (errorsCount) { | ||
Logger.log(`\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\n`); | ||
invalidDocuments.forEach((doc) => { | ||
if (doc.errors.length) { | ||
renderInvalidDocument(doc).forEach((line) => { | ||
Logger.log(line); | ||
}); | ||
} | ||
}); | ||
if (!silent) { | ||
Logger.log(`\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\n`); | ||
} | ||
printInvalidDocuments(invalidDocuments, 'errors', true, silent); | ||
} | ||
else if (!failOnDeprecated) { | ||
else { | ||
Logger.success('All documents are valid'); | ||
} | ||
if (deprecated) { | ||
Logger.info(`\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\n`); | ||
invalidDocuments.forEach((doc) => { | ||
if (doc.deprecated.length) { | ||
renderDeprecatedUsageInDocument(doc, failOnDeprecated).forEach((line) => { | ||
Logger.log(line); | ||
}); | ||
} | ||
if (deprecated && !onlyErrors) { | ||
if (!silent) { | ||
Logger.info(`\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\n`); | ||
} | ||
printInvalidDocuments(invalidDocuments, 'deprecated', false, silent); | ||
} | ||
if (output) { | ||
writeFileSync(output, JSON.stringify({ | ||
status: !shouldFailProcess, | ||
documents: invalidDocuments, | ||
}, null, 2), { | ||
encoding: 'utf-8', | ||
}); | ||
} | ||
if (errorsCount || (deprecated && failOnDeprecated)) { | ||
if (shouldFailProcess) { | ||
process.exit(1); | ||
@@ -53,2 +62,18 @@ } | ||
} | ||
function moveDeprecatedToErrors(docs) { | ||
return docs.map((doc) => { | ||
var _a, _b; | ||
return ({ | ||
source: doc.source, | ||
errors: [...((_a = doc.errors) !== null && _a !== void 0 ? _a : []), ...((_b = doc.deprecated) !== null && _b !== void 0 ? _b : [])], | ||
deprecated: [], | ||
}); | ||
}); | ||
} | ||
function useRelativePaths(docs) { | ||
return docs.map((doc) => { | ||
doc.source.name = relative(process.cwd(), doc.source.name); | ||
return doc; | ||
}); | ||
} | ||
const index = createCommand((api) => { | ||
@@ -100,4 +125,23 @@ const { loaders } = api; | ||
array: true, | ||
type: 'string' | ||
type: 'string', | ||
}, | ||
onlyErrors: { | ||
describe: 'Show only errors', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
relativePaths: { | ||
describe: 'Show relative paths', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
silent: { | ||
describe: 'Do not print results', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
output: { | ||
describe: 'Output JSON file', | ||
type: 'string', | ||
}, | ||
}); | ||
@@ -117,2 +161,6 @@ }, | ||
const failOnDeprecated = args.deprecated; | ||
const output = args.output; | ||
const silent = args.silent || false; | ||
const relativePaths = args.relativePaths || false; | ||
const onlyErrors = args.onlyErrors || false; | ||
const schema = yield loaders.loadSchema(args.schema, { | ||
@@ -133,2 +181,6 @@ headers, | ||
filter: args.filter, | ||
silent, | ||
output, | ||
relativePaths, | ||
onlyErrors, | ||
}); | ||
@@ -152,21 +204,22 @@ }); | ||
} | ||
function renderInvalidDocument(invalidDoc) { | ||
const errors = invalidDoc.errors | ||
.map((e) => ` - ${bolderize(e.message)}`) | ||
.join('\n'); | ||
return [ | ||
chalk.redBright('error'), | ||
`in ${invalidDoc.source.name}:\n\n`, | ||
errors, | ||
'\n\n', | ||
]; | ||
function printInvalidDocuments(invalidDocuments, listKey, isError = false, silent = false) { | ||
if (silent) { | ||
return; | ||
} | ||
invalidDocuments.forEach((doc) => { | ||
if (doc.errors.length) { | ||
renderErrors(doc.source.name, doc[listKey], isError).forEach((line) => { | ||
Logger.log(line); | ||
}); | ||
} | ||
}); | ||
} | ||
function renderDeprecatedUsageInDocument(invalidDoc, isCritical = false) { | ||
const deprecated = invalidDoc.deprecated | ||
function renderErrors(sourceName, errors, isError = false) { | ||
const errorsAsString = errors | ||
.map((e) => ` - ${bolderize(e.message)}`) | ||
.join('\n'); | ||
return [ | ||
isCritical ? chalk.redBright('error') : chalk.yellowBright('warn'), | ||
`in ${invalidDoc.source.name}:\n\n`, | ||
deprecated, | ||
isError ? chalk.redBright('error') : chalk.yellowBright('warn'), | ||
`in ${sourceName}:\n\n`, | ||
errorsAsString, | ||
'\n\n', | ||
@@ -173,0 +226,0 @@ ]; |
{ | ||
"name": "@graphql-inspector/validate-command", | ||
"version": "2.7.0", | ||
"version": "2.8.0", | ||
"description": "Validate Documents in GraphQL Inspector", | ||
@@ -10,5 +10,5 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@graphql-inspector/commands": "2.7.0", | ||
"@graphql-inspector/core": "2.7.0", | ||
"@graphql-inspector/logger": "2.7.0", | ||
"@graphql-inspector/commands": "2.8.0", | ||
"@graphql-inspector/core": "2.8.0", | ||
"@graphql-inspector/logger": "2.8.0", | ||
"tslib": "^2.0.0" | ||
@@ -15,0 +15,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
50597
479
2
+ Added@graphql-inspector/commands@2.8.0(transitive)
+ Added@graphql-inspector/config@2.8.0(transitive)
+ Added@graphql-inspector/core@2.8.0(transitive)
+ Added@graphql-inspector/loaders@2.8.0(transitive)
+ Added@graphql-inspector/logger@2.8.0(transitive)
- Removed@graphql-inspector/commands@2.7.0(transitive)
- Removed@graphql-inspector/config@2.7.0(transitive)
- Removed@graphql-inspector/core@2.7.0(transitive)
- Removed@graphql-inspector/loaders@2.7.0(transitive)
- Removed@graphql-inspector/logger@2.7.0(transitive)