@graphql-inspector/audit-command 0.0.0-canary.36b26d2

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 +16 -0
  2. package/index.js +113 -0
  3. package/index.mjs +107 -0
  4. package/package.json +49 -0
package/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { GlobalArgs } from '@graphql-inspector/commands';
2
+ import type { CalculateOperationComplexityConfig } from '@graphql-inspector/core';
3
+ import { Source as DocumentSource } from '@graphql-tools/utils';
4
+ declare const _default: import("@graphql-inspector/commands").CommandFactory<{}, {
5
+ documents: string;
6
+ detail: boolean;
7
+ complexityScalarCost: number;
8
+ complexityObjectCost: number;
9
+ complexityDepthCostFactor: number;
10
+ } & GlobalArgs>;
11
+ export default _default;
12
+ export declare function handler(args: {
13
+ documents: DocumentSource[];
14
+ detail: boolean;
15
+ complexityConfig: CalculateOperationComplexityConfig;
16
+ }): void;
package/index.js ADDED
@@ -0,0 +1,113 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
+
7
+ const tslib = require('tslib');
8
+ const commands = require('@graphql-inspector/commands');
9
+ const core = require('@graphql-inspector/core');
10
+ const logger = require('@graphql-inspector/logger');
11
+ const Table = _interopDefault(require('cli-table3'));
12
+
13
+ const index = commands.createCommand(api => {
14
+ return {
15
+ command: 'audit <documents>',
16
+ describe: 'Audit Fragments and Operations for a better understanding of the depth, alias count, and directive count.',
17
+ builder(yargs) {
18
+ return yargs
19
+ .positional('documents', {
20
+ describe: 'Point to some documents',
21
+ type: 'string',
22
+ demandOption: true,
23
+ })
24
+ .options({
25
+ detail: {
26
+ alias: 'd',
27
+ describe: 'Print an overview of all operations and their audit breakdown.',
28
+ type: 'boolean',
29
+ default: false,
30
+ },
31
+ complexityScalarCost: {
32
+ describe: 'The cost per scalar for calculating the complexity score.',
33
+ type: 'number',
34
+ default: 1,
35
+ },
36
+ complexityObjectCost: {
37
+ describe: 'The cost per object for calculating the complexity score.',
38
+ type: 'number',
39
+ default: 2,
40
+ },
41
+ complexityDepthCostFactor: {
42
+ describe: 'The cost factor per introduced depth level for calculating the complexity score.',
43
+ type: 'number',
44
+ default: 1.5,
45
+ },
46
+ });
47
+ },
48
+ handler(args) {
49
+ return tslib.__awaiter(this, void 0, void 0, function* () {
50
+ const { loaders } = api;
51
+ const ignore = args.ignore || [];
52
+ const documents = yield loaders.loadDocuments(args.documents, {
53
+ ignore,
54
+ });
55
+ const complexityConfig = {
56
+ scalarCost: args.complexityScalarCost,
57
+ objectCost: args.complexityObjectCost,
58
+ depthCostFactor: args.complexityDepthCostFactor,
59
+ };
60
+ return handler({ documents, detail: args.detail, complexityConfig });
61
+ });
62
+ },
63
+ };
64
+ });
65
+ function handler(args) {
66
+ const fragments = new Map();
67
+ const operations = new Map();
68
+ const getFragmentReference = (fragmentName) => fragments.get(fragmentName);
69
+ for (const record of args.documents) {
70
+ if (record.document) {
71
+ for (const definition of record.document.definitions) {
72
+ if (definition.kind === 'FragmentDefinition') {
73
+ fragments.set(definition.name.value, definition);
74
+ }
75
+ else if (definition.kind === 'OperationDefinition') {
76
+ if (definition.name) {
77
+ operations.set(definition.name.value, definition);
78
+ }
79
+ }
80
+ }
81
+ }
82
+ }
83
+ let maxDepth = 0;
84
+ let maxAliases = 0;
85
+ let maxDirectives = 0;
86
+ let maxComplexity = 0;
87
+ const results = [];
88
+ for (const [name, operation] of operations.entries()) {
89
+ const depth = core.countDepth(operation, 0, getFragmentReference);
90
+ const aliases = core.countAliases(operation, getFragmentReference);
91
+ const directives = core.countDirectives(operation, getFragmentReference);
92
+ const complexity = core.calculateOperationComplexity(operation, args.complexityConfig, getFragmentReference);
93
+ results.push([name, depth, aliases, directives, complexity]);
94
+ maxDepth = Math.max(maxDepth, depth);
95
+ maxAliases = Math.max(maxAliases, aliases);
96
+ maxDirectives = Math.max(maxDirectives, directives);
97
+ maxComplexity = Math.max(maxComplexity, complexity);
98
+ }
99
+ if (args.detail) {
100
+ const table = new Table({
101
+ head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Complexity Score'],
102
+ });
103
+ table.push(...results);
104
+ logger.Logger.log(table.toString());
105
+ }
106
+ logger.Logger.log(`Maximum depth is ${logger.chalk.bold(maxDepth)}`);
107
+ logger.Logger.log(`Maximum alias amount is ${logger.chalk.bold(maxAliases)}`);
108
+ logger.Logger.log(`Maximum directive amount is ${logger.chalk.bold(maxDirectives)}`);
109
+ logger.Logger.log(`Maximum complexity score is ${logger.chalk.bold(maxComplexity)}`);
110
+ }
111
+
112
+ exports.default = index;
113
+ exports.handler = handler;
package/index.mjs ADDED
@@ -0,0 +1,107 @@
1
+ import { __awaiter } from 'tslib';
2
+ import { createCommand } from '@graphql-inspector/commands';
3
+ import { countDepth, countAliases, countDirectives, calculateOperationComplexity } from '@graphql-inspector/core';
4
+ import { Logger, chalk } from '@graphql-inspector/logger';
5
+ import Table from 'cli-table3';
6
+
7
+ const index = createCommand(api => {
8
+ return {
9
+ command: 'audit <documents>',
10
+ describe: 'Audit Fragments and Operations for a better understanding of the depth, alias count, and directive count.',
11
+ builder(yargs) {
12
+ return yargs
13
+ .positional('documents', {
14
+ describe: 'Point to some documents',
15
+ type: 'string',
16
+ demandOption: true,
17
+ })
18
+ .options({
19
+ detail: {
20
+ alias: 'd',
21
+ describe: 'Print an overview of all operations and their audit breakdown.',
22
+ type: 'boolean',
23
+ default: false,
24
+ },
25
+ complexityScalarCost: {
26
+ describe: 'The cost per scalar for calculating the complexity score.',
27
+ type: 'number',
28
+ default: 1,
29
+ },
30
+ complexityObjectCost: {
31
+ describe: 'The cost per object for calculating the complexity score.',
32
+ type: 'number',
33
+ default: 2,
34
+ },
35
+ complexityDepthCostFactor: {
36
+ describe: 'The cost factor per introduced depth level for calculating the complexity score.',
37
+ type: 'number',
38
+ default: 1.5,
39
+ },
40
+ });
41
+ },
42
+ handler(args) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const { loaders } = api;
45
+ const ignore = args.ignore || [];
46
+ const documents = yield loaders.loadDocuments(args.documents, {
47
+ ignore,
48
+ });
49
+ const complexityConfig = {
50
+ scalarCost: args.complexityScalarCost,
51
+ objectCost: args.complexityObjectCost,
52
+ depthCostFactor: args.complexityDepthCostFactor,
53
+ };
54
+ return handler({ documents, detail: args.detail, complexityConfig });
55
+ });
56
+ },
57
+ };
58
+ });
59
+ function handler(args) {
60
+ const fragments = new Map();
61
+ const operations = new Map();
62
+ const getFragmentReference = (fragmentName) => fragments.get(fragmentName);
63
+ for (const record of args.documents) {
64
+ if (record.document) {
65
+ for (const definition of record.document.definitions) {
66
+ if (definition.kind === 'FragmentDefinition') {
67
+ fragments.set(definition.name.value, definition);
68
+ }
69
+ else if (definition.kind === 'OperationDefinition') {
70
+ if (definition.name) {
71
+ operations.set(definition.name.value, definition);
72
+ }
73
+ }
74
+ }
75
+ }
76
+ }
77
+ let maxDepth = 0;
78
+ let maxAliases = 0;
79
+ let maxDirectives = 0;
80
+ let maxComplexity = 0;
81
+ const results = [];
82
+ for (const [name, operation] of operations.entries()) {
83
+ const depth = countDepth(operation, 0, getFragmentReference);
84
+ const aliases = countAliases(operation, getFragmentReference);
85
+ const directives = countDirectives(operation, getFragmentReference);
86
+ const complexity = calculateOperationComplexity(operation, args.complexityConfig, getFragmentReference);
87
+ results.push([name, depth, aliases, directives, complexity]);
88
+ maxDepth = Math.max(maxDepth, depth);
89
+ maxAliases = Math.max(maxAliases, aliases);
90
+ maxDirectives = Math.max(maxDirectives, directives);
91
+ maxComplexity = Math.max(maxComplexity, complexity);
92
+ }
93
+ if (args.detail) {
94
+ const table = new Table({
95
+ head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Complexity Score'],
96
+ });
97
+ table.push(...results);
98
+ Logger.log(table.toString());
99
+ }
100
+ Logger.log(`Maximum depth is ${chalk.bold(maxDepth)}`);
101
+ Logger.log(`Maximum alias amount is ${chalk.bold(maxAliases)}`);
102
+ Logger.log(`Maximum directive amount is ${chalk.bold(maxDirectives)}`);
103
+ Logger.log(`Maximum complexity score is ${chalk.bold(maxComplexity)}`);
104
+ }
105
+
106
+ export default index;
107
+ export { handler };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@graphql-inspector/audit-command",
3
+ "version": "0.0.0-canary.36b26d2",
4
+ "description": "Audit Documents 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-canary.36b26d2",
11
+ "@graphql-inspector/core": "0.0.0-canary.36b26d2",
12
+ "@graphql-inspector/logger": "0.0.0-canary.36b26d2",
13
+ "cli-table3": "0.6.2",
14
+ "tslib": "^2.0.0"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "kamilkisiela/graphql-inspector",
19
+ "directory": "packages/commands/audit"
20
+ },
21
+ "keywords": [
22
+ "graphql",
23
+ "graphql-inspector",
24
+ "graphql-inspector-command",
25
+ "tools"
26
+ ],
27
+ "author": {
28
+ "name": "Laurin Quast",
29
+ "email": "laurinquast@googlemail.com",
30
+ "url": "https://github.com/kamilkisiela"
31
+ },
32
+ "license": "MIT",
33
+ "main": "index.js",
34
+ "module": "index.mjs",
35
+ "typings": "index.d.ts",
36
+ "typescript": {
37
+ "definition": "index.d.ts"
38
+ },
39
+ "exports": {
40
+ ".": {
41
+ "require": "./index.js",
42
+ "import": "./index.mjs"
43
+ },
44
+ "./*": {
45
+ "require": "./*.js",
46
+ "import": "./*.mjs"
47
+ }
48
+ }
49
+ }