-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Breaking out query complexity into its own class #3254
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
134 changes: 134 additions & 0 deletions
134
src/main/java/graphql/analysis/QueryComplexityCalculator.java
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 |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package graphql.analysis; | ||
|
||
import graphql.PublicApi; | ||
import graphql.execution.CoercedVariables; | ||
import graphql.language.Document; | ||
import graphql.schema.GraphQLSchema; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import static graphql.Assert.assertNotNull; | ||
import static java.util.Optional.ofNullable; | ||
|
||
/** | ||
* This can calculate the complexity of an operation using the specified {@link FieldComplexityCalculator} you pass | ||
* into it. | ||
*/ | ||
@PublicApi | ||
public class QueryComplexityCalculator { | ||
|
||
private final FieldComplexityCalculator fieldComplexityCalculator; | ||
private final GraphQLSchema schema; | ||
private final Document document; | ||
private final String operationName; | ||
private final CoercedVariables variables; | ||
|
||
public QueryComplexityCalculator(Builder builder) { | ||
this.fieldComplexityCalculator = assertNotNull(builder.fieldComplexityCalculator, () -> "fieldComplexityCalculator can't be null"); | ||
this.schema = assertNotNull(builder.schema, () -> "schema can't be null"); | ||
this.document = assertNotNull(builder.document, () -> "document can't be null"); | ||
this.variables = assertNotNull(builder.variables, () -> "variables can't be null"); | ||
this.operationName = builder.operationName; | ||
} | ||
|
||
|
||
public int calculate() { | ||
Map<QueryVisitorFieldEnvironment, Integer> valuesByParent = calculateByParents(); | ||
return valuesByParent.getOrDefault(null, 0); | ||
} | ||
|
||
/** | ||
* @return a map that shows the field complexity for each field level in the operation | ||
*/ | ||
public Map<QueryVisitorFieldEnvironment, Integer> calculateByParents() { | ||
QueryTraverser queryTraverser = QueryTraverser.newQueryTraverser() | ||
.schema(this.schema) | ||
.document(this.document) | ||
.operationName(this.operationName) | ||
.coercedVariables(this.variables) | ||
.build(); | ||
|
||
|
||
Map<QueryVisitorFieldEnvironment, Integer> valuesByParent = new LinkedHashMap<>(); | ||
queryTraverser.visitPostOrder(new QueryVisitorStub() { | ||
@Override | ||
public void visitField(QueryVisitorFieldEnvironment env) { | ||
int childComplexity = valuesByParent.getOrDefault(env, 0); | ||
int value = calculateComplexity(env, childComplexity); | ||
|
||
QueryVisitorFieldEnvironment parentEnvironment = env.getParentEnvironment(); | ||
valuesByParent.compute(parentEnvironment, (key, oldValue) -> { | ||
Integer currentValue = ofNullable(oldValue).orElse(0); | ||
return currentValue + value; | ||
} | ||
); | ||
} | ||
}); | ||
|
||
return valuesByParent; | ||
} | ||
|
||
private int calculateComplexity(QueryVisitorFieldEnvironment queryVisitorFieldEnvironment, int childComplexity) { | ||
if (queryVisitorFieldEnvironment.isTypeNameIntrospectionField()) { | ||
return 0; | ||
} | ||
FieldComplexityEnvironment fieldComplexityEnvironment = convertEnv(queryVisitorFieldEnvironment); | ||
return fieldComplexityCalculator.calculate(fieldComplexityEnvironment, childComplexity); | ||
} | ||
|
||
private FieldComplexityEnvironment convertEnv(QueryVisitorFieldEnvironment queryVisitorFieldEnvironment) { | ||
FieldComplexityEnvironment parentEnv = null; | ||
if (queryVisitorFieldEnvironment.getParentEnvironment() != null) { | ||
parentEnv = convertEnv(queryVisitorFieldEnvironment.getParentEnvironment()); | ||
} | ||
return new FieldComplexityEnvironment( | ||
queryVisitorFieldEnvironment.getField(), | ||
queryVisitorFieldEnvironment.getFieldDefinition(), | ||
queryVisitorFieldEnvironment.getFieldsContainer(), | ||
queryVisitorFieldEnvironment.getArguments(), | ||
parentEnv | ||
); | ||
} | ||
|
||
public static Builder newCalculator() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
private FieldComplexityCalculator fieldComplexityCalculator; | ||
private GraphQLSchema schema; | ||
private Document document; | ||
private String operationName; | ||
private CoercedVariables variables = CoercedVariables.emptyVariables(); | ||
|
||
public Builder schema(GraphQLSchema graphQLSchema) { | ||
this.schema = graphQLSchema; | ||
return this; | ||
} | ||
|
||
public Builder fieldComplexityCalculator(FieldComplexityCalculator complexityCalculator) { | ||
this.fieldComplexityCalculator = complexityCalculator; | ||
return this; | ||
} | ||
|
||
public Builder document(Document document) { | ||
this.document = document; | ||
return this; | ||
} | ||
|
||
public Builder operationName(String operationName) { | ||
this.operationName = operationName; | ||
return this; | ||
} | ||
|
||
public Builder variables(CoercedVariables variables) { | ||
this.variables = variables; | ||
return this; | ||
} | ||
|
||
public QueryComplexityCalculator build() { | ||
return new QueryComplexityCalculator(this); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/test/groovy/graphql/analysis/QueryComplexityCalculatorTest.groovy
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package graphql.analysis | ||
|
||
|
||
import graphql.TestUtil | ||
import graphql.execution.CoercedVariables | ||
import graphql.language.Document | ||
import graphql.parser.Parser | ||
import spock.lang.Specification | ||
|
||
class QueryComplexityCalculatorTest extends Specification { | ||
|
||
Document createQuery(String query) { | ||
Parser parser = new Parser() | ||
parser.parseDocument(query) | ||
} | ||
|
||
def "can calculator complexity"() { | ||
given: | ||
def schema = TestUtil.schema(""" | ||
type Query{ | ||
foo: Foo | ||
bar: String | ||
} | ||
type Foo { | ||
scalar: String | ||
foo: Foo | ||
} | ||
""") | ||
def query = createQuery(""" | ||
query q { | ||
f2: foo {scalar foo{scalar}} | ||
f1: foo { foo {foo {foo {foo{foo{scalar}}}}}} } | ||
""") | ||
|
||
|
||
when: | ||
FieldComplexityCalculator fieldComplexityCalculator = new FieldComplexityCalculator() { | ||
@Override | ||
int calculate(FieldComplexityEnvironment environment, int childComplexity) { | ||
return environment.getField().name.startsWith("foo") ? 10 : 1 | ||
} | ||
} | ||
QueryComplexityCalculator calculator = QueryComplexityCalculator.newCalculator() | ||
.fieldComplexityCalculator(fieldComplexityCalculator).schema(schema).document(query).variables(CoercedVariables.emptyVariables()) | ||
.build() | ||
def complexityScore = calculator.calculate() | ||
then: | ||
complexityScore == 20 | ||
|
||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
moved to the new class