forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLUnusualConfiguration.java
More file actions
403 lines (352 loc) · 15 KB
/
Copy pathGraphQLUnusualConfiguration.java
File metadata and controls
403 lines (352 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package graphql;
import graphql.execution.ResponseMapFactory;
import graphql.introspection.GoodFaithIntrospection;
import graphql.parser.ParserOptions;
import graphql.schema.PropertyDataFetcherHelper;
import static graphql.Assert.assertNotNull;
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING;
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING;
/**
* This allows you to control "unusual" aspects of the GraphQL system
* including some JVM wide settings and some per execution settings
* as well as experimental ones.
* <p>
* This is named unusual because in general we don't expect you to
* have to make ths configuration by default, but you can opt into certain features
* or disable them if you want to.
*/
public class GraphQLUnusualConfiguration {
GraphQLUnusualConfiguration() {
}
/**
* @return an element that allows you to control JVM wide parsing configuration
*/
public ParserConfig parsing() {
return new ParserConfig(this);
}
/**
* @return an element that allows you to control JVM wide {@link graphql.schema.PropertyDataFetcher} configuration
*/
public PropertyDataFetcherConfig propertyDataFetching() {
return new PropertyDataFetcherConfig(this);
}
/**
* @return an element that allows you to control JVM wide configuration
* of {@link graphql.introspection.GoodFaithIntrospection}
*/
public GoodFaithIntrospectionConfig goodFaithIntrospection() {
return new GoodFaithIntrospectionConfig(this);
}
private static class BaseConfig {
protected final GraphQLUnusualConfiguration configuration;
private BaseConfig(GraphQLUnusualConfiguration configuration) {
this.configuration = configuration;
}
/**
* @return an element that allows you to chain multiple configuration elements
*/
public GraphQLUnusualConfiguration then() {
return configuration;
}
}
public static class ParserConfig extends BaseConfig {
private ParserConfig(GraphQLUnusualConfiguration configuration) {
super(configuration);
}
/**
* By default, the Parser will not capture ignored characters. A static holds this default
* value in a JVM wide basis options object.
* <p>
* Significant memory savings can be made if we do NOT capture ignored characters,
* especially in SDL parsing.
*
* @return the static default JVM value
*
* @see graphql.language.IgnoredChar
* @see graphql.language.SourceLocation
*/
public ParserOptions getDefaultParserOptions() {
return ParserOptions.getDefaultParserOptions();
}
/**
* By default, the Parser will not capture ignored characters. A static holds this default
* value in a JVM wide basis options object.
* <p>
* Significant memory savings can be made if we do NOT capture ignored characters,
* especially in SDL parsing. So we have set this to false by default.
* <p>
* This static can be set to true to allow the behavior of version 16.x or before.
*
* @param options - the new default JVM parser options
*
* @see graphql.language.IgnoredChar
* @see graphql.language.SourceLocation
*/
public ParserConfig setDefaultParserOptions(ParserOptions options) {
ParserOptions.setDefaultParserOptions(options);
return this;
}
/**
* By default, for operation parsing, the Parser will not capture ignored characters, and it will not capture line comments into AST
* elements . A static holds this default value for operation parsing in a JVM wide basis options object.
*
* @return the static default JVM value for operation parsing
*
* @see graphql.language.IgnoredChar
* @see graphql.language.SourceLocation
*/
public ParserOptions getDefaultOperationParserOptions() {
return ParserOptions.getDefaultOperationParserOptions();
}
/**
* By default, the Parser will not capture ignored characters or line comments. A static holds this default
* value in a JVM wide basis options object for operation parsing.
* <p>
* This static can be set to true to allow the behavior of version 16.x or before.
*
* @param options - the new default JVM parser options for operation parsing
*
* @see graphql.language.IgnoredChar
* @see graphql.language.SourceLocation
*/
public ParserConfig setDefaultOperationParserOptions(ParserOptions options) {
ParserOptions.setDefaultOperationParserOptions(options);
return this;
}
/**
* By default, for SDL parsing, the Parser will not capture ignored characters, but it will capture line comments into AST
* elements. The SDL default options allow unlimited tokens and whitespace, since a DOS attack vector is
* not commonly available via schema SDL parsing.
* <p>
* A static holds this default value for SDL parsing in a JVM wide basis options object.
*
* @return the static default JVM value for SDL parsing
*
* @see graphql.language.IgnoredChar
* @see graphql.language.SourceLocation
* @see graphql.schema.idl.SchemaParser
*/
public ParserOptions getDefaultSdlParserOptions() {
return ParserOptions.getDefaultSdlParserOptions();
}
/**
* By default, for SDL parsing, the Parser will not capture ignored characters, but it will capture line comments into AST
* elements . A static holds this default value for operation parsing in a JVM wide basis options object.
* <p>
* This static can be set to true to allow the behavior of version 16.x or before.
*
* @param options - the new default JVM parser options for SDL parsing
*
* @see graphql.language.IgnoredChar
* @see graphql.language.SourceLocation
*/
public ParserConfig setDefaultSdlParserOptions(ParserOptions options) {
ParserOptions.setDefaultSdlParserOptions(options);
return this;
}
}
public static class PropertyDataFetcherConfig extends BaseConfig {
private PropertyDataFetcherConfig(GraphQLUnusualConfiguration configuration) {
super(configuration);
}
/**
* PropertyDataFetcher caches the methods and fields that map from a class to a property for runtime performance reasons
* as well as negative misses.
* <p>
* However during development you might be using an assistance tool like JRebel to allow you to tweak your code base and this
* caching may interfere with this. So you can call this method to clear the cache. A JRebel plugin could
* be developed to do just that.
*/
@SuppressWarnings("unused")
public PropertyDataFetcherConfig clearReflectionCache() {
PropertyDataFetcherHelper.clearReflectionCache();
return this;
}
/**
* This can be used to control whether PropertyDataFetcher will use {@link java.lang.reflect.Method#setAccessible(boolean)} to gain access to property
* values. By default, it PropertyDataFetcher WILL use setAccessible.
*
* @param flag whether to use setAccessible
*
* @return the previous value of the flag
*/
public boolean setUseSetAccessible(boolean flag) {
return PropertyDataFetcherHelper.setUseSetAccessible(flag);
}
/**
* This can be used to control whether PropertyDataFetcher will cache negative lookups for a property for performance reasons. By default it PropertyDataFetcher WILL cache misses.
*
* @param flag whether to cache misses
*
* @return the previous value of the flag
*/
public boolean setUseNegativeCache(boolean flag) {
return PropertyDataFetcherHelper.setUseNegativeCache(flag);
}
}
public static class GoodFaithIntrospectionConfig extends BaseConfig {
private GoodFaithIntrospectionConfig(GraphQLUnusualConfiguration configuration) {
super(configuration);
}
/**
* @return true if good faith introspection is enabled
*/
public boolean isEnabledJvmWide() {
return GoodFaithIntrospection.isEnabledJvmWide();
}
/**
* This allows you to disable good faith introspection, which is on by default.
*
* @param enabled the desired state
*
* @return the previous state
*/
public GoodFaithIntrospectionConfig enabledJvmWide(boolean enabled) {
GoodFaithIntrospection.enabledJvmWide(enabled);
return this;
}
}
/*
* ===============================================
* Per GraphqlContext code down here
* ===============================================
*/
@SuppressWarnings("DataFlowIssue")
public static class GraphQLContextConfiguration {
// it will be one or the other types of GraphQLContext
private final GraphQLContext graphQLContext;
private final GraphQLContext.Builder graphQLContextBuilder;
GraphQLContextConfiguration(GraphQLContext graphQLContext) {
this.graphQLContext = graphQLContext;
this.graphQLContextBuilder = null;
}
GraphQLContextConfiguration(GraphQLContext.Builder graphQLContextBuilder) {
this.graphQLContextBuilder = graphQLContextBuilder;
this.graphQLContext = null;
}
/**
* @return an element that allows you to control incremental support, that is @defer configuration
*/
public IncrementalSupportConfig incrementalSupport() {
return new IncrementalSupportConfig(this);
}
/**
* @return an element that allows you to precisely control {@link org.dataloader.DataLoader} behavior
* in graphql-java.
*/
public DataloaderConfig dataloaderConfig() {
return new DataloaderConfig(this);
}
/**
* @return an element that allows you to control the {@link ResponseMapFactory} used
*/
public ResponseMapFactoryConfig responseMapFactory() {
return new ResponseMapFactoryConfig(this);
}
private void put(String named, Object value) {
if (graphQLContext != null) {
graphQLContext.put(named, value);
} else {
assertNotNull(graphQLContextBuilder).put(named, value);
}
}
private boolean getBoolean(String named) {
if (graphQLContext != null) {
return graphQLContext.getBoolean(named);
} else {
return assertNotNull(graphQLContextBuilder).getBoolean(named);
}
}
private <T> T get(String named) {
if (graphQLContext != null) {
return graphQLContext.get(named);
} else {
//noinspection unchecked
return (T) assertNotNull(graphQLContextBuilder).get(named);
}
}
}
private static class BaseContextConfig {
protected final GraphQLContextConfiguration contextConfig;
private BaseContextConfig(GraphQLContextConfiguration contextConfig) {
this.contextConfig = contextConfig;
}
/**
* @return an element that allows you to chain multiple configuration elements
*/
public GraphQLContextConfiguration then() {
return contextConfig;
}
}
public static class IncrementalSupportConfig extends BaseContextConfig {
private IncrementalSupportConfig(GraphQLContextConfiguration contextConfig) {
super(contextConfig);
}
/**
* @return true if @defer and @stream behaviour is enabled for this execution.
*/
public boolean isIncrementalSupportEnabled() {
return contextConfig.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT);
}
/**
* This controls whether @defer and @stream behaviour is enabled for this execution.
*/
@ExperimentalApi
public IncrementalSupportConfig enableIncrementalSupport(boolean enable) {
contextConfig.put(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT, enable);
return this;
}
}
public static class DataloaderConfig extends BaseContextConfig {
private DataloaderConfig(GraphQLContextConfiguration contextConfig) {
super(contextConfig);
}
/**
* returns true if chained data loader dispatching is enabled
*/
public boolean isDataLoaderChainingEnabled() {
return contextConfig.getBoolean(ENABLE_DATA_LOADER_CHAINING);
}
public boolean isDataLoaderExhaustedDispatchingEnabled() {
return contextConfig.get(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING);
}
/**
* Enables the ability that chained DataLoaders are dispatched automatically.
*/
@ExperimentalApi
public DataloaderConfig enableDataLoaderChaining(boolean enable) {
contextConfig.put(ENABLE_DATA_LOADER_CHAINING, enable);
return this;
}
/**
* Enables a dispatching strategy that will dispatch as long as there is no
* other data fetcher or batch loader running.
*/
@ExperimentalApi
public DataloaderConfig enableDataLoaderExhaustedDispatching(boolean enable) {
contextConfig.put(ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, enable);
return this;
}
}
public static class ResponseMapFactoryConfig extends BaseContextConfig {
private ResponseMapFactoryConfig(GraphQLContextConfiguration contextConfig) {
super(contextConfig);
}
/**
* @return the {@link ResponseMapFactory} in play - this can be null
*/
@ExperimentalApi
public ResponseMapFactory getOr(ResponseMapFactory defaultFactory) {
ResponseMapFactory responseMapFactory = contextConfig.get(ResponseMapFactory.class.getCanonicalName());
return responseMapFactory != null ? responseMapFactory : defaultFactory;
}
/**
* This controls the {@link ResponseMapFactory} to use for this request
*/
@ExperimentalApi
public ResponseMapFactoryConfig setFactory(ResponseMapFactory factory) {
contextConfig.put(ResponseMapFactory.class.getCanonicalName(), factory);
return this;
}
}
}