@graphql-inspector/audit-command 0.0.0-canary.d95a8a3 → 3.4.0

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
@@ -1,11 +1,16 @@
1
1
  import { GlobalArgs } from '@graphql-inspector/commands';
2
+ import type { CalculateOperationComplexityConfig } from '@graphql-inspector/core';
2
3
  import { Source as DocumentSource } from '@graphql-tools/utils';
3
4
  declare const _default: import("@graphql-inspector/commands").CommandFactory<{}, {
4
5
  documents: string;
5
6
  detail: boolean;
7
+ complexityScalarCost: number;
8
+ complexityObjectCost: number;
9
+ complexityDepthCostFactor: number;
6
10
  } & GlobalArgs>;
7
11
  export default _default;
8
12
  export declare function handler(args: {
9
13
  documents: DocumentSource[];
10
14
  detail: boolean;
15
+ complexityConfig: CalculateOperationComplexityConfig;
11
16
  }): void;
package/index.js CHANGED
@@ -7,6 +7,7 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
7
7
  const tslib = require('tslib');
8
8
  const commands = require('@graphql-inspector/commands');
9
9
  const core = require('@graphql-inspector/core');
10
+ const graphql = require('graphql');
10
11
  const logger = require('@graphql-inspector/logger');
11
12
  const Table = _interopDefault(require('cli-table3'));
12
13
 
@@ -28,6 +29,21 @@ const index = commands.createCommand(api => {
28
29
  type: 'boolean',
29
30
  default: false,
30
31
  },
32
+ complexityScalarCost: {
33
+ describe: 'The cost per scalar for calculating the complexity score.',
34
+ type: 'number',
35
+ default: 1,
36
+ },
37
+ complexityObjectCost: {
38
+ describe: 'The cost per object for calculating the complexity score.',
39
+ type: 'number',
40
+ default: 2,
41
+ },
42
+ complexityDepthCostFactor: {
43
+ describe: 'The cost factor per introduced depth level for calculating the complexity score.',
44
+ type: 'number',
45
+ default: 1.5,
46
+ },
31
47
  });
32
48
  },
33
49
  handler(args) {
@@ -37,20 +53,28 @@ const index = commands.createCommand(api => {
37
53
  const documents = yield loaders.loadDocuments(args.documents, {
38
54
  ignore,
39
55
  });
40
- return handler({ documents, detail: args.detail });
56
+ const complexityConfig = {
57
+ scalarCost: args.complexityScalarCost,
58
+ objectCost: args.complexityObjectCost,
59
+ depthCostFactor: args.complexityDepthCostFactor,
60
+ };
61
+ return handler({ documents, detail: args.detail, complexityConfig });
41
62
  });
42
63
  },
43
64
  };
44
65
  });
45
66
  function handler(args) {
46
67
  const fragments = new Map();
68
+ const fragmentStrings = new Map();
47
69
  const operations = new Map();
48
70
  const getFragmentReference = (fragmentName) => fragments.get(fragmentName);
71
+ const getFragmentSource = (fragmentName) => fragmentStrings.get(fragmentName);
49
72
  for (const record of args.documents) {
50
73
  if (record.document) {
51
74
  for (const definition of record.document.definitions) {
52
75
  if (definition.kind === 'FragmentDefinition') {
53
76
  fragments.set(definition.name.value, definition);
77
+ fragmentStrings.set(definition.name.value, graphql.print(definition));
54
78
  }
55
79
  else if (definition.kind === 'OperationDefinition') {
56
80
  if (definition.name) {
@@ -63,19 +87,28 @@ function handler(args) {
63
87
  let maxDepth = 0;
64
88
  let maxAliases = 0;
65
89
  let maxDirectives = 0;
90
+ let maxTokenCount = 0;
91
+ let maxComplexity = 0;
66
92
  const results = [];
67
93
  for (const [name, operation] of operations.entries()) {
68
94
  const depth = core.countDepth(operation, 0, getFragmentReference);
69
95
  const aliases = core.countAliases(operation, getFragmentReference);
70
96
  const directives = core.countDirectives(operation, getFragmentReference);
71
- results.push([name, depth, aliases, directives]);
97
+ const tokenCount = core.calculateTokenCount({
98
+ source: graphql.print(operation),
99
+ getReferencedFragmentSource: getFragmentSource,
100
+ });
101
+ const complexity = core.calculateOperationComplexity(operation, args.complexityConfig, getFragmentReference);
102
+ results.push([name, depth, aliases, directives, tokenCount, complexity.toFixed(2)]);
72
103
  maxDepth = Math.max(maxDepth, depth);
73
104
  maxAliases = Math.max(maxAliases, aliases);
74
105
  maxDirectives = Math.max(maxDirectives, directives);
106
+ maxTokenCount = Math.max(maxTokenCount, tokenCount);
107
+ maxComplexity = Math.max(maxComplexity, complexity);
75
108
  }
76
109
  if (args.detail) {
77
110
  const table = new Table({
78
- head: ['Operation Name', 'Depth', 'Aliases', 'Directives'],
111
+ head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Token Count', 'Complexity Score'],
79
112
  });
80
113
  table.push(...results);
81
114
  logger.Logger.log(table.toString());
@@ -83,6 +116,8 @@ function handler(args) {
83
116
  logger.Logger.log(`Maximum depth is ${logger.chalk.bold(maxDepth)}`);
84
117
  logger.Logger.log(`Maximum alias amount is ${logger.chalk.bold(maxAliases)}`);
85
118
  logger.Logger.log(`Maximum directive amount is ${logger.chalk.bold(maxDirectives)}`);
119
+ logger.Logger.log(`Maximum token count is ${logger.chalk.bold(maxTokenCount)}`);
120
+ logger.Logger.log(`Maximum complexity score is ${logger.chalk.bold(maxComplexity.toFixed(2))}`);
86
121
  }
87
122
 
88
123
  exports.default = index;
package/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { __awaiter } from 'tslib';
2
2
  import { createCommand } from '@graphql-inspector/commands';
3
- import { countDepth, countAliases, countDirectives } from '@graphql-inspector/core';
3
+ import { countDepth, countAliases, countDirectives, calculateTokenCount, calculateOperationComplexity } from '@graphql-inspector/core';
4
+ import { print } from 'graphql';
4
5
  import { Logger, chalk } from '@graphql-inspector/logger';
5
6
  import Table from 'cli-table3';
6
7
 
@@ -22,6 +23,21 @@ const index = createCommand(api => {
22
23
  type: 'boolean',
23
24
  default: false,
24
25
  },
26
+ complexityScalarCost: {
27
+ describe: 'The cost per scalar for calculating the complexity score.',
28
+ type: 'number',
29
+ default: 1,
30
+ },
31
+ complexityObjectCost: {
32
+ describe: 'The cost per object for calculating the complexity score.',
33
+ type: 'number',
34
+ default: 2,
35
+ },
36
+ complexityDepthCostFactor: {
37
+ describe: 'The cost factor per introduced depth level for calculating the complexity score.',
38
+ type: 'number',
39
+ default: 1.5,
40
+ },
25
41
  });
26
42
  },
27
43
  handler(args) {
@@ -31,20 +47,28 @@ const index = createCommand(api => {
31
47
  const documents = yield loaders.loadDocuments(args.documents, {
32
48
  ignore,
33
49
  });
34
- return handler({ documents, detail: args.detail });
50
+ const complexityConfig = {
51
+ scalarCost: args.complexityScalarCost,
52
+ objectCost: args.complexityObjectCost,
53
+ depthCostFactor: args.complexityDepthCostFactor,
54
+ };
55
+ return handler({ documents, detail: args.detail, complexityConfig });
35
56
  });
36
57
  },
37
58
  };
38
59
  });
39
60
  function handler(args) {
40
61
  const fragments = new Map();
62
+ const fragmentStrings = new Map();
41
63
  const operations = new Map();
42
64
  const getFragmentReference = (fragmentName) => fragments.get(fragmentName);
65
+ const getFragmentSource = (fragmentName) => fragmentStrings.get(fragmentName);
43
66
  for (const record of args.documents) {
44
67
  if (record.document) {
45
68
  for (const definition of record.document.definitions) {
46
69
  if (definition.kind === 'FragmentDefinition') {
47
70
  fragments.set(definition.name.value, definition);
71
+ fragmentStrings.set(definition.name.value, print(definition));
48
72
  }
49
73
  else if (definition.kind === 'OperationDefinition') {
50
74
  if (definition.name) {
@@ -57,19 +81,28 @@ function handler(args) {
57
81
  let maxDepth = 0;
58
82
  let maxAliases = 0;
59
83
  let maxDirectives = 0;
84
+ let maxTokenCount = 0;
85
+ let maxComplexity = 0;
60
86
  const results = [];
61
87
  for (const [name, operation] of operations.entries()) {
62
88
  const depth = countDepth(operation, 0, getFragmentReference);
63
89
  const aliases = countAliases(operation, getFragmentReference);
64
90
  const directives = countDirectives(operation, getFragmentReference);
65
- results.push([name, depth, aliases, directives]);
91
+ const tokenCount = calculateTokenCount({
92
+ source: print(operation),
93
+ getReferencedFragmentSource: getFragmentSource,
94
+ });
95
+ const complexity = calculateOperationComplexity(operation, args.complexityConfig, getFragmentReference);
96
+ results.push([name, depth, aliases, directives, tokenCount, complexity.toFixed(2)]);
66
97
  maxDepth = Math.max(maxDepth, depth);
67
98
  maxAliases = Math.max(maxAliases, aliases);
68
99
  maxDirectives = Math.max(maxDirectives, directives);
100
+ maxTokenCount = Math.max(maxTokenCount, tokenCount);
101
+ maxComplexity = Math.max(maxComplexity, complexity);
69
102
  }
70
103
  if (args.detail) {
71
104
  const table = new Table({
72
- head: ['Operation Name', 'Depth', 'Aliases', 'Directives'],
105
+ head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Token Count', 'Complexity Score'],
73
106
  });
74
107
  table.push(...results);
75
108
  Logger.log(table.toString());
@@ -77,6 +110,8 @@ function handler(args) {
77
110
  Logger.log(`Maximum depth is ${chalk.bold(maxDepth)}`);
78
111
  Logger.log(`Maximum alias amount is ${chalk.bold(maxAliases)}`);
79
112
  Logger.log(`Maximum directive amount is ${chalk.bold(maxDirectives)}`);
113
+ Logger.log(`Maximum token count is ${chalk.bold(maxTokenCount)}`);
114
+ Logger.log(`Maximum complexity score is ${chalk.bold(maxComplexity.toFixed(2))}`);
80
115
  }
81
116
 
82
117
  export default index;
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@graphql-inspector/audit-command",
3
- "version": "0.0.0-canary.d95a8a3",
3
+ "version": "3.4.0",
4
4
  "description": "Audit Documents in GraphQL Inspector",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
7
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.d95a8a3",
11
- "@graphql-inspector/core": "0.0.0-canary.d95a8a3",
12
- "@graphql-inspector/logger": "0.0.0-canary.d95a8a3",
10
+ "@graphql-inspector/commands": "3.4.0",
11
+ "@graphql-inspector/core": "3.4.0",
12
+ "@graphql-inspector/logger": "3.4.0",
13
13
  "cli-table3": "0.6.2",
14
14
  "tslib": "^2.0.0"
15
15
  },