Socket
Socket
Sign inDemoInstall

@graphql-inspector/validate-command

Package Overview
Dependencies
76
Maintainers
2
Versions
516
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

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;

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc