Skip to content

Commit

Permalink
Merge pull request #3582 from Salzian/add-nullability-annotations-in-…
Browse files Browse the repository at this point in the history
…data-fetching-environment

Add nullability annotations in DataFetchingEnvironment & sub-classes
  • Loading branch information
bbakerman committed May 21, 2024
2 parents 58f82d2 + f715621 commit f3253b4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/main/java/graphql/schema/DataFetchingEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import graphql.language.OperationDefinition;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderRegistry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Locale;
Expand All @@ -37,6 +39,7 @@ public interface DataFetchingEnvironment extends IntrospectionDataFetchingEnviro
*
* @return can be null for the root query, otherwise it is never null
*/
@Nullable
<T> T getSource();

/**
Expand All @@ -61,6 +64,7 @@ public interface DataFetchingEnvironment extends IntrospectionDataFetchingEnviro
*
* @return the named argument or null if it's not present
*/
@Nullable
<T> T getArgument(String name);

/**
Expand Down Expand Up @@ -97,6 +101,7 @@ public interface DataFetchingEnvironment extends IntrospectionDataFetchingEnviro
*
* @return can NOT be null
*/
@NotNull
GraphQLContext getGraphQlContext();

/**
Expand All @@ -114,6 +119,7 @@ public interface DataFetchingEnvironment extends IntrospectionDataFetchingEnviro
*
* @return can be null if no field context objects are passed back by previous parent fields
*/
@Nullable
<T> T getLocalContext();

/**
Expand Down Expand Up @@ -228,6 +234,7 @@ public interface DataFetchingEnvironment extends IntrospectionDataFetchingEnviro
*
* @see org.dataloader.DataLoaderRegistry#getDataLoader(String)
*/
@Nullable
<K, V> DataLoader<K, V> getDataLoader(String dataLoaderName);

/**
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/graphql/schema/DataFetchingEnvironmentImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import graphql.language.OperationDefinition;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderRegistry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -129,12 +131,12 @@ public <T> T getContext() {
}

@Override
public GraphQLContext getGraphQlContext() {
public @NotNull GraphQLContext getGraphQlContext() {
return graphQLContext;
}

@Override
public <T> T getLocalContext() {
public <T> @Nullable T getLocalContext() {
return (T) localContext;
}

Expand Down Expand Up @@ -204,7 +206,7 @@ public ExecutionStepInfo getExecutionStepInfo() {
}

@Override
public <K, V> DataLoader<K, V> getDataLoader(String dataLoaderName) {
public <K, V> @Nullable DataLoader<K, V> getDataLoader(String dataLoaderName) {
return dataLoaderRegistry.getDataLoader(dataLoaderName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import graphql.language.OperationDefinition;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderRegistry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -70,12 +72,12 @@ public <T> T getContext() {
}

@Override
public GraphQLContext getGraphQlContext() {
public @NotNull GraphQLContext getGraphQlContext() {
return delegateEnvironment.getGraphQlContext();
}

@Override
public <T> T getLocalContext() {
public <T> @Nullable T getLocalContext() {
return delegateEnvironment.getLocalContext();
}

Expand Down Expand Up @@ -146,7 +148,7 @@ public QueryDirectives getQueryDirectives() {
}

@Override
public <K, V> DataLoader<K, V> getDataLoader(String dataLoaderName) {
public <K, V> @Nullable DataLoader<K, V> getDataLoader(String dataLoaderName) {
return delegateEnvironment.getDataLoader(dataLoaderName);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package graphql.schema

import graphql.GraphQLContext
import org.jetbrains.annotations.NotNull
import spock.lang.Specification

class DelegatingDataFetchingEnvironmentTest extends Specification {
Expand All @@ -22,6 +23,7 @@ class DelegatingDataFetchingEnvironmentTest extends Specification {

when:
def delegatingDFE = new DelegatingDataFetchingEnvironment(dfe) {
@NotNull
@Override
GraphQLContext getGraphQlContext() {
return GraphQLContext.of(["key": "overriddenContext"])
Expand Down

0 comments on commit f3253b4

Please sign in to comment.