@graphql-inspector/coverage-command 0.0.0-PLACEHOLDER

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.
Files changed (4) hide show
  1. package/index.d.ts +17 -0
  2. package/index.js +129 -0
  3. package/index.mjs +125 -0
  4. package/package.json +48 -0
package/index.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { GlobalArgs, CommandFactory } from '@graphql-inspector/commands';
2
+ import { Source as DocumentSource } from '@graphql-tools/utils';
3
+ import { GraphQLSchema } from 'graphql';
4
+ export { CommandFactory };
5
+ export declare function handler({ schema, documents, silent, writePath, }: {
6
+ schema: GraphQLSchema;
7
+ documents: DocumentSource[];
8
+ silent?: boolean;
9
+ writePath?: string;
10
+ }): void;
11
+ declare const _default: CommandFactory<{}, {
12
+ schema: string;
13
+ documents: string;
14
+ write?: string | undefined;
15
+ silent?: boolean | undefined;
16
+ } & GlobalArgs>;
17
+ export default _default;
package/index.js ADDED
@@ -0,0 +1,129 @@
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
+ const path = require('path');
11
+ const fs = require('fs');
12
+
13
+ function handler({ schema, documents, silent, writePath, }) {
14
+ const shouldWrite = typeof writePath !== 'undefined';
15
+ const coverage = core.coverage(schema, documents.map((doc) => new graphql.Source(graphql.print(doc.document), doc.location)));
16
+ if (silent !== true) {
17
+ renderCoverage(coverage);
18
+ }
19
+ if (shouldWrite) {
20
+ if (typeof writePath !== 'string') {
21
+ throw new Error(`--write is not valid file path: ${writePath}`);
22
+ }
23
+ const absPath = commands.ensureAbsolute(writePath);
24
+ const ext = path.extname(absPath).replace('.', '').toLocaleLowerCase();
25
+ let output = undefined;
26
+ if (ext === 'json') {
27
+ output = outputJSON(coverage);
28
+ }
29
+ if (output) {
30
+ fs.writeFileSync(absPath, output, {
31
+ encoding: 'utf-8',
32
+ });
33
+ logger.Logger.success(`Available at ${absPath}\n`);
34
+ }
35
+ else {
36
+ throw new Error(`Extension ${ext} is not supported`);
37
+ }
38
+ }
39
+ }
40
+ const index = commands.createCommand((api) => {
41
+ const { loaders } = api;
42
+ return {
43
+ command: 'coverage <documents> <schema>',
44
+ describe: 'Schema coverage based on documents',
45
+ builder(yargs) {
46
+ return yargs
47
+ .positional('schema', {
48
+ describe: 'Point to a schema',
49
+ type: 'string',
50
+ demandOption: true,
51
+ })
52
+ .positional('documents', {
53
+ describe: 'Point to documents',
54
+ type: 'string',
55
+ demandOption: true,
56
+ })
57
+ .options({
58
+ w: {
59
+ alias: 'write',
60
+ describe: 'Write a file with coverage stats',
61
+ type: 'string',
62
+ },
63
+ s: {
64
+ alias: 'silent',
65
+ describe: 'Do not render any stats in the terminal',
66
+ type: 'boolean',
67
+ },
68
+ });
69
+ },
70
+ handler(args) {
71
+ var _a;
72
+ return tslib.__awaiter(this, void 0, void 0, function* () {
73
+ const writePath = args.write;
74
+ const silent = args.silent;
75
+ const { headers, token } = commands.parseGlobalArgs(args);
76
+ const apolloFederation = args.federation || false;
77
+ const aws = args.aws || false;
78
+ const method = ((_a = args.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'POST';
79
+ const schema = yield loaders.loadSchema(args.schema, {
80
+ token,
81
+ headers,
82
+ method,
83
+ }, apolloFederation, aws);
84
+ const documents = yield loaders.loadDocuments(args.documents);
85
+ return handler({ schema, documents, silent, writePath });
86
+ });
87
+ },
88
+ };
89
+ });
90
+ function outputJSON(coverage) {
91
+ return JSON.stringify(coverage, null, 2);
92
+ }
93
+ function renderCoverage(coverage) {
94
+ logger.Logger.info('Schema coverage based on documents:\n');
95
+ for (const typeName in coverage.types) {
96
+ if (coverage.types.hasOwnProperty(typeName)) {
97
+ const typeCoverage = coverage.types[typeName];
98
+ logger.Logger.log([
99
+ logger.chalk.grey(core.getTypePrefix(typeCoverage.type)),
100
+ logger.chalk.bold(`${typeName}`),
101
+ logger.chalk.grey('{'),
102
+ ].join(' '));
103
+ for (const childName in typeCoverage.children) {
104
+ if (typeCoverage.children.hasOwnProperty(childName)) {
105
+ const childCoverage = typeCoverage.children[childName];
106
+ if (childCoverage.hits) {
107
+ logger.Logger.log([
108
+ indent(childName, 2),
109
+ logger.chalk.italic.grey(`x ${childCoverage.hits}`),
110
+ ].join(' '));
111
+ }
112
+ else {
113
+ logger.Logger.log([
114
+ logger.chalk.redBright(indent(childName, 2)),
115
+ logger.chalk.italic.grey('x 0'),
116
+ ].join(' '));
117
+ }
118
+ }
119
+ }
120
+ logger.Logger.log(logger.chalk.grey('}\n'));
121
+ }
122
+ }
123
+ }
124
+ function indent(line, space) {
125
+ return line.padStart(line.length + space, ' ');
126
+ }
127
+
128
+ exports.default = index;
129
+ exports.handler = handler;
package/index.mjs ADDED
@@ -0,0 +1,125 @@
1
+ import { __awaiter } from 'tslib';
2
+ import { createCommand, parseGlobalArgs, ensureAbsolute } from '@graphql-inspector/commands';
3
+ import { Logger, chalk } from '@graphql-inspector/logger';
4
+ import { coverage, getTypePrefix } from '@graphql-inspector/core';
5
+ import { Source, print } from 'graphql';
6
+ import { extname } from 'path';
7
+ import { writeFileSync } from 'fs';
8
+
9
+ function handler({ schema, documents, silent, writePath, }) {
10
+ const shouldWrite = typeof writePath !== 'undefined';
11
+ const coverage$1 = coverage(schema, documents.map((doc) => new Source(print(doc.document), doc.location)));
12
+ if (silent !== true) {
13
+ renderCoverage(coverage$1);
14
+ }
15
+ if (shouldWrite) {
16
+ if (typeof writePath !== 'string') {
17
+ throw new Error(`--write is not valid file path: ${writePath}`);
18
+ }
19
+ const absPath = ensureAbsolute(writePath);
20
+ const ext = extname(absPath).replace('.', '').toLocaleLowerCase();
21
+ let output = undefined;
22
+ if (ext === 'json') {
23
+ output = outputJSON(coverage$1);
24
+ }
25
+ if (output) {
26
+ writeFileSync(absPath, output, {
27
+ encoding: 'utf-8',
28
+ });
29
+ Logger.success(`Available at ${absPath}\n`);
30
+ }
31
+ else {
32
+ throw new Error(`Extension ${ext} is not supported`);
33
+ }
34
+ }
35
+ }
36
+ const index = createCommand((api) => {
37
+ const { loaders } = api;
38
+ return {
39
+ command: 'coverage <documents> <schema>',
40
+ describe: 'Schema coverage based on documents',
41
+ builder(yargs) {
42
+ return yargs
43
+ .positional('schema', {
44
+ describe: 'Point to a schema',
45
+ type: 'string',
46
+ demandOption: true,
47
+ })
48
+ .positional('documents', {
49
+ describe: 'Point to documents',
50
+ type: 'string',
51
+ demandOption: true,
52
+ })
53
+ .options({
54
+ w: {
55
+ alias: 'write',
56
+ describe: 'Write a file with coverage stats',
57
+ type: 'string',
58
+ },
59
+ s: {
60
+ alias: 'silent',
61
+ describe: 'Do not render any stats in the terminal',
62
+ type: 'boolean',
63
+ },
64
+ });
65
+ },
66
+ handler(args) {
67
+ var _a;
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ const writePath = args.write;
70
+ const silent = args.silent;
71
+ const { headers, token } = parseGlobalArgs(args);
72
+ const apolloFederation = args.federation || false;
73
+ const aws = args.aws || false;
74
+ const method = ((_a = args.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'POST';
75
+ const schema = yield loaders.loadSchema(args.schema, {
76
+ token,
77
+ headers,
78
+ method,
79
+ }, apolloFederation, aws);
80
+ const documents = yield loaders.loadDocuments(args.documents);
81
+ return handler({ schema, documents, silent, writePath });
82
+ });
83
+ },
84
+ };
85
+ });
86
+ function outputJSON(coverage) {
87
+ return JSON.stringify(coverage, null, 2);
88
+ }
89
+ function renderCoverage(coverage) {
90
+ Logger.info('Schema coverage based on documents:\n');
91
+ for (const typeName in coverage.types) {
92
+ if (coverage.types.hasOwnProperty(typeName)) {
93
+ const typeCoverage = coverage.types[typeName];
94
+ Logger.log([
95
+ chalk.grey(getTypePrefix(typeCoverage.type)),
96
+ chalk.bold(`${typeName}`),
97
+ chalk.grey('{'),
98
+ ].join(' '));
99
+ for (const childName in typeCoverage.children) {
100
+ if (typeCoverage.children.hasOwnProperty(childName)) {
101
+ const childCoverage = typeCoverage.children[childName];
102
+ if (childCoverage.hits) {
103
+ Logger.log([
104
+ indent(childName, 2),
105
+ chalk.italic.grey(`x ${childCoverage.hits}`),
106
+ ].join(' '));
107
+ }
108
+ else {
109
+ Logger.log([
110
+ chalk.redBright(indent(childName, 2)),
111
+ chalk.italic.grey('x 0'),
112
+ ].join(' '));
113
+ }
114
+ }
115
+ }
116
+ Logger.log(chalk.grey('}\n'));
117
+ }
118
+ }
119
+ }
120
+ function indent(line, space) {
121
+ return line.padStart(line.length + space, ' ');
122
+ }
123
+
124
+ export default index;
125
+ export { handler };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@graphql-inspector/coverage-command",
3
+ "version": "0.0.0-PLACEHOLDER",
4
+ "description": "Schema Coverage in GraphQL Inspector",
5
+ "sideEffects": false,
6
+ "peerDependencies": {
7
+ "graphql": "^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
8
+ },
9
+ "dependencies": {
10
+ "@graphql-inspector/commands": "0.0.0-PLACEHOLDER",
11
+ "@graphql-inspector/core": "0.0.0-PLACEHOLDER",
12
+ "@graphql-inspector/logger": "0.0.0-PLACEHOLDER",
13
+ "tslib": "^2.0.0"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "kamilkisiela/graphql-inspector",
18
+ "directory": "packages/commands/coverage"
19
+ },
20
+ "keywords": [
21
+ "graphql",
22
+ "graphql-inspector",
23
+ "graphql-inspector-command",
24
+ "tools"
25
+ ],
26
+ "author": {
27
+ "name": "Kamil Kisiela",
28
+ "email": "kamil.kisiela@gmail.com",
29
+ "url": "https://github.com/kamilkisiela"
30
+ },
31
+ "license": "MIT",
32
+ "main": "index.js",
33
+ "module": "index.mjs",
34
+ "typings": "index.d.ts",
35
+ "typescript": {
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
+ }
47
+ }
48
+ }