@graphql-inspector/validate-command
Advanced tools
Comparing version 0.0.0-canary.da4bdcc to 0.0.0-canary.e88eb08
206
index.cjs.js
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const tslib = require('tslib'); | ||
@@ -7,6 +9,66 @@ const commands = require('@graphql-inspector/commands'); | ||
const core = require('@graphql-inspector/core'); | ||
const path = require('path'); | ||
const graphql = require('graphql'); | ||
function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, filter, onlyErrors, relativePaths, json, }) { | ||
let invalidDocuments = core.validate(schema, documents.map((doc) => new graphql.Source(graphql.print(doc.document), doc.location)), { | ||
strictFragments, | ||
maxDepth, | ||
apollo, | ||
keepClientFields, | ||
}); | ||
if (filter) { | ||
invalidDocuments = invalidDocuments.filter((doc) => filter.some((filepath) => doc.source.name.includes(filepath))); | ||
} | ||
if (!invalidDocuments.length) { | ||
logger.Logger.success('All documents are valid'); | ||
} | ||
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) { | ||
if (!json) { | ||
logger.Logger.log(`\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\n`); | ||
} | ||
printInvalidDocuments(invalidDocuments, 'errors', true, json); | ||
} | ||
else { | ||
logger.Logger.success('All documents are valid'); | ||
} | ||
if (deprecated && !onlyErrors) { | ||
if (!json) { | ||
logger.Logger.info(`\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\n`); | ||
} | ||
printInvalidDocuments(invalidDocuments, 'deprecated', false, json); | ||
} | ||
if (shouldFailProcess) { | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
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) => { | ||
const { loaders, interceptArguments, interceptPositional, interceptOptions, } = api; | ||
const { loaders } = api; | ||
return { | ||
@@ -17,15 +79,15 @@ command: 'validate <documents> <schema>', | ||
return yargs | ||
.positional('schema', interceptPositional('schema', { | ||
.positional('schema', { | ||
describe: 'Point to a schema', | ||
type: 'string', | ||
demandOption: true, | ||
})) | ||
.positional('documents', interceptPositional('documents', { | ||
describe: 'Point to docuents', | ||
}) | ||
.positional('documents', { | ||
describe: 'Point to documents', | ||
type: 'string', | ||
demandOption: true, | ||
})) | ||
.options(interceptOptions({ | ||
d: { | ||
alias: 'deprecated', | ||
}) | ||
.options({ | ||
deprecated: { | ||
alias: 'd', | ||
describe: 'Fail on deprecated usage', | ||
@@ -54,52 +116,58 @@ type: 'boolean', | ||
}, | ||
})); | ||
filter: { | ||
describe: 'Show results only from a list of files (or file)', | ||
array: true, | ||
type: 'string', | ||
}, | ||
onlyErrors: { | ||
describe: 'Show only errors', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
relativePaths: { | ||
describe: 'Show relative paths', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
json: { | ||
describe: 'Display as JSON', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
}); | ||
}, | ||
handler(args) { | ||
var _a; | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
interceptArguments(args); | ||
const { headers, token } = commands.parseGlobalArgs(args); | ||
const apollo = args.apollo || false; | ||
const aws = args.aws || false; | ||
const apolloFederation = args.federation || false; | ||
const method = ((_a = args.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'POST'; | ||
const maxDepth = args.maxDepth || undefined; | ||
const strictFragments = !args.noStrictFragments; | ||
const keepClientFields = args.keepClientFields || false; | ||
const failOnDeprecated = args.deprecated; | ||
const json = args.json || false; | ||
const relativePaths = args.relativePaths || false; | ||
const onlyErrors = args.onlyErrors || false; | ||
const schema = yield loaders.loadSchema(args.schema, { | ||
headers, | ||
token, | ||
}); | ||
method, | ||
}, apolloFederation, aws); | ||
const documents = yield loaders.loadDocuments(args.documents); | ||
const invalidDocuments = core.validate(schema, documents.map((doc) => new graphql.Source(graphql.print(doc.document), doc.location)), { | ||
strictFragments: !args.noStrictFragments, | ||
maxDepth: args.maxDepth || undefined, | ||
apollo: args.apollo || false, | ||
keepClientFields: args.keepClientFields || false, | ||
return handler({ | ||
schema, | ||
documents, | ||
apollo, | ||
maxDepth, | ||
strictFragments, | ||
keepClientFields, | ||
failOnDeprecated, | ||
filter: args.filter, | ||
json, | ||
relativePaths, | ||
onlyErrors, | ||
}); | ||
if (!invalidDocuments.length) { | ||
logger.Logger.success('All documents are valid'); | ||
} | ||
else { | ||
const errorsCount = countErrors(invalidDocuments); | ||
const deprecated = countDeprecated(invalidDocuments); | ||
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); | ||
}); | ||
} | ||
}); | ||
} | ||
else if (!args.deprecated) { | ||
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, args.deprecated).forEach((line) => { | ||
logger.Logger.log(line); | ||
}); | ||
} | ||
}); | ||
} | ||
if (errorsCount || (deprecated && args.deprecated)) { | ||
process.exit(1); | ||
} | ||
} | ||
}); | ||
@@ -122,21 +190,26 @@ }, | ||
} | ||
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, isJson = false) { | ||
if (isJson) { | ||
logger.Logger.log(JSON.stringify(invalidDocuments.map((doc) => ({ | ||
source: doc.source.name, | ||
[listKey]: doc[listKey].map((item) => item.message), | ||
})))); | ||
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', | ||
@@ -146,3 +219,4 @@ ]; | ||
module.exports = index; | ||
exports.default = index; | ||
exports.handler = handler; | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -1,11 +0,31 @@ | ||
import { GlobalArgs } from '@graphql-inspector/commands'; | ||
declare const _default: import("../../commands/src").CommandFactory<{}, { | ||
import { GlobalArgs, CommandFactory } from '@graphql-inspector/commands'; | ||
import { Source as DocumentSource } from '@graphql-tools/utils'; | ||
import { GraphQLSchema } from 'graphql'; | ||
export { CommandFactory }; | ||
export declare function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, filter, onlyErrors, relativePaths, json, }: { | ||
schema: GraphQLSchema; | ||
documents: DocumentSource[]; | ||
failOnDeprecated: boolean; | ||
strictFragments: boolean; | ||
apollo: boolean; | ||
keepClientFields: boolean; | ||
maxDepth?: number; | ||
filter?: string[]; | ||
onlyErrors?: boolean; | ||
relativePaths?: boolean; | ||
json?: boolean; | ||
}): void; | ||
declare const _default: CommandFactory<{}, { | ||
schema: string; | ||
documents: string; | ||
deprecated?: boolean | undefined; | ||
deprecated: boolean; | ||
noStrictFragments: boolean; | ||
apollo?: boolean | undefined; | ||
keepClientFields?: boolean | undefined; | ||
apollo: boolean; | ||
keepClientFields: boolean; | ||
maxDepth?: number | undefined; | ||
filter?: string[] | undefined; | ||
onlyErrors?: boolean | undefined; | ||
relativePaths?: boolean | undefined; | ||
json?: boolean | undefined; | ||
} & GlobalArgs>; | ||
export default _default; |
202
index.esm.js
@@ -5,6 +5,66 @@ import { __awaiter } from 'tslib'; | ||
import { validate } from '@graphql-inspector/core'; | ||
import { relative } from 'path'; | ||
import { Source, print } from 'graphql'; | ||
function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, filter, onlyErrors, relativePaths, json, }) { | ||
let invalidDocuments = validate(schema, documents.map((doc) => new Source(print(doc.document), doc.location)), { | ||
strictFragments, | ||
maxDepth, | ||
apollo, | ||
keepClientFields, | ||
}); | ||
if (filter) { | ||
invalidDocuments = invalidDocuments.filter((doc) => filter.some((filepath) => doc.source.name.includes(filepath))); | ||
} | ||
if (!invalidDocuments.length) { | ||
Logger.success('All documents are valid'); | ||
} | ||
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) { | ||
if (!json) { | ||
Logger.log(`\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\n`); | ||
} | ||
printInvalidDocuments(invalidDocuments, 'errors', true, json); | ||
} | ||
else { | ||
Logger.success('All documents are valid'); | ||
} | ||
if (deprecated && !onlyErrors) { | ||
if (!json) { | ||
Logger.info(`\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\n`); | ||
} | ||
printInvalidDocuments(invalidDocuments, 'deprecated', false, json); | ||
} | ||
if (shouldFailProcess) { | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
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) => { | ||
const { loaders, interceptArguments, interceptPositional, interceptOptions, } = api; | ||
const { loaders } = api; | ||
return { | ||
@@ -15,15 +75,15 @@ command: 'validate <documents> <schema>', | ||
return yargs | ||
.positional('schema', interceptPositional('schema', { | ||
.positional('schema', { | ||
describe: 'Point to a schema', | ||
type: 'string', | ||
demandOption: true, | ||
})) | ||
.positional('documents', interceptPositional('documents', { | ||
describe: 'Point to docuents', | ||
}) | ||
.positional('documents', { | ||
describe: 'Point to documents', | ||
type: 'string', | ||
demandOption: true, | ||
})) | ||
.options(interceptOptions({ | ||
d: { | ||
alias: 'deprecated', | ||
}) | ||
.options({ | ||
deprecated: { | ||
alias: 'd', | ||
describe: 'Fail on deprecated usage', | ||
@@ -52,52 +112,58 @@ type: 'boolean', | ||
}, | ||
})); | ||
filter: { | ||
describe: 'Show results only from a list of files (or file)', | ||
array: true, | ||
type: 'string', | ||
}, | ||
onlyErrors: { | ||
describe: 'Show only errors', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
relativePaths: { | ||
describe: 'Show relative paths', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
json: { | ||
describe: 'Display as JSON', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
}); | ||
}, | ||
handler(args) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
interceptArguments(args); | ||
const { headers, token } = parseGlobalArgs(args); | ||
const apollo = args.apollo || false; | ||
const aws = args.aws || false; | ||
const apolloFederation = args.federation || false; | ||
const method = ((_a = args.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'POST'; | ||
const maxDepth = args.maxDepth || undefined; | ||
const strictFragments = !args.noStrictFragments; | ||
const keepClientFields = args.keepClientFields || false; | ||
const failOnDeprecated = args.deprecated; | ||
const json = args.json || false; | ||
const relativePaths = args.relativePaths || false; | ||
const onlyErrors = args.onlyErrors || false; | ||
const schema = yield loaders.loadSchema(args.schema, { | ||
headers, | ||
token, | ||
}); | ||
method, | ||
}, apolloFederation, aws); | ||
const documents = yield loaders.loadDocuments(args.documents); | ||
const invalidDocuments = validate(schema, documents.map((doc) => new Source(print(doc.document), doc.location)), { | ||
strictFragments: !args.noStrictFragments, | ||
maxDepth: args.maxDepth || undefined, | ||
apollo: args.apollo || false, | ||
keepClientFields: args.keepClientFields || false, | ||
return handler({ | ||
schema, | ||
documents, | ||
apollo, | ||
maxDepth, | ||
strictFragments, | ||
keepClientFields, | ||
failOnDeprecated, | ||
filter: args.filter, | ||
json, | ||
relativePaths, | ||
onlyErrors, | ||
}); | ||
if (!invalidDocuments.length) { | ||
Logger.success('All documents are valid'); | ||
} | ||
else { | ||
const errorsCount = countErrors(invalidDocuments); | ||
const deprecated = countDeprecated(invalidDocuments); | ||
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); | ||
}); | ||
} | ||
}); | ||
} | ||
else if (!args.deprecated) { | ||
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, args.deprecated).forEach((line) => { | ||
Logger.log(line); | ||
}); | ||
} | ||
}); | ||
} | ||
if (errorsCount || (deprecated && args.deprecated)) { | ||
process.exit(1); | ||
} | ||
} | ||
}); | ||
@@ -120,21 +186,26 @@ }, | ||
} | ||
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, isJson = false) { | ||
if (isJson) { | ||
Logger.log(JSON.stringify(invalidDocuments.map((doc) => ({ | ||
source: doc.source.name, | ||
[listKey]: doc[listKey].map((item) => item.message), | ||
})))); | ||
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', | ||
@@ -145,2 +216,3 @@ ]; | ||
export default index; | ||
export { handler }; | ||
//# sourceMappingURL=index.esm.js.map |
{ | ||
"name": "@graphql-inspector/validate-command", | ||
"version": "0.0.0-canary.da4bdcc", | ||
"version": "0.0.0-canary.e88eb08", | ||
"description": "Validate Documents in GraphQL Inspector", | ||
@@ -10,5 +10,5 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@graphql-inspector/commands": "0.0.0-canary.da4bdcc", | ||
"@graphql-inspector/core": "0.0.0-canary.da4bdcc", | ||
"@graphql-inspector/logger": "0.0.0-canary.da4bdcc", | ||
"@graphql-inspector/commands": "0.0.0-canary.e88eb08", | ||
"@graphql-inspector/core": "0.0.0-canary.e88eb08", | ||
"@graphql-inspector/logger": "0.0.0-canary.e88eb08", | ||
"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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
48926
455
1
+ Added@graphql-inspector/commands@0.0.0-canary.e88eb08(transitive)
+ Added@graphql-inspector/config@0.0.0-canary.e88eb08(transitive)
+ Added@graphql-inspector/core@0.0.0-canary.e88eb08(transitive)
+ Added@graphql-inspector/loaders@0.0.0-canary.e88eb08(transitive)
+ Added@graphql-inspector/logger@0.0.0-canary.e88eb08(transitive)
+ Addedchalk@4.1.1(transitive)
+ Addedci-info@3.9.0(transitive)
+ Addedcliui@7.0.4(transitive)
+ Addeddependency-graph@0.11.0(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedfastq@1.19.0(transitive)
+ Addedis-unicode-supported@0.1.0(transitive)
+ Addedlog-symbols@4.1.0(transitive)
+ Addedobject-inspect@1.10.3(transitive)
+ Addedstd-env@2.3.0(transitive)
+ Addedwrap-ansi@7.0.0(transitive)
+ Addedy18n@5.0.8(transitive)
+ Addedyargs@17.0.1(transitive)
+ Addedyargs-parser@20.2.9(transitive)
- Removed@graphql-inspector/commands@0.0.0-canary.da4bdcc(transitive)
- Removed@graphql-inspector/config@0.0.0-canary.da4bdcc(transitive)
- Removed@graphql-inspector/core@0.0.0-canary.da4bdcc(transitive)
- Removed@graphql-inspector/loaders@0.0.0-canary.da4bdcc(transitive)
- Removed@graphql-inspector/logger@0.0.0-canary.da4bdcc(transitive)
- Removed@graphql-tools/utils@6.2.4(transitive)
- Removedcamel-case@4.1.1(transitive)
- Removedcamelcase@5.3.1(transitive)
- Removedchalk@4.0.0(transitive)
- Removedci-info@1.6.0(transitive)
- Removedcliui@6.0.0(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removeddependency-graph@0.9.0(transitive)
- Removedfastq@1.18.0(transitive)
- Removedfind-up@4.1.0(transitive)
- Removedlocate-path@5.0.0(transitive)
- Removedlog-symbols@4.0.0(transitive)
- Removedp-limit@2.3.0(transitive)
- Removedp-locate@4.1.0(transitive)
- Removedp-try@2.2.0(transitive)
- Removedpath-exists@4.0.0(transitive)
- Removedrequire-main-filename@2.0.0(transitive)
- Removedset-blocking@2.0.0(transitive)
- Removedstd-env@2.2.1(transitive)
- Removedtslib@1.14.1(transitive)
- Removedwhich-module@2.0.1(transitive)
- Removedwrap-ansi@6.2.0(transitive)
- Removedy18n@4.0.3(transitive)
- Removedyargs@15.3.1(transitive)
- Removedyargs-parser@18.1.3(transitive)