Skip to content

Commit ec4b7ae

Browse files
committed
add possibility to override default options
1 parent a999912 commit ec4b7ae

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

src/main/java/graphql/normalized/ExecutableNormalizedOperationFactory.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,27 @@
7777
public class ExecutableNormalizedOperationFactory {
7878

7979
public static class Options {
80+
81+
8082
private final GraphQLContext graphQLContext;
8183
private final Locale locale;
8284
private final int maxChildrenDepth;
8385
private final int maxFieldsCount;
8486

8587
private final boolean deferSupport;
8688

89+
/**
90+
* The default max fields count is 100,000.
91+
* This is big enough for even very large queries, but
92+
* can be changed via {#setDefaultOptions
93+
*/
94+
public static final int DEFAULT_MAX_FIELDS_COUNT = 100_000;
95+
private static Options defaultOptions = new Options(GraphQLContext.getDefault(),
96+
Locale.getDefault(),
97+
Integer.MAX_VALUE,
98+
DEFAULT_MAX_FIELDS_COUNT,
99+
false);
100+
87101
private Options(GraphQLContext graphQLContext,
88102
Locale locale,
89103
int maxChildrenDepth,
@@ -96,13 +110,23 @@ private Options(GraphQLContext graphQLContext,
96110
this.maxFieldsCount = maxFieldsCount;
97111
}
98112

113+
/**
114+
* Sets new default Options used when creating instances of {@link ExecutableNormalizedOperation}.
115+
*
116+
* @param options new default options
117+
*/
118+
public static void setDefaultOptions(Options options) {
119+
defaultOptions = Assert.assertNotNull(options);
120+
}
121+
122+
123+
/**
124+
* Returns the default options used when creating instances of {@link ExecutableNormalizedOperation}.
125+
*
126+
* @return the default options
127+
*/
99128
public static Options defaultOptions() {
100-
return new Options(
101-
GraphQLContext.getDefault(),
102-
Locale.getDefault(),
103-
Integer.MAX_VALUE,
104-
100_000,
105-
false);
129+
return defaultOptions;
106130
}
107131

108132
/**

src/test/groovy/graphql/normalized/ExecutableNormalizedOperationFactoryTest.groovy

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,6 +3152,41 @@ fragment personName on Person {
31523152
e.message == "Maximum field count exceeded. 100001 > 100000"
31533153
}
31543154
3155+
def "default max fields can be changed "() {
3156+
String schema = """
3157+
type Query {
3158+
foo: Foo
3159+
}
3160+
type Foo {
3161+
foo: Foo
3162+
name: String
3163+
}
3164+
"""
3165+
3166+
GraphQLSchema graphQLSchema = TestUtil.schema(schema)
3167+
3168+
String query = "{ foo { foo{ name}}} "
3169+
3170+
assertValidQuery(graphQLSchema, query)
3171+
3172+
Document document = TestUtil.parseQuery(query)
3173+
ExecutableNormalizedOperationFactory.Options.setDefaultOptions(ExecutableNormalizedOperationFactory.Options.defaultOptions().maxFieldsCount(2))
3174+
3175+
when:
3176+
def result = ExecutableNormalizedOperationFactory.createExecutableNormalizedOperationWithRawVariables(
3177+
graphQLSchema,
3178+
document,
3179+
null,
3180+
RawVariables.emptyVariables()
3181+
)
3182+
then:
3183+
def e = thrown(AbortExecutionException)
3184+
e.message == "Maximum field count exceeded. 3 > 2"
3185+
cleanup:
3186+
ExecutableNormalizedOperationFactory.Options.setDefaultOptions(ExecutableNormalizedOperationFactory.Options.defaultOptions().maxFieldsCount(ExecutableNormalizedOperationFactory.Options.DEFAULT_MAX_FIELDS_COUNT))
3187+
}
3188+
3189+
31553190
private static ExecutableNormalizedOperation localCreateExecutableNormalizedOperation(
31563191
GraphQLSchema graphQLSchema,
31573192
Document document,

0 commit comments

Comments
 (0)