@graphql-inspector/audit-command
Advanced tools
+5
-0
| import { GlobalArgs } from '@graphql-inspector/commands'; | ||
| import type { CalculateOperationComplexityConfig } from '@graphql-inspector/core'; | ||
| import { Source as DocumentSource } from '@graphql-tools/utils'; | ||
@@ -6,2 +7,5 @@ declare const _default: import("@graphql-inspector/commands").CommandFactory<{}, { | ||
| detail: boolean; | ||
| complexityScalarCost: number; | ||
| complexityObjectCost: number; | ||
| complexityDepthCostFactor: number; | ||
| } & GlobalArgs>; | ||
@@ -12,2 +16,3 @@ export default _default; | ||
| detail: boolean; | ||
| complexityConfig: CalculateOperationComplexityConfig; | ||
| }): void; |
+38
-3
@@ -10,2 +10,3 @@ 'use strict'; | ||
| const core = require('@graphql-inspector/core'); | ||
| const graphql = require('graphql'); | ||
| const logger = require('@graphql-inspector/logger'); | ||
@@ -32,2 +33,17 @@ const Table = _interopDefault(require('cli-table3')); | ||
| }, | ||
| complexityScalarCost: { | ||
| describe: 'The cost per scalar for calculating the complexity score.', | ||
| type: 'number', | ||
| default: 1, | ||
| }, | ||
| complexityObjectCost: { | ||
| describe: 'The cost per object for calculating the complexity score.', | ||
| type: 'number', | ||
| default: 2, | ||
| }, | ||
| complexityDepthCostFactor: { | ||
| describe: 'The cost factor per introduced depth level for calculating the complexity score.', | ||
| type: 'number', | ||
| default: 1.5, | ||
| }, | ||
| }); | ||
@@ -42,3 +58,8 @@ }, | ||
| }); | ||
| return handler({ documents, detail: args.detail }); | ||
| const complexityConfig = { | ||
| scalarCost: args.complexityScalarCost, | ||
| objectCost: args.complexityObjectCost, | ||
| depthCostFactor: args.complexityDepthCostFactor, | ||
| }; | ||
| return handler({ documents, detail: args.detail, complexityConfig }); | ||
| }); | ||
@@ -50,4 +71,6 @@ }, | ||
| const fragments = new Map(); | ||
| const fragmentStrings = new Map(); | ||
| const operations = new Map(); | ||
| const getFragmentReference = (fragmentName) => fragments.get(fragmentName); | ||
| const getFragmentSource = (fragmentName) => fragmentStrings.get(fragmentName); | ||
| for (const record of args.documents) { | ||
@@ -58,2 +81,3 @@ if (record.document) { | ||
| fragments.set(definition.name.value, definition); | ||
| fragmentStrings.set(definition.name.value, graphql.print(definition)); | ||
| } | ||
@@ -71,2 +95,4 @@ else if (definition.kind === 'OperationDefinition') { | ||
| let maxDirectives = 0; | ||
| let maxTokenCount = 0; | ||
| let maxComplexity = 0; | ||
| const results = []; | ||
@@ -77,10 +103,17 @@ for (const [name, operation] of operations.entries()) { | ||
| const directives = core.countDirectives(operation, getFragmentReference); | ||
| results.push([name, depth, aliases, directives]); | ||
| const tokenCount = core.calculateTokenCount({ | ||
| source: graphql.print(operation), | ||
| getReferencedFragmentSource: getFragmentSource, | ||
| }); | ||
| const complexity = core.calculateOperationComplexity(operation, args.complexityConfig, getFragmentReference); | ||
| results.push([name, depth, aliases, directives, tokenCount.toFixed(2), complexity]); | ||
| maxDepth = Math.max(maxDepth, depth); | ||
| maxAliases = Math.max(maxAliases, aliases); | ||
| maxDirectives = Math.max(maxDirectives, directives); | ||
| maxTokenCount = Math.max(maxTokenCount, tokenCount); | ||
| maxComplexity = Math.max(maxComplexity, complexity); | ||
| } | ||
| if (args.detail) { | ||
| const table = new Table({ | ||
| head: ['Operation Name', 'Depth', 'Aliases', 'Directives'], | ||
| head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Token Count', 'Complexity Score'], | ||
| }); | ||
@@ -93,2 +126,4 @@ table.push(...results); | ||
| logger.Logger.log(`Maximum directive amount is ${logger.chalk.bold(maxDirectives)}`); | ||
| logger.Logger.log(`Maximum token count is ${logger.chalk.bold(maxTokenCount)}`); | ||
| logger.Logger.log(`Maximum complexity score is ${logger.chalk.bold(maxComplexity.toFixed(2))}`); | ||
| } | ||
@@ -95,0 +130,0 @@ |
+39
-4
| import { __awaiter } from 'tslib'; | ||
| import { createCommand } from '@graphql-inspector/commands'; | ||
| import { countDepth, countAliases, countDirectives } from '@graphql-inspector/core'; | ||
| import { countDepth, countAliases, countDirectives, calculateTokenCount, calculateOperationComplexity } from '@graphql-inspector/core'; | ||
| import { print } from 'graphql'; | ||
| import { Logger, chalk } from '@graphql-inspector/logger'; | ||
@@ -25,2 +26,17 @@ import Table from 'cli-table3'; | ||
| }, | ||
| complexityScalarCost: { | ||
| describe: 'The cost per scalar for calculating the complexity score.', | ||
| type: 'number', | ||
| default: 1, | ||
| }, | ||
| complexityObjectCost: { | ||
| describe: 'The cost per object for calculating the complexity score.', | ||
| type: 'number', | ||
| default: 2, | ||
| }, | ||
| complexityDepthCostFactor: { | ||
| describe: 'The cost factor per introduced depth level for calculating the complexity score.', | ||
| type: 'number', | ||
| default: 1.5, | ||
| }, | ||
| }); | ||
@@ -35,3 +51,8 @@ }, | ||
| }); | ||
| return handler({ documents, detail: args.detail }); | ||
| const complexityConfig = { | ||
| scalarCost: args.complexityScalarCost, | ||
| objectCost: args.complexityObjectCost, | ||
| depthCostFactor: args.complexityDepthCostFactor, | ||
| }; | ||
| return handler({ documents, detail: args.detail, complexityConfig }); | ||
| }); | ||
@@ -43,4 +64,6 @@ }, | ||
| const fragments = new Map(); | ||
| const fragmentStrings = new Map(); | ||
| const operations = new Map(); | ||
| const getFragmentReference = (fragmentName) => fragments.get(fragmentName); | ||
| const getFragmentSource = (fragmentName) => fragmentStrings.get(fragmentName); | ||
| for (const record of args.documents) { | ||
@@ -51,2 +74,3 @@ if (record.document) { | ||
| fragments.set(definition.name.value, definition); | ||
| fragmentStrings.set(definition.name.value, print(definition)); | ||
| } | ||
@@ -64,2 +88,4 @@ else if (definition.kind === 'OperationDefinition') { | ||
| let maxDirectives = 0; | ||
| let maxTokenCount = 0; | ||
| let maxComplexity = 0; | ||
| const results = []; | ||
@@ -70,10 +96,17 @@ for (const [name, operation] of operations.entries()) { | ||
| const directives = countDirectives(operation, getFragmentReference); | ||
| results.push([name, depth, aliases, directives]); | ||
| const tokenCount = calculateTokenCount({ | ||
| source: print(operation), | ||
| getReferencedFragmentSource: getFragmentSource, | ||
| }); | ||
| const complexity = calculateOperationComplexity(operation, args.complexityConfig, getFragmentReference); | ||
| results.push([name, depth, aliases, directives, tokenCount.toFixed(2), complexity]); | ||
| maxDepth = Math.max(maxDepth, depth); | ||
| maxAliases = Math.max(maxAliases, aliases); | ||
| maxDirectives = Math.max(maxDirectives, directives); | ||
| maxTokenCount = Math.max(maxTokenCount, tokenCount); | ||
| maxComplexity = Math.max(maxComplexity, complexity); | ||
| } | ||
| if (args.detail) { | ||
| const table = new Table({ | ||
| head: ['Operation Name', 'Depth', 'Aliases', 'Directives'], | ||
| head: ['Operation Name', 'Depth', 'Aliases', 'Directives', 'Token Count', 'Complexity Score'], | ||
| }); | ||
@@ -86,2 +119,4 @@ table.push(...results); | ||
| Logger.log(`Maximum directive amount is ${chalk.bold(maxDirectives)}`); | ||
| Logger.log(`Maximum token count is ${chalk.bold(maxTokenCount)}`); | ||
| Logger.log(`Maximum complexity score is ${chalk.bold(maxComplexity.toFixed(2))}`); | ||
| } | ||
@@ -88,0 +123,0 @@ |
+4
-4
| { | ||
| "name": "@graphql-inspector/audit-command", | ||
| "version": "0.0.0-canary.e5ed377", | ||
| "version": "0.0.0-canary.fec2df4", | ||
| "description": "Audit Documents in GraphQL Inspector", | ||
@@ -10,5 +10,5 @@ "sideEffects": false, | ||
| "dependencies": { | ||
| "@graphql-inspector/commands": "0.0.0-canary.e5ed377", | ||
| "@graphql-inspector/core": "0.0.0-canary.e5ed377", | ||
| "@graphql-inspector/logger": "0.0.0-canary.e5ed377", | ||
| "@graphql-inspector/commands": "0.0.0-canary.fec2df4", | ||
| "@graphql-inspector/core": "0.0.0-canary.fec2df4", | ||
| "@graphql-inspector/logger": "0.0.0-canary.fec2df4", | ||
| "cli-table3": "0.6.2", | ||
@@ -15,0 +15,0 @@ "tslib": "^2.0.0" |
12370
49.32%251
42.61%+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed