Skip to content

Commit 49a68f9

Browse files
authored
Merge pull request #4046 from graphql-java/execution-input-nullability
Improve Execution input nullability
2 parents ecaebb1 + 2553e4c commit 49a68f9

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/main/java/graphql/EngineRunningState.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class EngineRunningState {
2929
private final EngineRunningObserver engineRunningObserver;
3030
private volatile ExecutionInput executionInput;
3131
private final GraphQLContext graphQLContext;
32+
33+
// will be null after updateExecutionInput is called
34+
@Nullable
3235
private volatile ExecutionId executionId;
3336

3437
// if true the last decrementRunning() call will be ignored
@@ -164,7 +167,7 @@ private void incrementRunning() {
164167

165168
public void updateExecutionInput(ExecutionInput executionInput) {
166169
this.executionInput = executionInput;
167-
this.executionId = executionInput.getExecutionId();
170+
this.executionId = executionInput.getExecutionIdNonNull();
168171
}
169172

170173
private void changeOfState(EngineRunningObserver.RunningState runningState) {

src/main/java/graphql/ExecutionInput.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import graphql.execution.ExecutionId;
55
import graphql.execution.RawVariables;
66
import org.dataloader.DataLoaderRegistry;
7+
import org.jspecify.annotations.NullMarked;
8+
import org.jspecify.annotations.NullUnmarked;
9+
import org.jspecify.annotations.Nullable;
710

811
import java.util.Locale;
912
import java.util.Map;
@@ -17,6 +20,7 @@
1720
* This represents the series of values that can be input on a graphql query execution
1821
*/
1922
@PublicApi
23+
@NullMarked
2024
public class ExecutionInput {
2125
private final String query;
2226
private final String operationName;
@@ -58,6 +62,7 @@ public String getQuery() {
5862
/**
5963
* @return the name of the query operation
6064
*/
65+
@Nullable
6166
public String getOperationName() {
6267
return operationName;
6368
}
@@ -71,6 +76,7 @@ public String getOperationName() {
7176
* @deprecated - use {@link #getGraphQLContext()}
7277
*/
7378
@Deprecated(since = "2021-07-05")
79+
@Nullable
7480
public Object getContext() {
7581
return context;
7682
}
@@ -85,13 +91,15 @@ public GraphQLContext getGraphQLContext() {
8591
/**
8692
* @return the local context object to pass to all top level (i.e. query, mutation, subscription) data fetchers
8793
*/
94+
@Nullable
8895
public Object getLocalContext() {
8996
return localContext;
9097
}
9198

9299
/**
93100
* @return the root object to start the query execution on
94101
*/
102+
@Nullable
95103
public Object getRoot() {
96104
return root;
97105
}
@@ -119,12 +127,27 @@ public DataLoaderRegistry getDataLoaderRegistry() {
119127

120128

121129
/**
130+
* This value can be null before the execution starts, but once the execution starts, it will be set to a non-null value.
131+
* See #getExecutionIdNonNull() for a non-null version of this.
132+
*
122133
* @return Id that will be/was used to execute this operation.
123134
*/
135+
@Nullable
124136
public ExecutionId getExecutionId() {
125137
return executionId;
126138
}
127139

140+
141+
/**
142+
* Once the execution starts, GraphQL Java will make sure that this execution id is non-null.
143+
* Therefore use this method if you are sue that the execution has started to get a guaranteed non-null execution id.
144+
*
145+
* @return the non null execution id of this operation.
146+
*/
147+
public ExecutionId getExecutionIdNonNull() {
148+
return Assert.assertNotNull(this.executionId);
149+
}
150+
128151
/**
129152
* This returns the locale of this operation.
130153
*
@@ -224,6 +247,7 @@ public static Builder newExecutionInput(String query) {
224247
return new Builder().query(query);
225248
}
226249

250+
@NullUnmarked
227251
public static class Builder {
228252

229253
private String query;
@@ -245,6 +269,7 @@ public static class Builder {
245269

246270
/**
247271
* Package level access to the graphql context
272+
*
248273
* @return shhh but it's the graphql context
249274
*/
250275
GraphQLContext graphQLContext() {
@@ -312,7 +337,7 @@ public Builder context(Object context) {
312337
return this;
313338
}
314339

315-
/**
340+
/**
316341
* This will give you a builder of {@link GraphQLContext} and any values you set will be copied
317342
* into the underlying {@link GraphQLContext} of this execution input
318343
*

src/main/java/graphql/execution/EngineRunningObserver.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import graphql.ExperimentalApi;
55
import graphql.GraphQLContext;
66
import org.jspecify.annotations.NullMarked;
7+
import org.jspecify.annotations.Nullable;
78

89
/**
910
* This class lets you observe the running state of the graphql-java engine. As it processes and dispatches graphql fields,
@@ -46,8 +47,9 @@ enum RunningState {
4647
/**
4748
* This will be called when the running state of the graphql-java engine changes.
4849
*
49-
* @param executionId the id of the current execution
50+
* @param executionId the id of the current execution. This could be null when the engine starts,
51+
* if there is no execution id provided in the execution input
5052
* @param graphQLContext the graphql context
5153
*/
52-
void runningStateChanged(ExecutionId executionId, GraphQLContext graphQLContext, RunningState runningState);
54+
void runningStateChanged(@Nullable ExecutionId executionId, GraphQLContext graphQLContext, RunningState runningState);
5355
}

src/main/java/graphql/execution/RawVariables.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import graphql.PublicApi;
44
import graphql.collect.ImmutableKit;
55
import graphql.collect.ImmutableMapWithNullValues;
6+
import org.jspecify.annotations.NullMarked;
7+
import org.jspecify.annotations.Nullable;
68

79
import java.util.Map;
810

911
/**
1012
* Holds raw variables, which have not been coerced yet into {@link CoercedVariables}
1113
*/
1214
@PublicApi
15+
@NullMarked
1316
public class RawVariables {
1417
private static final RawVariables EMPTY = RawVariables.of(ImmutableKit.emptyMap());
1518
private final ImmutableMapWithNullValues<String, Object> rawVariables;
@@ -26,7 +29,7 @@ public boolean containsKey(String key) {
2629
return rawVariables.containsKey(key);
2730
}
2831

29-
public Object get(String key) {
32+
public @Nullable Object get(String key) {
3033
return rawVariables.get(key);
3134
}
3235

0 commit comments

Comments
 (0)