UNPKG

@graphql-cli/validate

Version:
145 lines (141 loc) 5.09 kB
import { __awaiter } from 'tslib'; import { defineCommand } from '@graphql-cli/common'; import { loaders } from '@graphql-cli/loaders'; import { handler } from '@graphql-inspector/validate-command'; function parseGlobalArgs(args) { const headers = {}; if (args.header) { args.header.forEach((header) => { const [name, ...values] = header.split(':'); headers[name] = values.join(''); }); } if (args.require) { args.require.forEach((mod) => require(mod)); } return { headers, token: args.token }; } const createInspectorExtension = (name) => (api) => { loaders.forEach((loader) => { api.loaders.schema.register(loader); }); loaders.forEach((loader) => { api.loaders.documents.register(loader); }); return { name, }; }; const index = defineCommand((api) => { return { command: 'validate [project]', describe: 'Validate Fragments and Operations', builder(yargs) { return yargs .positional('project', { describe: 'Point to a project', type: 'string', }) .options({ schema: { describe: 'Point to a schema', type: 'string', }, documents: { describe: 'Point to operations and fragments', type: 'string', }, deprecated: { alias: 'd', describe: 'Fail on deprecated usage', type: 'boolean', default: false, }, noStrictFragments: { describe: 'Do not fail on duplicated fragment names', type: 'boolean', default: false, }, maxDepth: { describe: 'Fail on deep operations', type: 'number', }, apollo: { describe: 'Support Apollo directives', type: 'boolean', default: false, }, keepClientFields: { describe: 'Keeps the fields with @client, but removes @client directive from them', type: 'boolean', default: false, }, require: { alias: 'r', describe: 'Require modules', type: 'array', }, token: { alias: 't', describe: 'Access Token', type: 'string', }, header: { alias: 'h', describe: 'Http Header', type: 'array', }, config: { alias: 'c', type: 'string', describe: 'Location of GraphQL Config', }, }); }, handler(args) { return __awaiter(this, void 0, void 0, function* () { const apollo = args.apollo || false; const maxDepth = args.maxDepth || undefined; const strictFragments = !args.noStrictFragments; const keepClientFields = args.keepClientFields || false; const failOnDeprecated = args.deprecated; const { headers, token } = parseGlobalArgs(args); const config = yield api.useConfig({ rootDir: args.config || process.cwd(), extensions: [createInspectorExtension('validate')], }); if (args.documents && args.schema) { const { loadDocuments, loadSchema } = api.useLoaders({ loaders }); const schema = yield loadSchema(args.schema, { headers, token }); const documents = yield loadDocuments(args.documents, { headers, token, }); return handler({ schema, documents, apollo, maxDepth, strictFragments, keepClientFields, failOnDeprecated, }); } const project = config.getProject(args.project); const schema = yield project.getSchema(); const documents = yield project.getDocuments(); return handler({ schema, documents, apollo, maxDepth, strictFragments, keepClientFields, failOnDeprecated, }); }); }, }; }); export default index; //# sourceMappingURL=index.esm.js.map