Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions src/main/java/graphql/TrivialDataFetcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package graphql;

import graphql.schema.DataFetcher;


/**
* 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.
*/
@PublicSpi
public interface TrivialDataFetcher<T> extends DataFetcher<T> {
}
3 changes: 2 additions & 1 deletion src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import graphql.Internal;
import graphql.PublicSpi;
import graphql.SerializationError;
import graphql.TrivialDataFetcher;
import graphql.TypeMismatchError;
import graphql.TypeResolutionEnvironment;
import graphql.UnresolvedTypeError;
Expand Down Expand Up @@ -251,7 +252,7 @@ protected CompletableFuture<Object> fetchField(ExecutionContext executionContext

Instrumentation instrumentation = executionContext.getInstrumentation();

InstrumentationFieldFetchParameters instrumentationFieldFetchParams = new InstrumentationFieldFetchParameters(executionContext, fieldDef, environment, parameters, dataFetcher.isTrivialDataFetcher());
InstrumentationFieldFetchParameters instrumentationFieldFetchParams = new InstrumentationFieldFetchParameters(executionContext, fieldDef, environment, parameters, dataFetcher instanceof TrivialDataFetcher);
InstrumentationContext<Object> fetchCtx = instrumentation.beginFieldFetch(instrumentationFieldFetchParams);

CompletableFuture<Object> fetchedValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.PublicApi;
import graphql.TrivialDataFetcher;
import graphql.execution.Async;
import graphql.execution.DataFetcherExceptionHandler;
import graphql.execution.DataFetcherExceptionHandlerParameters;
Expand Down Expand Up @@ -258,7 +259,7 @@ private CompletableFuture<FetchedValues> fetchData(ExecutionContext executionCon
.build();

DataFetcher supplied = fieldDef.getDataFetcher();
boolean trivialDataFetcher = supplied.isTrivialDataFetcher();
boolean trivialDataFetcher = supplied instanceof TrivialDataFetcher;
BatchedDataFetcher batchedDataFetcher = batchingFactory.create(supplied);

Instrumentation instrumentation = executionContext.getInstrumentation();
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/graphql/relay/SimpleListConnection.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package graphql.relay;

import graphql.PublicApi;
import graphql.TrivialDataFetcher;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

Expand All @@ -16,7 +17,7 @@
import static java.util.Base64.getEncoder;

@PublicApi
public class SimpleListConnection<T> implements DataFetcher<Connection<T>> {
public class SimpleListConnection<T> implements DataFetcher<Connection<T>>, TrivialDataFetcher<Connection<T>> {

static final String DUMMY_CURSOR_PREFIX = "simple-cursor";
private final String prefix;
Expand All @@ -41,11 +42,6 @@ private List<Edge<T>> buildEdges() {
return edges;
}

@Override
public boolean isTrivialDataFetcher() {
return true;
}

@Override
public Connection<T> get(DataFetchingEnvironment environment) {

Expand Down
9 changes: 0 additions & 9 deletions src/main/java/graphql/schema/DataFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,4 @@ public interface DataFetcher<T> {
T get(DataFetchingEnvironment environment) throws Exception;


/**
* 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.
*
* @return true if the data fetcher can be considered a trivial data fetcher.
*/
default boolean isTrivialDataFetcher() {
return false;
}
}
11 changes: 2 additions & 9 deletions src/main/java/graphql/schema/PropertyDataFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import graphql.Assert;
import graphql.GraphQLException;
import graphql.PublicApi;
import graphql.TrivialDataFetcher;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -46,7 +47,7 @@
* @see graphql.schema.DataFetcher
*/
@PublicApi
public class PropertyDataFetcher<T> implements DataFetcher<T> {
public class PropertyDataFetcher<T> implements DataFetcher<T>, TrivialDataFetcher<T> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simply be

 public class PropertyDataFetcher<T> implements TrivialDataFetcher<T> 


private final String propertyName;
private final Function<Object, Object> function;
Expand Down Expand Up @@ -122,14 +123,6 @@ public String getPropertyName() {
return propertyName;
}

/**
* @return true - because PropertyDataFetcher is one the most trivial fetchers
*/
@Override
public boolean isTrivialDataFetcher() {
return true;
}

@SuppressWarnings("unchecked")
@Override
public T get(DataFetchingEnvironment environment) {
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/graphql/schema/StaticDataFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@


import graphql.PublicApi;
import graphql.TrivialDataFetcher;

/**
* A {@link graphql.schema.DataFetcher} that always returns the same value
*/
@PublicApi
public class StaticDataFetcher implements DataFetcher {
public class StaticDataFetcher implements DataFetcher, TrivialDataFetcher {


private final Object value;
Expand All @@ -21,8 +22,4 @@ public Object get(DataFetchingEnvironment environment) {
return value;
}

@Override
public boolean isTrivialDataFetcher() {
return true;
}
}