Skip to content

Commit 190bf64

Browse files
committed
Added a getBoolean onto GraphQLContext.java
1 parent 321c7d8 commit 190bf64

6 files changed

Lines changed: 54 additions & 11 deletions

File tree

src/main/java/graphql/GraphQLContext.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,33 @@ public <T> Optional<T> getOrEmpty(Object key) {
9696
return Optional.ofNullable(t);
9797
}
9898

99+
/**
100+
* This returns true if the value at the specified key is equal to
101+
* {@link Boolean#TRUE}
102+
*
103+
* @param key the key to look up
104+
*
105+
* @return true if the value is equal to {@link Boolean#TRUE}
106+
*/
107+
public Boolean getBoolean(Object key) {
108+
Object val = map.get(assertNotNull(key));
109+
return Boolean.TRUE.equals(val);
110+
}
111+
112+
/**
113+
* This returns true if the value at the specified key is equal to
114+
* {@link Boolean#TRUE} or the default value if the key is missing
115+
*
116+
* @param key the key to look up
117+
* @param defaultValue the value to use if the key is not present
118+
*
119+
* @return true if the value is equal to {@link Boolean#TRUE}
120+
*/
121+
public Boolean getBoolean(Object key, Boolean defaultValue) {
122+
Object val = map.getOrDefault(assertNotNull(key), defaultValue);
123+
return Boolean.TRUE.equals(val);
124+
}
125+
99126
/**
100127
* Returns true if the context contains a value for that key
101128
*
@@ -177,11 +204,11 @@ public GraphQLContext putAll(Consumer<GraphQLContext.Builder> contextBuilderCons
177204
* Attempts to compute a mapping for the specified key and its
178205
* current mapped value (or null if there is no current mapping).
179206
*
180-
* @param key key with which the specified value is to be associated
207+
* @param key key with which the specified value is to be associated
181208
* @param remappingFunction the function to compute a value
209+
* @param <T> for two
182210
*
183211
* @return the new value associated with the specified key, or null if none
184-
* @param <T> for two
185212
*/
186213
public <T> T compute(Object key, BiFunction<Object, ? super T, ? extends T> remappingFunction) {
187214
assertNotNull(remappingFunction);
@@ -192,11 +219,11 @@ public <T> T compute(Object key, BiFunction<Object, ? super T, ? extends T> rema
192219
* If the specified key is not already associated with a value (or is mapped to null),
193220
* attempts to compute its value using the given mapping function and enters it into this map unless null.
194221
*
195-
* @param key key with which the specified value is to be associated
222+
* @param key key with which the specified value is to be associated
196223
* @param mappingFunction the function to compute a value
224+
* @param <T> for two
197225
*
198226
* @return the current (existing or computed) value associated with the specified key, or null if the computed value is null
199-
* @param <T> for two
200227
*/
201228

202229
public <T> T computeIfAbsent(Object key, Function<Object, ? extends T> mappingFunction) {
@@ -207,11 +234,11 @@ public <T> T computeIfAbsent(Object key, Function<Object, ? extends T> mappingFu
207234
* If the value for the specified key is present and non-null,
208235
* attempts to compute a new mapping given the key and its current mapped value.
209236
*
210-
* @param key key with which the specified value is to be associated
237+
* @param key key with which the specified value is to be associated
211238
* @param remappingFunction the function to compute a value
239+
* @param <T> for two
212240
*
213241
* @return the new value associated with the specified key, or null if none
214-
* @param <T> for two
215242
*/
216243

217244
public <T> T computeIfPresent(Object key, BiFunction<Object, ? super T, ? extends T> remappingFunction) {

src/main/java/graphql/execution/Execution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe
154154
collectorParameters,
155155
operationDefinition.getSelectionSet(),
156156
Optional.ofNullable(executionContext.getGraphQLContext())
157-
.map(graphqlContext -> (Boolean) graphqlContext.get(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
157+
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
158158
.orElse(false)
159159
);
160160

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ DeferredExecutionSupport createDeferredExecutionSupport(ExecutionContext executi
298298
MergedSelectionSet fields = parameters.getFields();
299299

300300
return Optional.ofNullable(executionContext.getGraphQLContext())
301-
.map(graphqlContext -> (Boolean) graphqlContext.get(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
301+
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
302302
.orElse(false) ?
303303
new DeferredExecutionSupport.DeferredExecutionSupportImpl(
304304
fields,
@@ -948,7 +948,7 @@ protected <T> void handleValueException(CompletableFuture<T> overallResult, Thro
948948
collectorParameters,
949949
parameters.getField(),
950950
Optional.ofNullable(executionContext.getGraphQLContext())
951-
.map(graphqlContext -> (Boolean) graphqlContext.get(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
951+
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
952952
.orElse(false)
953953
);
954954

src/main/java/graphql/introspection/GoodFaithIntrospection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private static boolean isIntrospectionEnabled(GraphQLContext graphQlContext) {
134134
if (!isEnabledJvmWide()) {
135135
return false;
136136
}
137-
return !graphQlContext.getOrDefault(GOOD_FAITH_INTROSPECTION_DISABLED, false);
137+
return !graphQlContext.getBoolean(GOOD_FAITH_INTROSPECTION_DISABLED, false);
138138
}
139139

140140
public static class BadFaithIntrospectionError implements GraphQLError {

src/main/java/graphql/introspection/Introspection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private static boolean isIntrospectionEnabled(GraphQLContext graphQlContext) {
144144
if (!isEnabledJvmWide()) {
145145
return false;
146146
}
147-
return !graphQlContext.getOrDefault(INTROSPECTION_DISABLED, false);
147+
return !graphQlContext.getBoolean(INTROSPECTION_DISABLED, false);
148148
}
149149

150150
private static final Map<FieldCoordinates, IntrospectionDataFetcher<?>> introspectionDataFetchers = new LinkedHashMap<>();

src/test/groovy/graphql/GraphQLContextTest.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,20 @@ class GraphQLContextTest extends Specification {
276276
executionResult.errors.isEmpty()
277277
executionResult.data == [field: "ctx1value"]
278278
}
279+
280+
def "boolean getters work"() {
281+
when:
282+
def context = GraphQLContext.newContext().of(
283+
"f", false,
284+
"t", true,
285+
"notABool", "true"
286+
).build()
287+
288+
then:
289+
!context.getBoolean("f")
290+
context.getBoolean("t")
291+
!context.getBoolean("notABool")
292+
!context.getBoolean("missing")
293+
context.getBoolean("missing", true)
294+
}
279295
}

0 commit comments

Comments
 (0)