Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ plugins {
id "groovy"
id "me.champeau.jmh" version "0.7.3"
id "io.github.reyerizo.gradle.jcstress" version "0.9.0"
id "net.ltgt.errorprone" version '5.1.1'
id "net.ltgt.errorprone" version '5.2.0'
id 'jacoco'
//
// Kotlin just for tests - not production code
id 'org.jetbrains.kotlin.jvm' version '2.4.20'
id 'org.jetbrains.kotlin.jvm' version '2.4.21'
}

java {
Expand Down Expand Up @@ -234,7 +234,7 @@ shadow {
dependencies {
api 'com.graphql-java:java-dataloader:6.0.0'
api 'org.reactivestreams:reactive-streams:' + reactiveStreamsVersion
api "org.jspecify:jspecify:1.0.0"
api "org.jspecify:jspecify:1.0.1"

implementation 'org.antlr:antlr4-runtime:' + antlrVersion
implementation 'com.google.guava:guava:' + guavaVersion
Expand Down Expand Up @@ -273,8 +273,8 @@ dependencies {
// comment this in if you want to run JMH benchmarks from idea
// jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.37'

errorprone 'com.uber.nullaway:nullaway:0.13.8'
errorprone 'com.google.errorprone:error_prone_core:2.50.0'
errorprone 'com.uber.nullaway:nullaway:0.14.3'
errorprone 'com.google.errorprone:error_prone_core:2.52.0'

// just tests - no Kotlin otherwise
testImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
Expand Down Expand Up @@ -761,6 +761,14 @@ tasks.withType(Test) {
useJUnitPlatform()
}
maxHeapSize = "1g"
// Consumer contract tests use the build toolchain and the existing NullAway processor.
doFirst {
systemProperty 'nullability.javac', javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(25)
}.get().executablePath.asFile.absolutePath
systemProperty 'nullability.classpath', (sourceSets.main.output + sourceSets.main.compileClasspath).asPath
systemProperty 'nullability.processorpath', configurations.annotationProcessor.asPath
}
testLogging {
events "FAILED", "SKIPPED"
exceptionFormat = "FULL"
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/graphql/EngineRunningState.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public EngineRunningState(ExecutionInput executionInput, Profiler profiler) {



public <U, T> CompletableFuture<U> handle(CompletableFuture<T> src, BiFunction<? super T, Throwable, ? extends U> fn) {
public <U extends @Nullable Object, T extends @Nullable Object> CompletableFuture<U> handle(CompletableFuture<T> src, BiFunction<? super @Nullable T, @Nullable Throwable, ? extends U> fn) {
if (engineRunningObserver == null) {
return src.handle(fn);
}
Expand All @@ -69,7 +69,7 @@ public <U, T> CompletableFuture<U> handle(CompletableFuture<T> src, BiFunction<?
return result;
}

public <T> CompletableFuture<T> whenComplete(CompletableFuture<T> src, BiConsumer<? super T, ? super Throwable> fn) {
public <T extends @Nullable Object> CompletableFuture<T> whenComplete(CompletableFuture<T> src, BiConsumer<? super @Nullable T, ? super @Nullable Throwable> fn) {
if (engineRunningObserver == null) {
return src.whenComplete(fn);
}
Expand All @@ -86,7 +86,7 @@ public <T> CompletableFuture<T> whenComplete(CompletableFuture<T> src, BiConsume
return result;
}

public <U, T> CompletableFuture<U> compose(CompletableFuture<T> src, Function<? super T, ? extends CompletionStage<U>> fn) {
public <U extends @Nullable Object, T extends @Nullable Object> CompletableFuture<U> compose(CompletableFuture<T> src, Function<? super T, ? extends CompletionStage<U>> fn) {
if (engineRunningObserver == null) {
return src.thenCompose(fn);
}
Expand All @@ -113,7 +113,7 @@ public <U, T> CompletableFuture<U> compose(CompletableFuture<T> src, Function<?
}


private <T> CompletableFuture<T> observeCompletableFutureStart(CompletableFuture<T> future) {
private <T extends @Nullable Object> CompletableFuture<T> observeCompletableFutureStart(CompletableFuture<T> future) {
if (engineRunningObserver == null) {
return future;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/GraphQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ private ParseAndValidateResult parse(ExecutionInput executionInput, GraphQLSchem
} else {
parseInstrumentationCtx.onCompleted(parseResult.getDocument(), null);

DocumentAndVariables documentAndVariables = parseResult.getDocumentAndVariables();
DocumentAndVariables documentAndVariables = assertNotNull(parseResult.getDocumentAndVariables(), "Successful parsing must produce a document and variables");
documentAndVariables = instrumentation.instrumentDocumentAndVariables(documentAndVariables, parameters, instrumentationState);
return ParseAndValidateResult.newResult()
.document(documentAndVariables.getDocument()).variables(documentAndVariables.getVariables()).build();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/Profiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ default void dataLoaderUsed(String dataLoaderName) {

}

default void fieldFetched(Object fetchedObject, DataFetcher<?> originalDataFetcher, DataFetcher<?> dataFetcher, ResultPath path, GraphQLFieldDefinition fieldDef, GraphQLOutputType parentType) {
default void fieldFetched(@Nullable Object fetchedObject, DataFetcher<?> originalDataFetcher, DataFetcher<?> dataFetcher, ResultPath path, GraphQLFieldDefinition fieldDef, GraphQLOutputType parentType) {

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/ProfilerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void collectInstrumentationClasses(List<String> result, Instrumentation


@Override
public void fieldFetched(Object fetchedObject, DataFetcher<?> originalDataFetcher, DataFetcher<?> dataFetcher, ResultPath path, GraphQLFieldDefinition fieldDef, GraphQLOutputType parentType) {
public void fieldFetched(@Nullable Object fetchedObject, DataFetcher<?> originalDataFetcher, DataFetcher<?> dataFetcher, ResultPath path, GraphQLFieldDefinition fieldDef, GraphQLOutputType parentType) {
String key = "/" + String.join("/", path.getKeysOnly());
if (Introspection.isIntrospectionTypes(GraphQLTypeUtil.unwrapAll(fieldDef.getType()))
|| Introspection.isIntrospectionTypes(GraphQLTypeUtil.unwrapAll(parentType))
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/graphql/TrivialDataFetcher.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package graphql;

import graphql.schema.DataFetcher;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;


/**
* Mark a DataFetcher as trivial:
* If a data fetcher is simply mapping data from an object to a field, it can be considered a trivial data fetcher for the purposes
* of tracing and so on.
*/
@NullMarked
@PublicSpi
public interface TrivialDataFetcher<T> extends DataFetcher<T> {
public interface TrivialDataFetcher<T extends @Nullable Object> extends DataFetcher<T> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public CompletableFuture<InstrumentationState> createStateAsync(InstrumentationC

@Override
public InstrumentationContext<List<ValidationError>> beginValidation(InstrumentationValidationParameters parameters, @Nullable InstrumentationState rawState) {
State state = ofState(rawState);
State state = ofState(assertNotNull(rawState, "Instrumentation state must be created before callbacks"));
// for API backwards compatibility reasons we capture the validation parameters, so we can put them into QueryComplexityInfo
state.instrumentationValidationParameters.set(parameters);
return noOp();
Expand All @@ -101,7 +101,7 @@ public InstrumentationContext<ExecutionResult> beginExecuteOperation(Instrumenta
if (totalComplexity > maxComplexity) {
QueryComplexityInfo queryComplexityInfo = QueryComplexityInfo.newQueryComplexityInfo()
.complexity(totalComplexity)
.instrumentationValidationParameters(state.instrumentationValidationParameters.get())
.instrumentationValidationParameters(assertNotNull(state, "Instrumentation state must be created before callbacks").instrumentationValidationParameters.get())
.instrumentationExecuteOperationParameters(instrumentationExecuteOperationParameters)
.build();
boolean throwAbortException = maxQueryComplexityExceededFunction.apply(queryComplexityInfo);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/graphql/analysis/QueryReducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import graphql.PublicApi;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* Used by {@link QueryTraverser} to reduce the fields of a Document (or part of it) to a single value.
Expand All @@ -13,7 +14,7 @@
@PublicApi
@NullMarked
@FunctionalInterface
public interface QueryReducer<T> {
public interface QueryReducer<T extends @Nullable Object> {

/**
* Called each time a field is visited.
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/graphql/analysis/QueryTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;

import static graphql.Assert.assertNotNull;
import static graphql.language.AstNodeAdapter.AST_NODE_ADAPTER;
Expand Down Expand Up @@ -64,11 +65,11 @@ private QueryTransformer(GraphQLSchema schema,
*
* @param queryVisitor the query visitor that will be called back.
*
* @return changed root
* @return changed root, or null if the root was deleted
*
* @throws IllegalArgumentException if there are multiple root nodes.
*/
public Node transform(QueryVisitor queryVisitor) {
public @Nullable Node transform(QueryVisitor queryVisitor) {
QueryVisitor noOp = new QueryVisitorStub();
NodeVisitorWithTypeTracking nodeVisitor = new NodeVisitorWithTypeTracking(queryVisitor,
noOp,
Expand All @@ -84,7 +85,7 @@ public Node transform(QueryVisitor queryVisitor) {

@Override
public TraversalControl enter(TraverserContext<Node> context) {
return context.thisNode().accept(context, nodeVisitor);
return assertNotNull(context.thisNode(), "thisNode should not be null").accept(context, nodeVisitor);
}

@Override
Expand Down
38 changes: 20 additions & 18 deletions src/main/java/graphql/analysis/QueryTraverser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
import graphql.schema.GraphQLCompositeType;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.util.MutableRef;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.jspecify.annotations.NullUnmarked;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
Expand All @@ -41,6 +44,7 @@
* It also doesn't consider field merging, which means for example {@code { user{firstName} user{firstName}} } will result in four
* visitField calls.
*/
@NullMarked
@PublicApi
public class QueryTraverser {

Expand All @@ -54,7 +58,7 @@ public class QueryTraverser {

private QueryTraverser(GraphQLSchema schema,
Document document,
String operation,
@Nullable String operation,
CoercedVariables coercedVariables,
QueryTraversalOptions options
) {
Expand All @@ -69,7 +73,7 @@ private QueryTraverser(GraphQLSchema schema,

private QueryTraverser(GraphQLSchema schema,
Document document,
String operation,
@Nullable String operation,
RawVariables rawVariables,
QueryTraversalOptions options
) {
Expand Down Expand Up @@ -98,7 +102,7 @@ private QueryTraverser(GraphQLSchema schema,
this.options = options;
}

public Object visitDepthFirst(QueryVisitor queryVisitor) {
public @Nullable Object visitDepthFirst(QueryVisitor queryVisitor) {
return visitImpl(queryVisitor, null);
}

Expand Down Expand Up @@ -130,17 +134,16 @@ public void visitPreOrder(QueryVisitor visitor) {
*
* @return the calculated overall value
*/
@SuppressWarnings("unchecked")
public <T> T reducePostOrder(QueryReducer<T> queryReducer, T initialValue) {
// compiler hack to make acc final and mutable :-)
final Object[] acc = {initialValue};
public <T extends @Nullable Object> T reducePostOrder(QueryReducer<T> queryReducer, T initialValue) {
MutableRef<T> acc = new MutableRef<>();
acc.value = initialValue;
visitPostOrder(new QueryVisitorStub() {
@Override
public void visitField(QueryVisitorFieldEnvironment env) {
acc[0] = queryReducer.reduceField(env, (T) acc[0]);
acc.value = queryReducer.reduceField(env, acc.value);
}
});
return (T) acc[0];
return acc.value;
}

/**
Expand All @@ -152,17 +155,16 @@ public void visitField(QueryVisitorFieldEnvironment env) {
*
* @return the calculated overall value
*/
@SuppressWarnings("unchecked")
public <T> T reducePreOrder(QueryReducer<T> queryReducer, T initialValue) {
// compiler hack to make acc final and mutable :-)
final Object[] acc = {initialValue};
public <T extends @Nullable Object> T reducePreOrder(QueryReducer<T> queryReducer, T initialValue) {
MutableRef<T> acc = new MutableRef<>();
acc.value = initialValue;
visitPreOrder(new QueryVisitorStub() {
@Override
public void visitField(QueryVisitorFieldEnvironment env) {
acc[0] = queryReducer.reduceField(env, (T) acc[0]);
acc.value = queryReducer.reduceField(env, acc.value);
}
});
return (T) acc[0];
return acc.value;
}

private GraphQLObjectType getRootTypeFromOperation(OperationDefinition operationDefinition) {
Expand All @@ -183,10 +185,10 @@ private List<Node> childrenOf(Node<?> node) {
return node.getChildren();
}
FragmentSpread fragmentSpread = (FragmentSpread) node;
return singletonList(fragmentsByName.get(fragmentSpread.getName()));
return singletonList(assertNotNull(fragmentsByName.get(fragmentSpread.getName()), "Fragment %s must exist", fragmentSpread.getName()));
}

private Object visitImpl(QueryVisitor visitFieldCallback, Boolean preOrder) {
private @Nullable Object visitImpl(QueryVisitor visitFieldCallback, @Nullable Boolean preOrder) {
Map<Class<?>, Object> rootVars = new LinkedHashMap<>();
rootVars.put(QueryTraversalContext.class, new QueryTraversalContext(rootParentType, null, null, GraphQLContext.getDefault()));

Expand Down
18 changes: 9 additions & 9 deletions src/main/java/graphql/analysis/values/ValueTraverser.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public List<GraphQLInputSchemaElement> getUnwrappedInputElements() {
*/
public static DataFetchingEnvironment visitPreOrder(DataFetchingEnvironment environment, ValueVisitor visitor) {
GraphQLFieldDefinition fieldDefinition = environment.getFieldDefinition();
Map<String, Object> originalArgs = environment.getArguments();
Map<String, Object> newArgs = visitPreOrder(originalArgs, fieldDefinition, visitor);
Map<String, @Nullable Object> originalArgs = environment.getArguments();
Map<String, @Nullable Object> newArgs = visitPreOrder(originalArgs, fieldDefinition, visitor);
if (newArgs != originalArgs) {
return DataFetchingEnvironmentImpl.newDataFetchingEnvironment(environment).arguments(newArgs).build();
}
Expand All @@ -124,7 +124,7 @@ public static DataFetchingEnvironment visitPreOrder(DataFetchingEnvironment envi
*
* @return the same set of arguments if nothing changes or new ones if the visitor changes anything
*/
public static Map<String, Object> visitPreOrder(Map<String, Object> coercedArgumentValues, GraphQLFieldDefinition fieldDefinition, ValueVisitor visitor) {
public static Map<String, @Nullable Object> visitPreOrder(Map<String, @Nullable Object> coercedArgumentValues, GraphQLFieldDefinition fieldDefinition, ValueVisitor visitor) {
List<GraphQLArgument> fieldArguments = fieldDefinition.getArguments();
boolean copied = false;
for (GraphQLArgument fieldArgument : fieldArguments) {
Expand Down Expand Up @@ -227,8 +227,8 @@ public static Map<String, Object> visitPreOrder(Map<String, Object> coercedArgum
assertTrue(coercedValue instanceof Map, "A input object type MUST have an Map<String,Object> value");
}
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) coercedValue;
Map<String, Object> newMap = visitor.visitInputObjectValue(map, inputObjectType, containingElements);
Map<String, @Nullable Object> map = (Map<String, @Nullable Object>) coercedValue;
Map<String, @Nullable Object> newMap = visitor.visitInputObjectValue(map, inputObjectType, containingElements);
if (newMap == ABSENCE_SENTINEL) {
return ABSENCE_SENTINEL;
}
Expand Down Expand Up @@ -272,8 +272,8 @@ public static Map<String, Object> visitPreOrder(Map<String, Object> coercedArgum
assertTrue(coercedValue instanceof List, "A list type MUST have an List value");
}
@SuppressWarnings("unchecked")
List<Object> list = (List<Object>) coercedValue;
List<Object> newList = visitor.visitListValue(list, listInputType, containingElements);
List<@Nullable Object> list = (List<@Nullable Object>) coercedValue;
List<@Nullable Object> newList = visitor.visitListValue(list, listInputType, containingElements);
if (newList == ABSENCE_SENTINEL) {
return ABSENCE_SENTINEL;
}
Expand All @@ -292,7 +292,7 @@ public static Map<String, Object> visitPreOrder(Map<String, Object> coercedArgum
// copy previous values up to this point
copiedList = ImmutableList.builder();
for (int j = 0; j < i; j++) {
copiedList.add(newList.get(j));
copiedList.add(assertNotNull(newList.get(j), "list element must not be null"));
}
if (newValue != ABSENCE_SENTINEL) {
copiedList.add(assertNotNull(newValue, "list element must not be null"));
Expand All @@ -314,7 +314,7 @@ private static boolean hasChanged(@Nullable Object newValue, @Nullable Object ol
return newValue != oldValue || newValue == ABSENCE_SENTINEL;
}

private static void setNewValue(Map<String, Object> newMap, String key, @Nullable Object newValue) {
private static void setNewValue(Map<String, @Nullable Object> newMap, String key, @Nullable Object newValue) {
if (newValue == ABSENCE_SENTINEL) {
newMap.remove(key);
} else {
Expand Down
Loading
Loading