Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ protected Object resolveFieldWithInfo(ExecutionContext executionContext, Executi

Instrumentation instrumentation = executionContext.getInstrumentation();
InstrumentationContext<Object> fieldCtx = nonNullCtx(instrumentation.beginFieldExecution(
new InstrumentationFieldParameters(executionContext, executionStepInfo), executionContext.getInstrumentationState()
new InstrumentationFieldParameters(executionContext, fieldDef, executionStepInfo), executionContext.getInstrumentationState()
));

Object fetchedValueObj = fetchField(executionContext, parameters);
Expand Down Expand Up @@ -554,7 +554,7 @@ private Object fetchField(GraphQLFieldDefinition fieldDef, ExecutionContext exec

Instrumentation instrumentation = executionContext.getInstrumentation();

InstrumentationFieldFetchParameters instrumentationFieldFetchParams = new InstrumentationFieldFetchParameters(executionContext, dataFetchingEnvironment, parameters, originalDataFetcher instanceof TrivialDataFetcher);
InstrumentationFieldFetchParameters instrumentationFieldFetchParams = new InstrumentationFieldFetchParameters(executionContext, fieldDef, dataFetchingEnvironment, parameters, originalDataFetcher instanceof TrivialDataFetcher);
FieldFetchingInstrumentationContext fetchCtx = FieldFetchingInstrumentationContext.nonNullCtx(instrumentation.beginFieldFetching(instrumentationFieldFetchParams,
executionContext.getInstrumentationState())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import graphql.execution.ExecutionStrategyParameters;
import graphql.execution.instrumentation.Instrumentation;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLFieldDefinition;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.function.Supplier;

Expand All @@ -19,13 +21,33 @@ public class InstrumentationFieldFetchParameters extends InstrumentationFieldPar
private final ExecutionStrategyParameters executionStrategyParameters;
private final boolean trivialDataFetcher;

public InstrumentationFieldFetchParameters(ExecutionContext getExecutionContext, Supplier<DataFetchingEnvironment> environment, ExecutionStrategyParameters executionStrategyParameters, boolean trivialDataFetcher) {
super(getExecutionContext, () -> environment.get().getExecutionStepInfo());
public InstrumentationFieldFetchParameters(ExecutionContext executionContext, Supplier<DataFetchingEnvironment> environment, ExecutionStrategyParameters executionStrategyParameters, boolean trivialDataFetcher) {
this(executionContext, null, environment, executionStrategyParameters, trivialDataFetcher);
}

/**
* @param executionContext the execution context
* @param fieldDefinition the field definition if it is already known, which allows {@link #getField()} to answer
* without building the {@link DataFetchingEnvironment}, or null if it is not known
* @param environment a supplier of the {@link DataFetchingEnvironment} for this field fetch
* @param executionStrategyParameters the execution strategy parameters
* @param trivialDataFetcher whether the data fetcher is a {@link graphql.TrivialDataFetcher}
*/
public InstrumentationFieldFetchParameters(ExecutionContext executionContext, @Nullable GraphQLFieldDefinition fieldDefinition, Supplier<DataFetchingEnvironment> environment, ExecutionStrategyParameters executionStrategyParameters, boolean trivialDataFetcher) {
super(executionContext, fieldDefinition, () -> environment.get().getExecutionStepInfo());
this.environment = environment;
this.executionStrategyParameters = executionStrategyParameters;
this.trivialDataFetcher = trivialDataFetcher;
}

/**
* This returns the {@link DataFetchingEnvironment} for this field fetch. Note that the environment is built lazily
* and calling this will materialise it, which has a cost. The same is true of {@link #getExecutionStepInfo()}.
* If you only need the field definition (for example to decide whether the field type is a leaf type) then
* use {@link #getField()} instead, which the engine supplies eagerly without building the environment.
*
* @return the data fetching environment for this field fetch
*/
public DataFetchingEnvironment getEnvironment() {
return environment.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import graphql.execution.instrumentation.Instrumentation;
import graphql.schema.GraphQLFieldDefinition;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.function.Supplier;

Expand All @@ -18,16 +19,42 @@
@PublicApi
public class InstrumentationFieldParameters {
private final ExecutionContext executionContext;
private final @Nullable GraphQLFieldDefinition fieldDefinition;
private final Supplier<ExecutionStepInfo> executionStepInfo;

public InstrumentationFieldParameters(ExecutionContext executionContext, Supplier<ExecutionStepInfo> executionStepInfo) {
this(executionContext, null, executionStepInfo);
}

/**
* @param executionContext the execution context
* @param fieldDefinition the field definition if it is already known, which allows {@link #getField()} to answer
* without invoking the {@link ExecutionStepInfo} supplier, or null if it is not known
* @param executionStepInfo a supplier of the {@link ExecutionStepInfo} for this field
*/
public InstrumentationFieldParameters(ExecutionContext executionContext, @Nullable GraphQLFieldDefinition fieldDefinition, Supplier<ExecutionStepInfo> executionStepInfo) {
this.executionContext = executionContext;
this.fieldDefinition = fieldDefinition;
this.executionStepInfo = executionStepInfo;
}

public ExecutionContext getExecutionContext() {
return executionContext;
}

/**
* This returns the {@link GraphQLFieldDefinition} of the field being instrumented.
* <p>
* When the field definition was supplied at construction time, this is a cheap call that does NOT invoke
* the {@link ExecutionStepInfo} supplier. Otherwise it reads the field definition from the
* {@link ExecutionStepInfo} supplier, which may be expensive depending on what the supplier has to build.
*
* @return the field definition of the field being instrumented
*/
public GraphQLFieldDefinition getField() {
if (fieldDefinition != null) {
return fieldDefinition;
}
return assertNotNull(executionStepInfo.get().getFieldDefinition(), "fieldDefinition must not be null");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package graphql.execution.instrumentation

import graphql.ExecutionInput
import graphql.TestUtil
import graphql.execution.ExecutionStepInfo
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters
import graphql.execution.instrumentation.parameters.InstrumentationFieldParameters
import graphql.schema.DataFetchingEnvironment
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLTypeUtil
import spock.lang.Specification

import java.util.concurrent.atomic.AtomicInteger
import java.util.function.Supplier

import static graphql.Scalars.GraphQLString
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition

class InstrumentationFieldFetchParametersTest extends Specification {

def "getField does not invoke the environment supplier when the field definition is supplied"() {
given:
def fieldDef = newFieldDefinition().name("name").type(GraphQLString).build()
Supplier<DataFetchingEnvironment> throwingSupplier = { throw new IllegalStateException("environment should not be built") }

def parameters = new InstrumentationFieldFetchParameters(null, fieldDef, throwingSupplier, null, true)

when:
def field = parameters.getField()

then:
field.is(fieldDef)
GraphQLTypeUtil.isLeaf(field.getType())
parameters.isTrivialDataFetcher()
}

def "getField falls back to the supplier when no field definition is supplied"() {
given:
def fieldDef = newFieldDefinition().name("name").type(GraphQLString).build()
def callCount = new AtomicInteger()
def stepInfo = Mock(ExecutionStepInfo) {
getFieldDefinition() >> fieldDef
}
def environment = Mock(DataFetchingEnvironment) {
getExecutionStepInfo() >> stepInfo
}
Supplier<DataFetchingEnvironment> countingSupplier = { callCount.incrementAndGet(); environment }

def parameters = new InstrumentationFieldFetchParameters(null, countingSupplier, null, false)

when:
def field = parameters.getField()

then:
field.is(fieldDef)
callCount.get() == 1
}

def "InstrumentationFieldParameters getField does not invoke the step info supplier when the field definition is supplied"() {
given:
def fieldDef = newFieldDefinition().name("name").type(GraphQLString).build()
Supplier<ExecutionStepInfo> throwingSupplier = { throw new IllegalStateException("step info should not be built") }

def parameters = new InstrumentationFieldParameters(null, fieldDef, throwingSupplier)

when:
def field = parameters.getField()

then:
field.is(fieldDef)
}

def "the engine supplies the field definition so an instrumentation can inspect the field type in beginFieldFetching"() {
given:
def sdl = '''
type Query {
hero: Hero
}
type Hero {
name: String
friends: [Hero]
}
'''
def root = [hero: [name: "Luke", friends: [[name: "Han", friends: []]]]]
def graphQL = TestUtil.graphQL(sdl).build()

Map<String, Boolean> leafByField = [:]
List<Object> suppliedFieldDefinitions = []
def instrumentation = new SimplePerformantInstrumentation() {
@Override
InstrumentationContext<Object> beginFieldExecution(InstrumentationFieldParameters parameters, InstrumentationState state) {
suppliedFieldDefinitions.add(suppliedFieldDefinition(parameters))
return SimpleInstrumentationContext.noOp()
}

@Override
FieldFetchingInstrumentationContext beginFieldFetching(InstrumentationFieldFetchParameters parameters, InstrumentationState state) {
suppliedFieldDefinitions.add(suppliedFieldDefinition(parameters))
GraphQLFieldDefinition fieldDef = parameters.getField()
leafByField.put(fieldDef.getName(), GraphQLTypeUtil.isLeaf(fieldDef.getType()))
return FieldFetchingInstrumentationContext.NOOP
}
}

when:
def result = graphQL.transform({ it.instrumentation(instrumentation) })
.execute(ExecutionInput.newExecutionInput('{ hero { name friends { name } } }').root(root).build())

then:
result.errors.isEmpty()
leafByField == [hero: false, name: true, friends: false]
// every field execution and field fetch was given the field definition eagerly by the engine
!suppliedFieldDefinitions.isEmpty()
suppliedFieldDefinitions.every { it != null }
}

private static Object suppliedFieldDefinition(InstrumentationFieldParameters parameters) {
def field = InstrumentationFieldParameters.getDeclaredField("fieldDefinition")
field.setAccessible(true)
return field.get(parameters)
}
}