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

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 +11 -2
  2. package/index.mjs +12 -3
  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) {
@@ -89,8 +93,12 @@ function handler(args) {
89
93
  const depth = core.countDepth(operation, 0, getFragmentReference);
90
94
  const aliases = core.countAliases(operation, getFragmentReference);
91
95
  const directives = core.countDirectives(operation, getFragmentReference);
96
+ const tokenCount = core.calculateTokenCount({
97
+ source: graphql.print(operation),
98
+ getReferencedFragmentSource: getFragmentSource,
99
+ });
92
100
  const complexity = core.calculateOperationComplexity(operation, args.complexityConfig, getFragmentReference);
93
- results.push([name, depth, aliases, directives, complexity]);
101
+ results.push([name, depth, aliases, directives, tokenCount, complexity]);
94
102
  maxDepth = Math.max(maxDepth, depth);
95
103
  maxAliases = Math.max(maxAliases, aliases);
96
104
  maxDirectives = Math.max(maxDirectives, directives);
@@ -98,7 +106,7 @@ function handler(args) {
98
106
  }
99
107
  if (args.detail) {
100
108
  const table = new Table({
101
- head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Complexity Score'],
109
+ head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Token Count', 'Complexity Score'],
102
110
  });
103
111
  table.push(...results);
104
112
  logger.Logger.log(table.toString());
@@ -106,6 +114,7 @@ function handler(args) {
106
114
  logger.Logger.log(`Maximum depth is ${logger.chalk.bold(maxDepth)}`);
107
115
  logger.Logger.log(`Maximum alias amount is ${logger.chalk.bold(maxAliases)}`);
108
116
  logger.Logger.log(`Maximum directive amount is ${logger.chalk.bold(maxDirectives)}`);
117
+ logger.Logger.log(`Maximum token count is ${logger.chalk.bold(maxComplexity)}`);
109
118
  logger.Logger.log(`Maximum complexity score is ${logger.chalk.bold(maxComplexity)}`);
110
119
  }
111
120
 
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) {
@@ -83,8 +87,12 @@ function handler(args) {
83
87
  const depth = countDepth(operation, 0, getFragmentReference);
84
88
  const aliases = countAliases(operation, getFragmentReference);
85
89
  const directives = countDirectives(operation, getFragmentReference);
90
+ const tokenCount = calculateTokenCount({
91
+ source: print(operation),
92
+ getReferencedFragmentSource: getFragmentSource,
93
+ });
86
94
  const complexity = calculateOperationComplexity(operation, args.complexityConfig, getFragmentReference);
87
- results.push([name, depth, aliases, directives, complexity]);
95
+ results.push([name, depth, aliases, directives, tokenCount, complexity]);
88
96
  maxDepth = Math.max(maxDepth, depth);
89
97
  maxAliases = Math.max(maxAliases, aliases);
90
98
  maxDirectives = Math.max(maxDirectives, directives);
@@ -92,7 +100,7 @@ function handler(args) {
92
100
  }
93
101
  if (args.detail) {
94
102
  const table = new Table({
95
- head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Complexity Score'],
103
+ head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Token Count', 'Complexity Score'],
96
104
  });
97
105
  table.push(...results);
98
106
  Logger.log(table.toString());
@@ -100,6 +108,7 @@ function handler(args) {
100
108
  Logger.log(`Maximum depth is ${chalk.bold(maxDepth)}`);
101
109
  Logger.log(`Maximum alias amount is ${chalk.bold(maxAliases)}`);
102
110
  Logger.log(`Maximum directive amount is ${chalk.bold(maxDirectives)}`);
111
+ Logger.log(`Maximum token count is ${chalk.bold(maxComplexity)}`);
103
112
  Logger.log(`Maximum complexity score is ${chalk.bold(maxComplexity)}`);
104
113
  }
105
114
 
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.9daef8b",
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.9daef8b",
11
+ "@graphql-inspector/core": "0.0.0-canary.9daef8b",
12
+ "@graphql-inspector/logger": "0.0.0-canary.9daef8b",
13
13
  "cli-table3": "0.6.2",
14
14
  "tslib": "^2.0.0"
15
15
  },