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

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 (3) hide show
  1. package/index.js +14 -3
  2. package/index.mjs +15 -4
  3. package/package.json +4 -4
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
 
@@ -64,13 +65,16 @@ const index = commands.createCommand(api => {
64
65
  });
65
66
  function handler(args) {
66
67
  const fragments = new Map();
68
+ const fragmentStrings = new Map();
67
69
  const operations = new Map();
68
70
  const getFragmentReference = (fragmentName) => fragments.get(fragmentName);
71
+ const getFragmentSource = (fragmentName) => fragmentStrings.get(fragmentName);
69
72
  for (const record of args.documents) {
70
73
  if (record.document) {
71
74
  for (const definition of record.document.definitions) {
72
75
  if (definition.kind === 'FragmentDefinition') {
73
76
  fragments.set(definition.name.value, definition);
77
+ fragmentStrings.set(definition.name.value, graphql.print(definition));
74
78
  }
75
79
  else if (definition.kind === 'OperationDefinition') {
76
80
  if (definition.name) {
@@ -83,22 +87,28 @@ function handler(args) {
83
87
  let maxDepth = 0;
84
88
  let maxAliases = 0;
85
89
  let maxDirectives = 0;
90
+ let maxTokenCount = 0;
86
91
  let maxComplexity = 0;
87
92
  const results = [];
88
93
  for (const [name, operation] of operations.entries()) {
89
94
  const depth = core.countDepth(operation, 0, getFragmentReference);
90
95
  const aliases = core.countAliases(operation, getFragmentReference);
91
96
  const directives = core.countDirectives(operation, getFragmentReference);
97
+ const tokenCount = core.calculateTokenCount({
98
+ source: graphql.print(operation),
99
+ getReferencedFragmentSource: getFragmentSource,
100
+ });
92
101
  const complexity = core.calculateOperationComplexity(operation, args.complexityConfig, getFragmentReference);
93
- results.push([name, depth, aliases, directives, complexity]);
102
+ results.push([name, depth, aliases, directives, tokenCount, complexity.toFixed(2)]);
94
103
  maxDepth = Math.max(maxDepth, depth);
95
104
  maxAliases = Math.max(maxAliases, aliases);
96
105
  maxDirectives = Math.max(maxDirectives, directives);
106
+ maxTokenCount = Math.max(maxTokenCount, tokenCount);
97
107
  maxComplexity = Math.max(maxComplexity, complexity);
98
108
  }
99
109
  if (args.detail) {
100
110
  const table = new Table({
101
- head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Complexity Score'],
111
+ head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Token Count', 'Complexity Score'],
102
112
  });
103
113
  table.push(...results);
104
114
  logger.Logger.log(table.toString());
@@ -106,7 +116,8 @@ function handler(args) {
106
116
  logger.Logger.log(`Maximum depth is ${logger.chalk.bold(maxDepth)}`);
107
117
  logger.Logger.log(`Maximum alias amount is ${logger.chalk.bold(maxAliases)}`);
108
118
  logger.Logger.log(`Maximum directive amount is ${logger.chalk.bold(maxDirectives)}`);
109
- logger.Logger.log(`Maximum complexity score is ${logger.chalk.bold(maxComplexity)}`);
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))}`);
110
121
  }
111
122
 
112
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, calculateOperationComplexity } 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
 
@@ -58,13 +59,16 @@ const index = createCommand(api => {
58
59
  });
59
60
  function handler(args) {
60
61
  const fragments = new Map();
62
+ const fragmentStrings = new Map();
61
63
  const operations = new Map();
62
64
  const getFragmentReference = (fragmentName) => fragments.get(fragmentName);
65
+ const getFragmentSource = (fragmentName) => fragmentStrings.get(fragmentName);
63
66
  for (const record of args.documents) {
64
67
  if (record.document) {
65
68
  for (const definition of record.document.definitions) {
66
69
  if (definition.kind === 'FragmentDefinition') {
67
70
  fragments.set(definition.name.value, definition);
71
+ fragmentStrings.set(definition.name.value, print(definition));
68
72
  }
69
73
  else if (definition.kind === 'OperationDefinition') {
70
74
  if (definition.name) {
@@ -77,22 +81,28 @@ function handler(args) {
77
81
  let maxDepth = 0;
78
82
  let maxAliases = 0;
79
83
  let maxDirectives = 0;
84
+ let maxTokenCount = 0;
80
85
  let maxComplexity = 0;
81
86
  const results = [];
82
87
  for (const [name, operation] of operations.entries()) {
83
88
  const depth = countDepth(operation, 0, getFragmentReference);
84
89
  const aliases = countAliases(operation, getFragmentReference);
85
90
  const directives = countDirectives(operation, getFragmentReference);
91
+ const tokenCount = calculateTokenCount({
92
+ source: print(operation),
93
+ getReferencedFragmentSource: getFragmentSource,
94
+ });
86
95
  const complexity = calculateOperationComplexity(operation, args.complexityConfig, getFragmentReference);
87
- results.push([name, depth, aliases, directives, complexity]);
96
+ results.push([name, depth, aliases, directives, tokenCount, complexity.toFixed(2)]);
88
97
  maxDepth = Math.max(maxDepth, depth);
89
98
  maxAliases = Math.max(maxAliases, aliases);
90
99
  maxDirectives = Math.max(maxDirectives, directives);
100
+ maxTokenCount = Math.max(maxTokenCount, tokenCount);
91
101
  maxComplexity = Math.max(maxComplexity, complexity);
92
102
  }
93
103
  if (args.detail) {
94
104
  const table = new Table({
95
- head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Complexity Score'],
105
+ head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Token Count', 'Complexity Score'],
96
106
  });
97
107
  table.push(...results);
98
108
  Logger.log(table.toString());
@@ -100,7 +110,8 @@ function handler(args) {
100
110
  Logger.log(`Maximum depth is ${chalk.bold(maxDepth)}`);
101
111
  Logger.log(`Maximum alias amount is ${chalk.bold(maxAliases)}`);
102
112
  Logger.log(`Maximum directive amount is ${chalk.bold(maxDirectives)}`);
103
- Logger.log(`Maximum complexity score is ${chalk.bold(maxComplexity)}`);
113
+ Logger.log(`Maximum token count is ${chalk.bold(maxTokenCount)}`);
114
+ Logger.log(`Maximum complexity score is ${chalk.bold(maxComplexity.toFixed(2))}`);
104
115
  }
105
116
 
106
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.36b26d2",
3
+ "version": "0.0.0-canary.86df6bb",
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.36b26d2",
11
- "@graphql-inspector/core": "0.0.0-canary.36b26d2",
12
- "@graphql-inspector/logger": "0.0.0-canary.36b26d2",
10
+ "@graphql-inspector/commands": "0.0.0-canary.86df6bb",
11
+ "@graphql-inspector/core": "0.0.0-canary.86df6bb",
12
+ "@graphql-inspector/logger": "0.0.0-canary.86df6bb",
13
13
  "cli-table3": "0.6.2",
14
14
  "tslib": "^2.0.0"
15
15
  },