@graphql-inspector/validate-command 0.0.0-canary.58ffd99 → 0.0.0-canary.7db8980

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import { GlobalArgs, CommandFactory } from '@graphql-inspector/commands';
2
2
  import { Source as DocumentSource } from '@graphql-tools/utils';
3
3
  import { GraphQLSchema } from 'graphql';
4
4
  export { CommandFactory };
5
- export declare function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, }: {
5
+ export declare function handler({ schema, documents, strictFragments, maxDepth, maxDirectiveCount, maxAliasCount, maxTokenCount, apollo, keepClientFields, failOnDeprecated, filter, onlyErrors, relativePaths, output, silent, }: {
6
6
  schema: GraphQLSchema;
7
7
  documents: DocumentSource[];
8
8
  failOnDeprecated: boolean;
@@ -10,6 +10,14 @@ export declare function handler({ schema, documents, strictFragments, maxDepth,
10
10
  apollo: boolean;
11
11
  keepClientFields: boolean;
12
12
  maxDepth?: number;
13
+ maxDirectiveCount?: number;
14
+ maxAliasCount?: number;
15
+ maxTokenCount?: number;
16
+ filter?: string[];
17
+ onlyErrors?: boolean;
18
+ relativePaths?: boolean;
19
+ output?: string;
20
+ silent?: boolean;
13
21
  }): void;
14
22
  declare const _default: CommandFactory<{}, {
15
23
  schema: string;
@@ -19,5 +27,14 @@ declare const _default: CommandFactory<{}, {
19
27
  apollo: boolean;
20
28
  keepClientFields: boolean;
21
29
  maxDepth?: number | undefined;
30
+ maxAliasCount?: number | undefined;
31
+ maxDirectiveCount?: number | undefined;
32
+ maxTokenCount?: number | undefined;
33
+ filter?: string[] | undefined;
34
+ onlyErrors?: boolean | undefined;
35
+ relativePaths?: boolean | undefined;
36
+ output?: string | undefined;
37
+ silent?: boolean | undefined;
38
+ ignore?: string[] | undefined;
22
39
  } & GlobalArgs>;
23
40
  export default _default;
package/index.js ADDED
@@ -0,0 +1,256 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const tslib = require('tslib');
6
+ const commands = require('@graphql-inspector/commands');
7
+ const logger = require('@graphql-inspector/logger');
8
+ const core = require('@graphql-inspector/core');
9
+ const path = require('path');
10
+ const fs = require('fs');
11
+ const graphql = require('graphql');
12
+
13
+ function handler({ schema, documents, strictFragments, maxDepth, maxDirectiveCount, maxAliasCount, maxTokenCount, apollo, keepClientFields, failOnDeprecated, filter, onlyErrors, relativePaths, output, silent, }) {
14
+ let invalidDocuments = core.validate(schema, documents.map(doc => new graphql.Source(graphql.print(doc.document), doc.location)), {
15
+ strictFragments,
16
+ maxDepth,
17
+ maxAliasCount,
18
+ maxDirectiveCount,
19
+ maxTokenCount,
20
+ apollo,
21
+ keepClientFields,
22
+ });
23
+ if (!invalidDocuments.length) {
24
+ logger.Logger.success('All documents are valid');
25
+ }
26
+ else {
27
+ if (failOnDeprecated) {
28
+ invalidDocuments = moveDeprecatedToErrors(invalidDocuments);
29
+ }
30
+ if (relativePaths) {
31
+ invalidDocuments = useRelativePaths(invalidDocuments);
32
+ }
33
+ const errorsCount = countErrors(invalidDocuments);
34
+ const deprecated = countDeprecated(invalidDocuments);
35
+ const shouldFailProcess = errorsCount > 0;
36
+ if (errorsCount) {
37
+ if (!silent) {
38
+ logger.Logger.log(`\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\n`);
39
+ }
40
+ printInvalidDocuments(useFilter(invalidDocuments, filter), 'errors', true, silent);
41
+ }
42
+ else {
43
+ logger.Logger.success('All documents are valid');
44
+ }
45
+ if (deprecated && !onlyErrors) {
46
+ if (!silent) {
47
+ logger.Logger.info(`\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\n`);
48
+ }
49
+ printInvalidDocuments(useFilter(invalidDocuments, filter), 'deprecated', false, silent);
50
+ }
51
+ if (output) {
52
+ fs.writeFileSync(output, JSON.stringify({
53
+ status: !shouldFailProcess,
54
+ documents: useFilter(invalidDocuments, filter),
55
+ }, null, 2), {
56
+ encoding: 'utf-8',
57
+ });
58
+ }
59
+ if (shouldFailProcess) {
60
+ process.exit(1);
61
+ }
62
+ }
63
+ }
64
+ function moveDeprecatedToErrors(docs) {
65
+ return docs.map(doc => {
66
+ var _a, _b;
67
+ return ({
68
+ source: doc.source,
69
+ errors: [...((_a = doc.errors) !== null && _a !== void 0 ? _a : []), ...((_b = doc.deprecated) !== null && _b !== void 0 ? _b : [])],
70
+ deprecated: [],
71
+ });
72
+ });
73
+ }
74
+ function useRelativePaths(docs) {
75
+ return docs.map(doc => {
76
+ doc.source.name = path.relative(process.cwd(), doc.source.name);
77
+ return doc;
78
+ });
79
+ }
80
+ function useFilter(docs, patterns) {
81
+ if (!patterns || !patterns.length) {
82
+ return docs;
83
+ }
84
+ return docs.filter(doc => patterns.some(filepath => doc.source.name.includes(filepath)));
85
+ }
86
+ const index = commands.createCommand(api => {
87
+ const { loaders } = api;
88
+ return {
89
+ command: 'validate <documents> <schema>',
90
+ describe: 'Validate Fragments and Operations',
91
+ builder(yargs) {
92
+ return yargs
93
+ .positional('schema', {
94
+ describe: 'Point to a schema',
95
+ type: 'string',
96
+ demandOption: true,
97
+ })
98
+ .positional('documents', {
99
+ describe: 'Point to documents',
100
+ type: 'string',
101
+ demandOption: true,
102
+ })
103
+ .options({
104
+ deprecated: {
105
+ alias: 'd',
106
+ describe: 'Fail on deprecated usage',
107
+ type: 'boolean',
108
+ default: false,
109
+ },
110
+ noStrictFragments: {
111
+ describe: 'Do not fail on duplicated fragment names',
112
+ type: 'boolean',
113
+ default: false,
114
+ },
115
+ maxDepth: {
116
+ describe: 'Fail on deep operations',
117
+ type: 'number',
118
+ },
119
+ maxAliasCount: {
120
+ describe: 'Fail on operations with too many aliases',
121
+ type: 'number',
122
+ },
123
+ maxDirectiveCount: {
124
+ describe: 'Fail on operations with too many directives',
125
+ type: 'number',
126
+ },
127
+ maxTokenCount: {
128
+ describe: 'Fail on operations with too many tokens',
129
+ type: 'number',
130
+ },
131
+ apollo: {
132
+ describe: 'Support Apollo directives',
133
+ type: 'boolean',
134
+ default: false,
135
+ },
136
+ keepClientFields: {
137
+ describe: 'Keeps the fields with @client, but removes @client directive from them',
138
+ type: 'boolean',
139
+ default: false,
140
+ },
141
+ filter: {
142
+ describe: 'Show results only from a list of files (or file)',
143
+ array: true,
144
+ type: 'string',
145
+ },
146
+ ignore: {
147
+ describe: 'Ignore and do not load these files (supports glob)',
148
+ array: true,
149
+ type: 'string',
150
+ },
151
+ onlyErrors: {
152
+ describe: 'Show only errors',
153
+ type: 'boolean',
154
+ default: false,
155
+ },
156
+ relativePaths: {
157
+ describe: 'Show relative paths',
158
+ type: 'boolean',
159
+ default: false,
160
+ },
161
+ silent: {
162
+ describe: 'Do not print results',
163
+ type: 'boolean',
164
+ default: false,
165
+ },
166
+ output: {
167
+ describe: 'Output JSON file',
168
+ type: 'string',
169
+ },
170
+ });
171
+ },
172
+ handler(args) {
173
+ var _a;
174
+ return tslib.__awaiter(this, void 0, void 0, function* () {
175
+ const { headers, token } = commands.parseGlobalArgs(args);
176
+ const apollo = args.apollo || false;
177
+ const aws = args.aws || false;
178
+ const apolloFederation = args.federation || false;
179
+ const method = ((_a = args.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'POST';
180
+ const maxDepth = args.maxDepth != null ? args.maxDepth : undefined;
181
+ const maxAliasCount = args.maxAliasCount != null ? args.maxAliasCount : undefined;
182
+ const maxDirectiveCount = args.maxDirectiveCount != null ? args.maxDirectiveCount : undefined;
183
+ const maxTokenCount = args.maxTokenCount != null ? args.maxTokenCount : undefined;
184
+ const strictFragments = !args.noStrictFragments;
185
+ const keepClientFields = args.keepClientFields || false;
186
+ const failOnDeprecated = args.deprecated;
187
+ const output = args.output;
188
+ const silent = args.silent || false;
189
+ const relativePaths = args.relativePaths || false;
190
+ const onlyErrors = args.onlyErrors || false;
191
+ const ignore = args.ignore || [];
192
+ const schema = yield loaders.loadSchema(args.schema, {
193
+ headers,
194
+ token,
195
+ method,
196
+ }, apolloFederation, aws);
197
+ const documents = yield loaders.loadDocuments(args.documents, {
198
+ ignore,
199
+ });
200
+ return handler({
201
+ schema,
202
+ documents,
203
+ apollo,
204
+ maxDepth,
205
+ maxAliasCount,
206
+ maxDirectiveCount,
207
+ maxTokenCount,
208
+ strictFragments,
209
+ keepClientFields,
210
+ failOnDeprecated,
211
+ filter: args.filter,
212
+ silent,
213
+ output,
214
+ relativePaths,
215
+ onlyErrors,
216
+ });
217
+ });
218
+ },
219
+ };
220
+ });
221
+ function countErrors(invalidDocuments) {
222
+ if (invalidDocuments.length) {
223
+ return invalidDocuments.filter(doc => doc.errors && doc.errors.length).length;
224
+ }
225
+ return 0;
226
+ }
227
+ function countDeprecated(invalidDocuments) {
228
+ if (invalidDocuments.length) {
229
+ return invalidDocuments.filter(doc => doc.deprecated && doc.deprecated.length).length;
230
+ }
231
+ return 0;
232
+ }
233
+ function printInvalidDocuments(invalidDocuments, listKey, isError = false, silent = false) {
234
+ if (silent) {
235
+ return;
236
+ }
237
+ invalidDocuments.forEach(doc => {
238
+ if (doc.errors.length) {
239
+ renderErrors(doc.source.name, doc[listKey], isError).forEach(line => {
240
+ logger.Logger.log(line);
241
+ });
242
+ }
243
+ });
244
+ }
245
+ function renderErrors(sourceName, errors, isError = false) {
246
+ const errorsAsString = errors.map(e => ` - ${logger.bolderize(e.message)}`).join('\n');
247
+ return [
248
+ isError ? logger.chalk.redBright('error') : logger.chalk.yellowBright('warn'),
249
+ `in ${sourceName}:\n\n`,
250
+ errorsAsString,
251
+ '\n\n',
252
+ ];
253
+ }
254
+
255
+ exports.default = index;
256
+ exports.handler = handler;
package/index.mjs ADDED
@@ -0,0 +1,252 @@
1
+ import { __awaiter } from 'tslib';
2
+ import { createCommand, parseGlobalArgs } from '@graphql-inspector/commands';
3
+ import { Logger, chalk, bolderize } from '@graphql-inspector/logger';
4
+ import { validate } from '@graphql-inspector/core';
5
+ import { relative } from 'path';
6
+ import { writeFileSync } from 'fs';
7
+ import { Source, print } from 'graphql';
8
+
9
+ function handler({ schema, documents, strictFragments, maxDepth, maxDirectiveCount, maxAliasCount, maxTokenCount, apollo, keepClientFields, failOnDeprecated, filter, onlyErrors, relativePaths, output, silent, }) {
10
+ let invalidDocuments = validate(schema, documents.map(doc => new Source(print(doc.document), doc.location)), {
11
+ strictFragments,
12
+ maxDepth,
13
+ maxAliasCount,
14
+ maxDirectiveCount,
15
+ maxTokenCount,
16
+ apollo,
17
+ keepClientFields,
18
+ });
19
+ if (!invalidDocuments.length) {
20
+ Logger.success('All documents are valid');
21
+ }
22
+ else {
23
+ if (failOnDeprecated) {
24
+ invalidDocuments = moveDeprecatedToErrors(invalidDocuments);
25
+ }
26
+ if (relativePaths) {
27
+ invalidDocuments = useRelativePaths(invalidDocuments);
28
+ }
29
+ const errorsCount = countErrors(invalidDocuments);
30
+ const deprecated = countDeprecated(invalidDocuments);
31
+ const shouldFailProcess = errorsCount > 0;
32
+ if (errorsCount) {
33
+ if (!silent) {
34
+ Logger.log(`\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\n`);
35
+ }
36
+ printInvalidDocuments(useFilter(invalidDocuments, filter), 'errors', true, silent);
37
+ }
38
+ else {
39
+ Logger.success('All documents are valid');
40
+ }
41
+ if (deprecated && !onlyErrors) {
42
+ if (!silent) {
43
+ Logger.info(`\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\n`);
44
+ }
45
+ printInvalidDocuments(useFilter(invalidDocuments, filter), 'deprecated', false, silent);
46
+ }
47
+ if (output) {
48
+ writeFileSync(output, JSON.stringify({
49
+ status: !shouldFailProcess,
50
+ documents: useFilter(invalidDocuments, filter),
51
+ }, null, 2), {
52
+ encoding: 'utf-8',
53
+ });
54
+ }
55
+ if (shouldFailProcess) {
56
+ process.exit(1);
57
+ }
58
+ }
59
+ }
60
+ function moveDeprecatedToErrors(docs) {
61
+ return docs.map(doc => {
62
+ var _a, _b;
63
+ return ({
64
+ source: doc.source,
65
+ errors: [...((_a = doc.errors) !== null && _a !== void 0 ? _a : []), ...((_b = doc.deprecated) !== null && _b !== void 0 ? _b : [])],
66
+ deprecated: [],
67
+ });
68
+ });
69
+ }
70
+ function useRelativePaths(docs) {
71
+ return docs.map(doc => {
72
+ doc.source.name = relative(process.cwd(), doc.source.name);
73
+ return doc;
74
+ });
75
+ }
76
+ function useFilter(docs, patterns) {
77
+ if (!patterns || !patterns.length) {
78
+ return docs;
79
+ }
80
+ return docs.filter(doc => patterns.some(filepath => doc.source.name.includes(filepath)));
81
+ }
82
+ const index = createCommand(api => {
83
+ const { loaders } = api;
84
+ return {
85
+ command: 'validate <documents> <schema>',
86
+ describe: 'Validate Fragments and Operations',
87
+ builder(yargs) {
88
+ return yargs
89
+ .positional('schema', {
90
+ describe: 'Point to a schema',
91
+ type: 'string',
92
+ demandOption: true,
93
+ })
94
+ .positional('documents', {
95
+ describe: 'Point to documents',
96
+ type: 'string',
97
+ demandOption: true,
98
+ })
99
+ .options({
100
+ deprecated: {
101
+ alias: 'd',
102
+ describe: 'Fail on deprecated usage',
103
+ type: 'boolean',
104
+ default: false,
105
+ },
106
+ noStrictFragments: {
107
+ describe: 'Do not fail on duplicated fragment names',
108
+ type: 'boolean',
109
+ default: false,
110
+ },
111
+ maxDepth: {
112
+ describe: 'Fail on deep operations',
113
+ type: 'number',
114
+ },
115
+ maxAliasCount: {
116
+ describe: 'Fail on operations with too many aliases',
117
+ type: 'number',
118
+ },
119
+ maxDirectiveCount: {
120
+ describe: 'Fail on operations with too many directives',
121
+ type: 'number',
122
+ },
123
+ maxTokenCount: {
124
+ describe: 'Fail on operations with too many tokens',
125
+ type: 'number',
126
+ },
127
+ apollo: {
128
+ describe: 'Support Apollo directives',
129
+ type: 'boolean',
130
+ default: false,
131
+ },
132
+ keepClientFields: {
133
+ describe: 'Keeps the fields with @client, but removes @client directive from them',
134
+ type: 'boolean',
135
+ default: false,
136
+ },
137
+ filter: {
138
+ describe: 'Show results only from a list of files (or file)',
139
+ array: true,
140
+ type: 'string',
141
+ },
142
+ ignore: {
143
+ describe: 'Ignore and do not load these files (supports glob)',
144
+ array: true,
145
+ type: 'string',
146
+ },
147
+ onlyErrors: {
148
+ describe: 'Show only errors',
149
+ type: 'boolean',
150
+ default: false,
151
+ },
152
+ relativePaths: {
153
+ describe: 'Show relative paths',
154
+ type: 'boolean',
155
+ default: false,
156
+ },
157
+ silent: {
158
+ describe: 'Do not print results',
159
+ type: 'boolean',
160
+ default: false,
161
+ },
162
+ output: {
163
+ describe: 'Output JSON file',
164
+ type: 'string',
165
+ },
166
+ });
167
+ },
168
+ handler(args) {
169
+ var _a;
170
+ return __awaiter(this, void 0, void 0, function* () {
171
+ const { headers, token } = parseGlobalArgs(args);
172
+ const apollo = args.apollo || false;
173
+ const aws = args.aws || false;
174
+ const apolloFederation = args.federation || false;
175
+ const method = ((_a = args.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'POST';
176
+ const maxDepth = args.maxDepth != null ? args.maxDepth : undefined;
177
+ const maxAliasCount = args.maxAliasCount != null ? args.maxAliasCount : undefined;
178
+ const maxDirectiveCount = args.maxDirectiveCount != null ? args.maxDirectiveCount : undefined;
179
+ const maxTokenCount = args.maxTokenCount != null ? args.maxTokenCount : undefined;
180
+ const strictFragments = !args.noStrictFragments;
181
+ const keepClientFields = args.keepClientFields || false;
182
+ const failOnDeprecated = args.deprecated;
183
+ const output = args.output;
184
+ const silent = args.silent || false;
185
+ const relativePaths = args.relativePaths || false;
186
+ const onlyErrors = args.onlyErrors || false;
187
+ const ignore = args.ignore || [];
188
+ const schema = yield loaders.loadSchema(args.schema, {
189
+ headers,
190
+ token,
191
+ method,
192
+ }, apolloFederation, aws);
193
+ const documents = yield loaders.loadDocuments(args.documents, {
194
+ ignore,
195
+ });
196
+ return handler({
197
+ schema,
198
+ documents,
199
+ apollo,
200
+ maxDepth,
201
+ maxAliasCount,
202
+ maxDirectiveCount,
203
+ maxTokenCount,
204
+ strictFragments,
205
+ keepClientFields,
206
+ failOnDeprecated,
207
+ filter: args.filter,
208
+ silent,
209
+ output,
210
+ relativePaths,
211
+ onlyErrors,
212
+ });
213
+ });
214
+ },
215
+ };
216
+ });
217
+ function countErrors(invalidDocuments) {
218
+ if (invalidDocuments.length) {
219
+ return invalidDocuments.filter(doc => doc.errors && doc.errors.length).length;
220
+ }
221
+ return 0;
222
+ }
223
+ function countDeprecated(invalidDocuments) {
224
+ if (invalidDocuments.length) {
225
+ return invalidDocuments.filter(doc => doc.deprecated && doc.deprecated.length).length;
226
+ }
227
+ return 0;
228
+ }
229
+ function printInvalidDocuments(invalidDocuments, listKey, isError = false, silent = false) {
230
+ if (silent) {
231
+ return;
232
+ }
233
+ invalidDocuments.forEach(doc => {
234
+ if (doc.errors.length) {
235
+ renderErrors(doc.source.name, doc[listKey], isError).forEach(line => {
236
+ Logger.log(line);
237
+ });
238
+ }
239
+ });
240
+ }
241
+ function renderErrors(sourceName, errors, isError = false) {
242
+ const errorsAsString = errors.map(e => ` - ${bolderize(e.message)}`).join('\n');
243
+ return [
244
+ isError ? chalk.redBright('error') : chalk.yellowBright('warn'),
245
+ `in ${sourceName}:\n\n`,
246
+ errorsAsString,
247
+ '\n\n',
248
+ ];
249
+ }
250
+
251
+ export default index;
252
+ export { handler };
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@graphql-inspector/validate-command",
3
- "version": "0.0.0-canary.58ffd99",
3
+ "version": "0.0.0-canary.7db8980",
4
4
  "description": "Validate Documents in GraphQL Inspector",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
7
- "graphql": "^0.13.0 || ^14.0.0 || ^15.0.0"
7
+ "graphql": "^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {
10
- "@graphql-inspector/commands": "0.0.0-canary.58ffd99",
11
- "@graphql-inspector/core": "0.0.0-canary.58ffd99",
12
- "@graphql-inspector/logger": "0.0.0-canary.58ffd99",
10
+ "@graphql-inspector/commands": "0.0.0-canary.7db8980",
11
+ "@graphql-inspector/core": "0.0.0-canary.7db8980",
12
+ "@graphql-inspector/logger": "0.0.0-canary.7db8980",
13
13
  "tslib": "^2.0.0"
14
14
  },
15
15
  "repository": {
@@ -29,10 +29,20 @@
29
29
  "url": "https://github.com/kamilkisiela"
30
30
  },
31
31
  "license": "MIT",
32
- "main": "index.cjs.js",
33
- "module": "index.esm.js",
32
+ "main": "index.js",
33
+ "module": "index.mjs",
34
34
  "typings": "index.d.ts",
35
35
  "typescript": {
36
36
  "definition": "index.d.ts"
37
+ },
38
+ "exports": {
39
+ ".": {
40
+ "require": "./index.js",
41
+ "import": "./index.mjs"
42
+ },
43
+ "./*": {
44
+ "require": "./*.js",
45
+ "import": "./*.mjs"
46
+ }
37
47
  }
38
48
  }
package/index.cjs.js DELETED
@@ -1,161 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const tslib = require('tslib');
6
- const commands = require('@graphql-inspector/commands');
7
- const logger = require('@graphql-inspector/logger');
8
- const core = require('@graphql-inspector/core');
9
- const graphql = require('graphql');
10
-
11
- function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, }) {
12
- const invalidDocuments = core.validate(schema, documents.map((doc) => new graphql.Source(graphql.print(doc.document), doc.location)), {
13
- strictFragments,
14
- maxDepth,
15
- apollo,
16
- keepClientFields,
17
- });
18
- if (!invalidDocuments.length) {
19
- logger.Logger.success('All documents are valid');
20
- }
21
- else {
22
- const errorsCount = countErrors(invalidDocuments);
23
- const deprecated = countDeprecated(invalidDocuments);
24
- if (errorsCount) {
25
- logger.Logger.log(`\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\n`);
26
- invalidDocuments.forEach((doc) => {
27
- if (doc.errors.length) {
28
- renderInvalidDocument(doc).forEach((line) => {
29
- logger.Logger.log(line);
30
- });
31
- }
32
- });
33
- }
34
- else if (!failOnDeprecated) {
35
- logger.Logger.success('All documents are valid');
36
- }
37
- if (deprecated) {
38
- logger.Logger.info(`\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\n`);
39
- invalidDocuments.forEach((doc) => {
40
- if (doc.deprecated.length) {
41
- renderDeprecatedUsageInDocument(doc, failOnDeprecated).forEach((line) => {
42
- logger.Logger.log(line);
43
- });
44
- }
45
- });
46
- }
47
- if (errorsCount || (deprecated && failOnDeprecated)) {
48
- process.exit(1);
49
- }
50
- }
51
- }
52
- const index = commands.createCommand((api) => {
53
- const { loaders } = api;
54
- return {
55
- command: 'validate <documents> <schema>',
56
- describe: 'Validate Fragments and Operations',
57
- builder(yargs) {
58
- return yargs
59
- .positional('schema', {
60
- describe: 'Point to a schema',
61
- type: 'string',
62
- demandOption: true,
63
- })
64
- .positional('documents', {
65
- describe: 'Point to docuents',
66
- type: 'string',
67
- demandOption: true,
68
- })
69
- .options({
70
- deprecated: {
71
- alias: 'd',
72
- describe: 'Fail on deprecated usage',
73
- type: 'boolean',
74
- default: false,
75
- },
76
- noStrictFragments: {
77
- describe: 'Do not fail on duplicated fragment names',
78
- type: 'boolean',
79
- default: false,
80
- },
81
- maxDepth: {
82
- describe: 'Fail on deep operations',
83
- type: 'number',
84
- },
85
- apollo: {
86
- describe: 'Support Apollo directives',
87
- type: 'boolean',
88
- default: false,
89
- },
90
- keepClientFields: {
91
- describe: 'Keeps the fields with @client, but removes @client directive from them',
92
- type: 'boolean',
93
- default: false,
94
- },
95
- });
96
- },
97
- handler(args) {
98
- return tslib.__awaiter(this, void 0, void 0, function* () {
99
- const { headers, token } = commands.parseGlobalArgs(args);
100
- const apollo = args.apollo || false;
101
- const maxDepth = args.maxDepth || undefined;
102
- const strictFragments = !args.noStrictFragments;
103
- const keepClientFields = args.keepClientFields || false;
104
- const failOnDeprecated = args.deprecated;
105
- const schema = yield loaders.loadSchema(args.schema, {
106
- headers,
107
- token,
108
- });
109
- const documents = yield loaders.loadDocuments(args.documents);
110
- return handler({
111
- schema,
112
- documents,
113
- apollo,
114
- maxDepth,
115
- strictFragments,
116
- keepClientFields,
117
- failOnDeprecated,
118
- });
119
- });
120
- },
121
- };
122
- });
123
- function countErrors(invalidDocuments) {
124
- if (invalidDocuments.length) {
125
- return invalidDocuments.filter((doc) => doc.errors && doc.errors.length)
126
- .length;
127
- }
128
- return 0;
129
- }
130
- function countDeprecated(invalidDocuments) {
131
- if (invalidDocuments.length) {
132
- return invalidDocuments.filter((doc) => doc.deprecated && doc.deprecated.length).length;
133
- }
134
- return 0;
135
- }
136
- function renderInvalidDocument(invalidDoc) {
137
- const errors = invalidDoc.errors
138
- .map((e) => ` - ${logger.bolderize(e.message)}`)
139
- .join('\n');
140
- return [
141
- logger.chalk.redBright('error'),
142
- `in ${invalidDoc.source.name}:\n\n`,
143
- errors,
144
- '\n\n',
145
- ];
146
- }
147
- function renderDeprecatedUsageInDocument(invalidDoc, isCritical = false) {
148
- const deprecated = invalidDoc.deprecated
149
- .map((e) => ` - ${logger.bolderize(e.message)}`)
150
- .join('\n');
151
- return [
152
- isCritical ? logger.chalk.redBright('error') : logger.chalk.yellowBright('warn'),
153
- `in ${invalidDoc.source.name}:\n\n`,
154
- deprecated,
155
- '\n\n',
156
- ];
157
- }
158
-
159
- exports.default = index;
160
- exports.handler = handler;
161
- //# sourceMappingURL=index.cjs.js.map
package/index.cjs.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../../dist/commands/validate/src/index.js"],"sourcesContent":["import { __awaiter } from \"tslib\";\nimport { createCommand, parseGlobalArgs, } from '@graphql-inspector/commands';\nimport { Logger, bolderize, chalk } from '@graphql-inspector/logger';\nimport { validate as validateDocuments, } from '@graphql-inspector/core';\nimport { Source, print } from 'graphql';\nexport function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, }) {\n const invalidDocuments = validateDocuments(schema, documents.map((doc) => new Source(print(doc.document), doc.location)), {\n strictFragments,\n maxDepth,\n apollo,\n keepClientFields,\n });\n if (!invalidDocuments.length) {\n Logger.success('All documents are valid');\n }\n else {\n const errorsCount = countErrors(invalidDocuments);\n const deprecated = countDeprecated(invalidDocuments);\n if (errorsCount) {\n Logger.log(`\\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\\n`);\n invalidDocuments.forEach((doc) => {\n if (doc.errors.length) {\n renderInvalidDocument(doc).forEach((line) => {\n Logger.log(line);\n });\n }\n });\n }\n else if (!failOnDeprecated) {\n Logger.success('All documents are valid');\n }\n if (deprecated) {\n Logger.info(`\\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\\n`);\n invalidDocuments.forEach((doc) => {\n if (doc.deprecated.length) {\n renderDeprecatedUsageInDocument(doc, failOnDeprecated).forEach((line) => {\n Logger.log(line);\n });\n }\n });\n }\n if (errorsCount || (deprecated && failOnDeprecated)) {\n process.exit(1);\n }\n }\n}\nexport default createCommand((api) => {\n const { loaders } = api;\n return {\n command: 'validate <documents> <schema>',\n describe: 'Validate Fragments and Operations',\n builder(yargs) {\n return yargs\n .positional('schema', {\n describe: 'Point to a schema',\n type: 'string',\n demandOption: true,\n })\n .positional('documents', {\n describe: 'Point to docuents',\n type: 'string',\n demandOption: true,\n })\n .options({\n deprecated: {\n alias: 'd',\n describe: 'Fail on deprecated usage',\n type: 'boolean',\n default: false,\n },\n noStrictFragments: {\n describe: 'Do not fail on duplicated fragment names',\n type: 'boolean',\n default: false,\n },\n maxDepth: {\n describe: 'Fail on deep operations',\n type: 'number',\n },\n apollo: {\n describe: 'Support Apollo directives',\n type: 'boolean',\n default: false,\n },\n keepClientFields: {\n describe: 'Keeps the fields with @client, but removes @client directive from them',\n type: 'boolean',\n default: false,\n },\n });\n },\n handler(args) {\n return __awaiter(this, void 0, void 0, function* () {\n const { headers, token } = parseGlobalArgs(args);\n const apollo = args.apollo || false;\n const maxDepth = args.maxDepth || undefined;\n const strictFragments = !args.noStrictFragments;\n const keepClientFields = args.keepClientFields || false;\n const failOnDeprecated = args.deprecated;\n const schema = yield loaders.loadSchema(args.schema, {\n headers,\n token,\n });\n const documents = yield loaders.loadDocuments(args.documents);\n return handler({\n schema,\n documents,\n apollo,\n maxDepth,\n strictFragments,\n keepClientFields,\n failOnDeprecated,\n });\n });\n },\n };\n});\nfunction countErrors(invalidDocuments) {\n if (invalidDocuments.length) {\n return invalidDocuments.filter((doc) => doc.errors && doc.errors.length)\n .length;\n }\n return 0;\n}\nfunction countDeprecated(invalidDocuments) {\n if (invalidDocuments.length) {\n return invalidDocuments.filter((doc) => doc.deprecated && doc.deprecated.length).length;\n }\n return 0;\n}\nfunction renderInvalidDocument(invalidDoc) {\n const errors = invalidDoc.errors\n .map((e) => ` - ${bolderize(e.message)}`)\n .join('\\n');\n return [\n chalk.redBright('error'),\n `in ${invalidDoc.source.name}:\\n\\n`,\n errors,\n '\\n\\n',\n ];\n}\nfunction renderDeprecatedUsageInDocument(invalidDoc, isCritical = false) {\n const deprecated = invalidDoc.deprecated\n .map((e) => ` - ${bolderize(e.message)}`)\n .join('\\n');\n return [\n isCritical ? chalk.redBright('error') : chalk.yellowBright('warn'),\n `in ${invalidDoc.source.name}:\\n\\n`,\n deprecated,\n '\\n\\n',\n ];\n}\n//# sourceMappingURL=index.js.map"],"names":["validateDocuments","Source","print","Logger","createCommand","__awaiter","parseGlobalArgs","bolderize","chalk"],"mappings":";;;;;;;;;;AAKO,SAAS,OAAO,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,EAAE,gBAAgB,EAAE,gBAAgB,GAAG,EAAE;AACvH,IAAI,MAAM,gBAAgB,GAAGA,aAAiB,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,IAAIC,cAAM,CAACC,aAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,EAAE;AAC9H,QAAQ,eAAe;AACvB,QAAQ,Q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;AAChC,QAAQ,CAAC,GAAG,EAAE,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;AAC3C,QAAQ,MAAM;AACd,QAAQ,MAAM;AACd,KAAK,CAAC;AACN,CAAC;AACD,SAAS,+BAA+B,CAAC,UAAU,EAAE,UAAU,GAAG,KAAK,EAAE;AACzE,IAAI,MAAM,UAAU,GAAG,UAAU,CAAC,UAAU;AAC5C,SAAS,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAED,gBAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AACjD,SAAS,IAAI,CAAC,IAAI,CAAC,CAAC;AACpB,IAAI,OAAO;AACX,QAAQ,UAAU,GAAGC,YAAK,CAAC,SAAS,CAAC,OAAO,CAAC,GAAGA,YAAK,CAAC,YAAY,CAAC,MAAM,CAAC;AAC1E,QAAQ,CAAC,GAAG,EAAE,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;AAC3C,QAAQ,UAAU;AAClB,QAAQ,MAAM;AACd,KAAK,CAAC;AACN;;;;;"}
package/index.esm.js DELETED
@@ -1,157 +0,0 @@
1
- import { __awaiter } from 'tslib';
2
- import { createCommand, parseGlobalArgs } from '@graphql-inspector/commands';
3
- import { Logger, chalk, bolderize } from '@graphql-inspector/logger';
4
- import { validate } from '@graphql-inspector/core';
5
- import { Source, print } from 'graphql';
6
-
7
- function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, }) {
8
- const invalidDocuments = validate(schema, documents.map((doc) => new Source(print(doc.document), doc.location)), {
9
- strictFragments,
10
- maxDepth,
11
- apollo,
12
- keepClientFields,
13
- });
14
- if (!invalidDocuments.length) {
15
- Logger.success('All documents are valid');
16
- }
17
- else {
18
- const errorsCount = countErrors(invalidDocuments);
19
- const deprecated = countDeprecated(invalidDocuments);
20
- if (errorsCount) {
21
- Logger.log(`\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\n`);
22
- invalidDocuments.forEach((doc) => {
23
- if (doc.errors.length) {
24
- renderInvalidDocument(doc).forEach((line) => {
25
- Logger.log(line);
26
- });
27
- }
28
- });
29
- }
30
- else if (!failOnDeprecated) {
31
- Logger.success('All documents are valid');
32
- }
33
- if (deprecated) {
34
- Logger.info(`\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\n`);
35
- invalidDocuments.forEach((doc) => {
36
- if (doc.deprecated.length) {
37
- renderDeprecatedUsageInDocument(doc, failOnDeprecated).forEach((line) => {
38
- Logger.log(line);
39
- });
40
- }
41
- });
42
- }
43
- if (errorsCount || (deprecated && failOnDeprecated)) {
44
- process.exit(1);
45
- }
46
- }
47
- }
48
- const index = createCommand((api) => {
49
- const { loaders } = api;
50
- return {
51
- command: 'validate <documents> <schema>',
52
- describe: 'Validate Fragments and Operations',
53
- builder(yargs) {
54
- return yargs
55
- .positional('schema', {
56
- describe: 'Point to a schema',
57
- type: 'string',
58
- demandOption: true,
59
- })
60
- .positional('documents', {
61
- describe: 'Point to docuents',
62
- type: 'string',
63
- demandOption: true,
64
- })
65
- .options({
66
- deprecated: {
67
- alias: 'd',
68
- describe: 'Fail on deprecated usage',
69
- type: 'boolean',
70
- default: false,
71
- },
72
- noStrictFragments: {
73
- describe: 'Do not fail on duplicated fragment names',
74
- type: 'boolean',
75
- default: false,
76
- },
77
- maxDepth: {
78
- describe: 'Fail on deep operations',
79
- type: 'number',
80
- },
81
- apollo: {
82
- describe: 'Support Apollo directives',
83
- type: 'boolean',
84
- default: false,
85
- },
86
- keepClientFields: {
87
- describe: 'Keeps the fields with @client, but removes @client directive from them',
88
- type: 'boolean',
89
- default: false,
90
- },
91
- });
92
- },
93
- handler(args) {
94
- return __awaiter(this, void 0, void 0, function* () {
95
- const { headers, token } = parseGlobalArgs(args);
96
- const apollo = args.apollo || false;
97
- const maxDepth = args.maxDepth || undefined;
98
- const strictFragments = !args.noStrictFragments;
99
- const keepClientFields = args.keepClientFields || false;
100
- const failOnDeprecated = args.deprecated;
101
- const schema = yield loaders.loadSchema(args.schema, {
102
- headers,
103
- token,
104
- });
105
- const documents = yield loaders.loadDocuments(args.documents);
106
- return handler({
107
- schema,
108
- documents,
109
- apollo,
110
- maxDepth,
111
- strictFragments,
112
- keepClientFields,
113
- failOnDeprecated,
114
- });
115
- });
116
- },
117
- };
118
- });
119
- function countErrors(invalidDocuments) {
120
- if (invalidDocuments.length) {
121
- return invalidDocuments.filter((doc) => doc.errors && doc.errors.length)
122
- .length;
123
- }
124
- return 0;
125
- }
126
- function countDeprecated(invalidDocuments) {
127
- if (invalidDocuments.length) {
128
- return invalidDocuments.filter((doc) => doc.deprecated && doc.deprecated.length).length;
129
- }
130
- return 0;
131
- }
132
- function renderInvalidDocument(invalidDoc) {
133
- const errors = invalidDoc.errors
134
- .map((e) => ` - ${bolderize(e.message)}`)
135
- .join('\n');
136
- return [
137
- chalk.redBright('error'),
138
- `in ${invalidDoc.source.name}:\n\n`,
139
- errors,
140
- '\n\n',
141
- ];
142
- }
143
- function renderDeprecatedUsageInDocument(invalidDoc, isCritical = false) {
144
- const deprecated = invalidDoc.deprecated
145
- .map((e) => ` - ${bolderize(e.message)}`)
146
- .join('\n');
147
- return [
148
- isCritical ? chalk.redBright('error') : chalk.yellowBright('warn'),
149
- `in ${invalidDoc.source.name}:\n\n`,
150
- deprecated,
151
- '\n\n',
152
- ];
153
- }
154
-
155
- export default index;
156
- export { handler };
157
- //# sourceMappingURL=index.esm.js.map
package/index.esm.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.esm.js","sources":["../../dist/commands/validate/src/index.js"],"sourcesContent":["import { __awaiter } from \"tslib\";\nimport { createCommand, parseGlobalArgs, } from '@graphql-inspector/commands';\nimport { Logger, bolderize, chalk } from '@graphql-inspector/logger';\nimport { validate as validateDocuments, } from '@graphql-inspector/core';\nimport { Source, print } from 'graphql';\nexport function handler({ schema, documents, strictFragments, maxDepth, apollo, keepClientFields, failOnDeprecated, }) {\n const invalidDocuments = validateDocuments(schema, documents.map((doc) => new Source(print(doc.document), doc.location)), {\n strictFragments,\n maxDepth,\n apollo,\n keepClientFields,\n });\n if (!invalidDocuments.length) {\n Logger.success('All documents are valid');\n }\n else {\n const errorsCount = countErrors(invalidDocuments);\n const deprecated = countDeprecated(invalidDocuments);\n if (errorsCount) {\n Logger.log(`\\nDetected ${errorsCount} invalid document${errorsCount > 1 ? 's' : ''}:\\n`);\n invalidDocuments.forEach((doc) => {\n if (doc.errors.length) {\n renderInvalidDocument(doc).forEach((line) => {\n Logger.log(line);\n });\n }\n });\n }\n else if (!failOnDeprecated) {\n Logger.success('All documents are valid');\n }\n if (deprecated) {\n Logger.info(`\\nDetected ${deprecated} document${deprecated > 1 ? 's' : ''} with deprecated fields:\\n`);\n invalidDocuments.forEach((doc) => {\n if (doc.deprecated.length) {\n renderDeprecatedUsageInDocument(doc, failOnDeprecated).forEach((line) => {\n Logger.log(line);\n });\n }\n });\n }\n if (errorsCount || (deprecated && failOnDeprecated)) {\n process.exit(1);\n }\n }\n}\nexport default createCommand((api) => {\n const { loaders } = api;\n return {\n command: 'validate <documents> <schema>',\n describe: 'Validate Fragments and Operations',\n builder(yargs) {\n return yargs\n .positional('schema', {\n describe: 'Point to a schema',\n type: 'string',\n demandOption: true,\n })\n .positional('documents', {\n describe: 'Point to docuents',\n type: 'string',\n demandOption: true,\n })\n .options({\n deprecated: {\n alias: 'd',\n describe: 'Fail on deprecated usage',\n type: 'boolean',\n default: false,\n },\n noStrictFragments: {\n describe: 'Do not fail on duplicated fragment names',\n type: 'boolean',\n default: false,\n },\n maxDepth: {\n describe: 'Fail on deep operations',\n type: 'number',\n },\n apollo: {\n describe: 'Support Apollo directives',\n type: 'boolean',\n default: false,\n },\n keepClientFields: {\n describe: 'Keeps the fields with @client, but removes @client directive from them',\n type: 'boolean',\n default: false,\n },\n });\n },\n handler(args) {\n return __awaiter(this, void 0, void 0, function* () {\n const { headers, token } = parseGlobalArgs(args);\n const apollo = args.apollo || false;\n const maxDepth = args.maxDepth || undefined;\n const strictFragments = !args.noStrictFragments;\n const keepClientFields = args.keepClientFields || false;\n const failOnDeprecated = args.deprecated;\n const schema = yield loaders.loadSchema(args.schema, {\n headers,\n token,\n });\n const documents = yield loaders.loadDocuments(args.documents);\n return handler({\n schema,\n documents,\n apollo,\n maxDepth,\n strictFragments,\n keepClientFields,\n failOnDeprecated,\n });\n });\n },\n };\n});\nfunction countErrors(invalidDocuments) {\n if (invalidDocuments.length) {\n return invalidDocuments.filter((doc) => doc.errors && doc.errors.length)\n .length;\n }\n return 0;\n}\nfunction countDeprecated(invalidDocuments) {\n if (invalidDocuments.length) {\n return invalidDocuments.filter((doc) => doc.deprecated && doc.deprecated.length).length;\n }\n return 0;\n}\nfunction renderInvalidDocument(invalidDoc) {\n const errors = invalidDoc.errors\n .map((e) => ` - ${bolderize(e.message)}`)\n .join('\\n');\n return [\n chalk.redBright('error'),\n `in ${invalidDoc.source.name}:\\n\\n`,\n errors,\n '\\n\\n',\n ];\n}\nfunction renderDeprecatedUsageInDocument(invalidDoc, isCritical = false) {\n const deprecated = invalidDoc.deprecated\n .map((e) => ` - ${bolderize(e.message)}`)\n .join('\\n');\n return [\n isCritical ? chalk.redBright('error') : chalk.yellowBright('warn'),\n `in ${invalidDoc.source.name}:\\n\\n`,\n deprecated,\n '\\n\\n',\n ];\n}\n//# sourceMappingURL=index.js.map"],"names":["validateDocuments"],"mappings":";;;;;;AAKO,SAAS,OAAO,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,EAAE,gBAAgB,EAAE,gBAAgB,GAAG,EAAE;AACvH,IAAI,MAAM,gBAAgB,GAAGA,QAAiB,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,EAAE;A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