Skip to content

Commit b5f2ab1

Browse files
committed
A generalised configuration mechanism
1 parent ea9104d commit b5f2ab1

File tree

4 files changed

+203
-1
lines changed

4 files changed

+203
-1
lines changed

src/main/java/graphql/GraphQL.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@
8484
@PublicApi
8585
public class GraphQL {
8686

87+
/**
88+
* This allows you to control specific aspects of the GraphQL system
89+
* including some JVM wide settings and some per execution setttings
90+
*
91+
* @return a GraphQL object
92+
*/
93+
public static GraphQLConfiguration configuration() {
94+
return GraphQLConfiguration.INSTANCE;
95+
}
96+
8797
private final GraphQLSchema graphQLSchema;
8898
private final ExecutionStrategy queryStrategy;
8999
private final ExecutionStrategy mutationStrategy;
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package graphql;
2+
3+
import graphql.parser.ParserOptions;
4+
5+
/**
6+
* This allows you to control specific aspects of the GraphQL system
7+
* including some JVM wide settings and some per execution settings
8+
* as well as experimental ones
9+
*/
10+
public class GraphQLConfiguration {
11+
static final GraphQLConfiguration INSTANCE = new GraphQLConfiguration();
12+
static final ParserCfg PARSER_CFG = new ParserCfg();
13+
static final IncrementalSupportCfg INCREMENTAL_SUPPORT_CFG = new IncrementalSupportCfg();
14+
15+
private GraphQLConfiguration() {
16+
}
17+
18+
public ParserCfg parser() {
19+
return PARSER_CFG;
20+
}
21+
22+
@ExperimentalApi
23+
public IncrementalSupportCfg incrementalSupport() {
24+
return INCREMENTAL_SUPPORT_CFG;
25+
}
26+
27+
public static class ParserCfg {
28+
29+
private ParserCfg() {
30+
}
31+
32+
/**
33+
* By default, the Parser will not capture ignored characters. A static holds this default
34+
* value in a JVM wide basis options object.
35+
* <p>
36+
* Significant memory savings can be made if we do NOT capture ignored characters,
37+
* especially in SDL parsing.
38+
*
39+
* @return the static default JVM value
40+
*
41+
* @see graphql.language.IgnoredChar
42+
* @see graphql.language.SourceLocation
43+
*/
44+
public ParserOptions getDefaultParserOptions() {
45+
return ParserOptions.getDefaultParserOptions();
46+
}
47+
48+
/**
49+
* By default, the Parser will not capture ignored characters. A static holds this default
50+
* value in a JVM wide basis options object.
51+
* <p>
52+
* Significant memory savings can be made if we do NOT capture ignored characters,
53+
* especially in SDL parsing. So we have set this to false by default.
54+
* <p>
55+
* This static can be set to true to allow the behavior of version 16.x or before.
56+
*
57+
* @param options - the new default JVM parser options
58+
*
59+
* @see graphql.language.IgnoredChar
60+
* @see graphql.language.SourceLocation
61+
*/
62+
public ParserCfg setDefaultParserOptions(ParserOptions options) {
63+
ParserOptions.setDefaultParserOptions(options);
64+
return this;
65+
}
66+
67+
68+
/**
69+
* By default, for operation parsing, the Parser will not capture ignored characters, and it will not capture line comments into AST
70+
* elements . A static holds this default value for operation parsing in a JVM wide basis options object.
71+
*
72+
* @return the static default JVM value for operation parsing
73+
*
74+
* @see graphql.language.IgnoredChar
75+
* @see graphql.language.SourceLocation
76+
*/
77+
public ParserOptions getDefaultOperationParserOptions() {
78+
return ParserOptions.getDefaultOperationParserOptions();
79+
}
80+
81+
/**
82+
* By default, the Parser will not capture ignored characters or line comments. A static holds this default
83+
* value in a JVM wide basis options object for operation parsing.
84+
* <p>
85+
* This static can be set to true to allow the behavior of version 16.x or before.
86+
*
87+
* @param options - the new default JVM parser options for operation parsing
88+
*
89+
* @see graphql.language.IgnoredChar
90+
* @see graphql.language.SourceLocation
91+
*/
92+
public ParserCfg setDefaultOperationParserOptions(ParserOptions options) {
93+
ParserOptions.setDefaultOperationParserOptions(options);
94+
return this;
95+
}
96+
97+
/**
98+
* By default, for SDL parsing, the Parser will not capture ignored characters, but it will capture line comments into AST
99+
* elements. The SDL default options allow unlimited tokens and whitespace, since a DOS attack vector is
100+
* not commonly available via schema SDL parsing.
101+
* <p>
102+
* A static holds this default value for SDL parsing in a JVM wide basis options object.
103+
*
104+
* @return the static default JVM value for SDL parsing
105+
*
106+
* @see graphql.language.IgnoredChar
107+
* @see graphql.language.SourceLocation
108+
* @see graphql.schema.idl.SchemaParser
109+
*/
110+
public ParserOptions getDefaultSdlParserOptions() {
111+
return ParserOptions.getDefaultSdlParserOptions();
112+
}
113+
114+
/**
115+
* By default, for SDL parsing, the Parser will not capture ignored characters, but it will capture line comments into AST
116+
* elements . A static holds this default value for operation parsing in a JVM wide basis options object.
117+
* <p>
118+
* This static can be set to true to allow the behavior of version 16.x or before.
119+
*
120+
* @param options - the new default JVM parser options for SDL parsing
121+
*
122+
* @see graphql.language.IgnoredChar
123+
* @see graphql.language.SourceLocation
124+
*/
125+
public ParserCfg setDefaultSdlParserOptions(ParserOptions options) {
126+
ParserOptions.setDefaultSdlParserOptions(options);
127+
return this;
128+
}
129+
}
130+
131+
132+
public static class IncrementalSupportCfg {
133+
private IncrementalSupportCfg() {
134+
}
135+
136+
@ExperimentalApi
137+
public boolean isIncrementalSupportEnabled(GraphQLContext graphQLContext) {
138+
return graphQLContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT);
139+
}
140+
141+
/**
142+
* This controls whether @defer and @stream behaviour is enabled for this execution.
143+
*/
144+
@ExperimentalApi
145+
public IncrementalSupportCfg enableIncrementalSupport(GraphQLContext.Builder graphqlContext, boolean enable) {
146+
graphqlContext.put(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT, enable);
147+
return this;
148+
}
149+
}
150+
}

src/main/java/graphql/parser/ParserOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public static ParserOptions getDefaultSdlParserOptions() {
175175
*
176176
* This static can be set to true to allow the behavior of version 16.x or before.
177177
*
178-
* @param options - the new default JVM parser options for operation parsing
178+
* @param options - the new default JVM parser options for SDL parsing
179179
*
180180
* @see graphql.language.IgnoredChar
181181
* @see graphql.language.SourceLocation
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package graphql.config
2+
3+
import graphql.GraphQL
4+
import graphql.GraphQLContext
5+
import spock.lang.Specification
6+
7+
import static graphql.parser.ParserOptions.newParserOptions
8+
9+
class GraphQLConfigurationTest extends Specification {
10+
11+
def "can set parser configurations"() {
12+
when:
13+
def parserOptions = newParserOptions().maxRuleDepth(99).build()
14+
GraphQL.configuration().parser().setDefaultParserOptions(parserOptions)
15+
def defaultParserOptions = GraphQL.configuration().parser().getDefaultParserOptions()
16+
17+
then:
18+
defaultParserOptions.getMaxRuleDepth() == 99
19+
}
20+
21+
def "can set defer configuration"() {
22+
when:
23+
def builder = GraphQLContext.newContext()
24+
GraphQL.configuration().incrementalSupport().enableIncrementalSupport(builder, true)
25+
26+
then:
27+
GraphQL.configuration().incrementalSupport().isIncrementalSupportEnabled(builder.build())
28+
29+
when:
30+
builder = GraphQLContext.newContext()
31+
GraphQL.configuration().incrementalSupport().enableIncrementalSupport(builder, false)
32+
33+
then:
34+
!GraphQL.configuration().incrementalSupport().isIncrementalSupportEnabled(builder.build())
35+
36+
when:
37+
builder = GraphQLContext.newContext()
38+
39+
then:
40+
!GraphQL.configuration().incrementalSupport().isIncrementalSupportEnabled(builder.build())
41+
}
42+
}

0 commit comments

Comments
 (0)