-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: plugin renaming & deepFactor for cost analysis
- Loading branch information
thibaudlabat
committed
Aug 1, 2022
1 parent
cc10ce1
commit 30be078
Showing
9 changed files
with
77 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,53 @@ | ||
import {ArmorPlugin} from '../ArmorPlugin'; | ||
import {ValidationRule, PluginConfig} from '../types'; | ||
import {ASTVisitor, GraphQLError, TypeInfo, visit, visitWithTypeInfo} from 'graphql'; | ||
import { ArmorPlugin } from '../ArmorPlugin'; | ||
import { ValidationRule, PluginConfig } from '../types'; | ||
import { ASTVisitor, GraphQLError, TypeInfo, visit, visitWithTypeInfo } from 'graphql'; | ||
import ComplexityVisitor from '../../lib/graphql-validation-complexity'; | ||
|
||
export type CostAnalysisConfig = { | ||
CostAnalysis?: { options: { maxCost: number } } & PluginConfig; | ||
CostAnalysis?: { options: { maxCost: number } } & PluginConfig; | ||
}; | ||
export const DefaultCostAnalysisConfig = { | ||
_namespace: 'CostAnalysis', | ||
enabled: true, | ||
options: { | ||
maxCost: 1000, | ||
scalarCost: 1, | ||
objectCost: 1, | ||
listFactor: 10, | ||
// Special list factor to make schema queries not have huge costs. | ||
introspectionListFactor: 2, | ||
}, | ||
_namespace: 'CostAnalysis', | ||
enabled: true, | ||
options: { | ||
maxCost: 1000, | ||
scalarCost: 1, | ||
objectCost: 1, | ||
listFactor: 3, | ||
depthFactor: 1.5, | ||
// Special list factor to make schema queries not have huge costs. | ||
introspectionListFactor: 2, | ||
}, | ||
}; | ||
|
||
const rule = ({options}: PluginConfig): ValidationRule => | ||
function ComplexityLimit(context) { | ||
const visitor = new ComplexityVisitor(context, options); | ||
// @ts-ignore | ||
const typeInfo = context._typeInfo || new TypeInfo(context.getSchema()); | ||
const rule = ({ options }: PluginConfig): ValidationRule => | ||
function ComplexityLimit(context) { | ||
const visitor = new ComplexityVisitor(context, options); | ||
// @ts-ignore | ||
const typeInfo = context._typeInfo || new TypeInfo(context.getSchema()); | ||
|
||
return { | ||
Document: { | ||
enter(node) { | ||
visit(node, visitWithTypeInfo(typeInfo, visitor as ASTVisitor)); | ||
}, | ||
leave(node) { | ||
const cost = visitor.getCost(); | ||
console.log(`COST: ${cost}`); | ||
if (cost > options.maxCost) { | ||
context.reportError( | ||
new GraphQLError('query exceeds complexity limit', { | ||
nodes: [node], | ||
}), | ||
); | ||
} | ||
}, | ||
}, | ||
}; | ||
return { | ||
Document: { | ||
enter(node) { | ||
visit(node, visitWithTypeInfo(typeInfo, visitor as ASTVisitor)); | ||
}, | ||
leave(node) { | ||
const cost = visitor.getCost(); | ||
console.log(`COST: ${cost}`); | ||
if (cost > options.maxCost) { | ||
context.reportError( | ||
new GraphQLError('query exceeds complexity limit', { | ||
nodes: [node], | ||
}), | ||
); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
export class CostAnalysis extends ArmorPlugin { | ||
getValidationRules(): ValidationRule[] { | ||
return [rule(this.getConfig())]; | ||
} | ||
getValidationRules(): ValidationRule[] { | ||
return [rule(this.getConfig())]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { CharacterLimit } from './CharacterLimit'; | ||
import { Introspection } from './Introspection'; | ||
import { BlockIntrospection } from './BlockIntrospection'; | ||
import { CostAnalysis } from './CostAnalysis'; | ||
import { FieldSuggestion } from './FieldSuggestion'; | ||
import { BlockFieldSuggestion } from './BlockFieldSuggestion'; | ||
|
||
export { CharacterLimit, Introspection, CostAnalysis, FieldSuggestion }; | ||
export { CharacterLimit, BlockIntrospection, CostAnalysis, BlockFieldSuggestion }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters