-
Notifications
You must be signed in to change notification settings - Fork 41
support @include/@skip directives #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,22 @@ | |
|
||
import { | ||
getArgumentValues, | ||
getDirectiveValues, | ||
} from 'graphql/execution/values'; | ||
|
||
import { | ||
ValidationContext, | ||
FragmentDefinitionNode, | ||
OperationDefinitionNode, | ||
DirectiveNode, | ||
FieldNode, | ||
FragmentSpreadNode, | ||
InlineFragmentNode, | ||
assertCompositeType, | ||
GraphQLField, isCompositeType, GraphQLCompositeType, GraphQLFieldMap, | ||
GraphQLSchema, DocumentNode, TypeInfo, | ||
visit, visitWithTypeInfo | ||
visit, visitWithTypeInfo, | ||
GraphQLDirective, | ||
} from 'graphql'; | ||
import { | ||
GraphQLUnionType, | ||
|
@@ -102,6 +105,8 @@ export default class QueryComplexity { | |
options: QueryComplexityOptions; | ||
OperationDefinition: Object; | ||
estimators: Array<ComplexityEstimator>; | ||
includeDirectiveDef: GraphQLDirective; | ||
skipDirectiveDef: GraphQLDirective; | ||
|
||
constructor( | ||
context: ValidationContext, | ||
|
@@ -115,6 +120,9 @@ export default class QueryComplexity { | |
this.complexity = 0; | ||
this.options = options; | ||
|
||
this.includeDirectiveDef = this.context.getSchema().getDirective('include'); | ||
this.skipDirectiveDef = this.context.getSchema().getDirective('skip'); | ||
|
||
if (!options.estimators) { | ||
console.warn( | ||
'DEPRECATION WARNING: Estimators should be configured in the queryComplexity options.' | ||
|
@@ -183,6 +191,29 @@ export default class QueryComplexity { | |
(total: number, childNode: FieldNode | FragmentSpreadNode | InlineFragmentNode) => { | ||
let nodeComplexity = 0; | ||
|
||
let includeNode = true; | ||
let skipNode = false; | ||
|
||
childNode.directives.forEach((directive: DirectiveNode) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case a server makes excessive use of directives it would iterate over all the directives, extract values, etc. regardless of whether it was already determined if we include or skip the field. For example: {
field @skip(if: true) @otherDirective @otherDirective2 @etc
} In this case, we could skip the evaluation of the last 3 directives. I'd use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Directives from the standard:
|
||
const directiveName = directive.name.value; | ||
switch (directiveName) { | ||
case 'include': { | ||
const values = getDirectiveValues(this.includeDirectiveDef, childNode, this.options.variables || {}); | ||
includeNode = values.if; | ||
break; | ||
} | ||
case 'skip': { | ||
const values = getDirectiveValues(this.skipDirectiveDef, childNode, this.options.variables || {}); | ||
skipNode = values.if; | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
if (!includeNode || skipNode) { | ||
return total; | ||
} | ||
|
||
switch (childNode.kind) { | ||
case Kind.FIELD: { | ||
const field = fields[childNode.name.value]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only need one variable to determine if the node should be skipped/included, this avoids contradictory (temporary) values...
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two variables were created for a better alignment with the GraphQL standard:
from: http://spec.graphql.org/June2018/#sec--include
We can technically use a single variable, but it would be a bit harder to follow.