8000 add a default max nodes count for the ExecutableNormalizedFactory by andimarek · Pull Request #3547 · graphql-java/graphql-java · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Apply and reload
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import static graphql.util.FpKit.filterSet;
import static graphql.util.FpKit.groupingBy;
import static graphql.util.FpKit.intersection;
import static java.util.Collections.max;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toCollection;
Expand All @@ -78,13 +77,27 @@
public class ExecutableNormalizedOperationFactory {

public static class Options {


private final GraphQLContext graphQLContext;
private final Locale locale;
private final int maxChildrenDepth;
private final int maxFieldsCount;

private final boolean deferSupport;

/**
* The default max fields count is 100,000.
* This is big enough for even very large queries, but
* can be changed via {#setDefaultOptions
*/
public static final int DEFAULT_MAX_FIELDS_COUNT = 100_000;
private static Options defaultOptions = new Options(GraphQLContext.getDefault(),
Locale.getDefault(),
Integer.MAX_VALUE,
DEFAULT_MAX_FIELDS_COUNT,
false);

private Options(GraphQLContext graphQLContext,
Locale locale,
int maxChildrenDepth,
Expand All @@ -97,13 +110,23 @@ private Options(GraphQLContext graphQLContext,
this.maxFieldsCount = maxFieldsCount;
}

/**
* Sets new default Options used when creating instances of {@link ExecutableNormalizedOperation}.
*
* @param options new default options
*/
public static void setDefaultOptions(Options options) {
defaultOptions = Assert.assertNotNull(options);
}


/**
* Returns the default options used when creating instances of {@link ExecutableNormalizedOperation}.
*
* @return the default options
*/
public static Options defaultOptions() {
return new Options(
GraphQLContext.getDefault(),
Locale.getDefault(),
Integer.MAX_VALUE,
Integer.MAX_VALUE,
false);
return defaultOptions;
}

/**
Expand Down Expand Up @@ -470,7 +493,7 @@ private ExecutableNormalizedOperation createNormalizedQueryImpl() {
topLevel,
fieldAndAstParents,
1);
maxDepthSeen = Math.max(maxDepthSeen,depthSeen);
maxDepthSeen = Math.max(maxDepthSeen, depthSeen);
}
// getPossibleMergerList
for (PossibleMerger possibleMerger : possibleMergerList) {
Expand Down Expand Up @@ -498,8 +521,8 @@ private void captureMergedField(ExecutableNormalizedField enf, MergedField merge
}

private int buildFieldWithChildren(ExecutableNormalizedField executableNormalizedField,
ImmutableList<FieldAndAstParent> fieldAndAstParents,
int curLevel) {
ImmutableList<FieldAndAstParent> fieldAndAstParents,
int curLevel) {
checkMaxDepthExceeded(curLevel);

CollectNFResult nextLevel = collectFromMergedField(executableNormalizedField, fieldAndAstParents, curLevel + 1);
Expand All @@ -518,7 +541,7 @@ private int buildFieldWithChildren(ExecutableNormalizedField executableNormalize
int depthSeen = buildFieldWithChildren(childENF,
childFieldAndAstParents,
curLevel + 1);
maxDepthSeen = Math.max(maxDepthSeen,depthSeen);
maxDepthSeen = Math.max(maxDepthSeen, depthSeen);

checkMaxDepthExceeded(maxDepthSeen);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3099,13 +3099,94 @@ fragment personName on Person {
document,
null,
RawVariables.emptyVariables()
)
)

then:
result.getOperationDepth() == 7
result.getOperationFieldCount() == 8
}

def "factory has a default max node count"() {
String schema = """
type Query {
foo: Foo
}
type Foo {
foo: Foo
name: String
}
"""

GraphQLSchema graphQLSchema = TestUtil.schema(schema)

String query = "{ foo { ...F1}} "
int fragmentCount = 12
for (int i = 1; i < fragmentCount; i++) {
query += """
fragment F$i on Foo {
foo { ...F${i + 1} }
a: foo{ ...F${i + 1} }
b: foo{ ...F${i + 1} }
}
"""
}
query += """
fragment F$fragmentCount on Foo{
name
}
"""

assertValidQuery(graphQLSchema, query)

Document document = TestUtil.parseQuery(query)

when:
def result = ExecutableNormalizedOperationFactory.createExecutableNormalizedOperationWithRawVariables(
graphQLSchema,
document,
null,
RawVariables.emptyVariables()
)
then:
def e = thrown(AbortExecutionException)
e.message == "Maximum field count exceeded. 100001 > 100000"
}

def "default max fields can be changed "() {
String schema = """
type Query {
foo: Foo
}
type Foo {
foo: Foo
name: String
}
"""

GraphQLSchema graphQLSchema = TestUtil.schema(schema)

String query = "{foo{foo{name}}} "

assertValidQuery(graphQLSchema, query)

Document document = TestUtil.parseQuery(query)
ExecutableNormalizedOperationFactory.Options.setDefaultOptions(ExecutableNormalizedOperationFactory.Options.defaultOptions().maxFieldsCount(2))

when:
def result = ExecutableNormalizedOperationFactory.createExecutableNormalizedOperationWithRawVariables(
graphQLSchema,
document,
null,
RawVariables.emptyVariables()
)
then:
def e = thrown(AbortExecutionException)
e.message == "Maximum field count exceeded. 3 > 2"
cleanup:
ExecutableNormalizedOperationFactory.Options.setDefaultOptions(ExecutableNormalizedOperationFactory.Options.defaultOptions().maxFieldsCount(ExecutableNormalizedOperationFactory.Options.DEFAULT_MAX_FIELDS_COUNT))
}


private static ExecutableNormalizedOperation localCreateExecutableNormalizedOperation(
GraphQLSchema graphQLSchema,
Document document,
Expand Down
0