Skip to content

Add cheap field definition accessor to InstrumentationFieldFetchParameters - #4544

Open
rhino88 wants to merge 5 commits into
graphql-java:masterfrom
rhino88:cheap-field-definition-accessor
Open

rhino88 wants to merge 5 commits into
graphql-java:masterfrom
rhino88:cheap-field-definition-accessor

Conversation

@rhino88

@rhino88 rhino88 commented Nov 7, 2026 •

Copy link
Copy Markdown

Problem

InstrumentationFieldFetchParameters.getField() (inherited from InstrumentationFieldParameters) goes through the ExecutionStepInfo supplier, which is () -> environment.get().getExecutionStepInfo(). So asking for the field definition in beginFieldFetching materialises the full lazily built DataFetchingEnvironmentImpl plus its ExecutionStepInfo on every fetch. For trivial PropertyDataFetcher fetches (a LightDataFetcher that never builds the environment itself) the instrumentation becomes the only reason the environment is built at all. ExecutionStrategy.fetchField already has the GraphQLFieldDefinition resolved eagerly before the environment supplier is created, so this cost is avoidable.

Motivating case: apollographql/federation-jvm#469 (see the "Possible follow-up in graphql-java" discussion) and apollographql/federation-jvm#470, whose isLeafField() check has to call parameters.getEnvironment().getFieldType() and notes this cost as unavoidable today.

Change

  • InstrumentationFieldParameters gains a @Nullable GraphQLFieldDefinition and a new constructor (ExecutionContext, @Nullable GraphQLFieldDefinition, Supplier<ExecutionStepInfo>). getField() returns the definition directly when it was supplied and otherwise falls back to the existing supplier path. The existing public constructor is unchanged and delegates with null.
  • InstrumentationFieldFetchParameters gains the matching constructor (ExecutionContext, @Nullable GraphQLFieldDefinition, Supplier<DataFetchingEnvironment>, ExecutionStrategyParameters, boolean), and passes the definition to super. getField() is inherited and now returns the eager definition; the getEnvironment() Javadoc points instrumentations that only need the field definition at getField() since it does not build the environment. The existing public constructor is unchanged and delegates with null.
  • ExecutionStrategy passes the already resolved fieldDef at both construction sites (beginFieldExecution and beginFieldFetching). These were the only two construction sites in main or test code. SubscriptionExecutionStrategy and DeferredExecutionSupport still use the old constructor as they only hold a step info supplier there.
  • getEnvironment() and getExecutionStepInfo() are untouched: they return the same objects and materialise at the same time as before. TracingInstrumentation is not changed in this PR; a follow-up could use getField() there.

Before / after for getField() in beginFieldFetching

Before After
Objects built DataFetchingEnvironmentImpl, ExecutionStepInfo (via createExecutionStepInfo), argument/normalized field suppliers, DataFetchingFieldSelectionSetImpl, QueryDirectivesImpl none
Supplier invoked yes (environment.get()) no
Result executionStepInfo.get().getFieldDefinition() the GraphQLFieldDefinition fetchField already had

So an instrumentation can now do GraphQLTypeUtil.isLeaf(parameters.getField().getType()) on every fetch for free, and only pay for the environment when it actually calls getEnvironment().

Tests

New Spock spec InstrumentationFieldFetchParametersTest:

  • getField() with a throwing environment Supplier returns the supplied definition and GraphQLTypeUtil.isLeaf works on it, proving the supplier is never invoked.
  • Old constructor still falls back to the supplier (counted, called exactly once).
  • Same no-invocation check for InstrumentationFieldParameters with a throwing ExecutionStepInfo supplier.
  • End to end: a SimplePerformantInstrumentation reads parameters.getField() in beginFieldFetching against a real schema and sees the right leaf/non-leaf classification per field.

./gradlew test (JDK 25 toolchain):

6076 tests run in 290716 ms
BUILD SUCCESSFUL in 5m 1s

6076 tests, 7 skipped, 0 failures, 0 errors.

🤖 Generated with Claude Code

rhino88 and others added 5 commits November 7, 2026 11:41
…eters

InstrumentationFieldFetchParameters.getField() previously went through
the ExecutionStepInfo supplier, which is backed by the lazily built
DataFetchingEnvironment. Any instrumentation that only wanted the field
definition in beginFieldFetching therefore paid for a
DataFetchingEnvironmentImpl plus ExecutionStepInfo on every fetch, even
for trivial PropertyDataFetcher fetches that never build the environment
themselves.

ExecutionStrategy.fetchField already resolves the GraphQLFieldDefinition
eagerly, so pass it into the parameters and have getField() return it
directly without touching the supplier. The existing public constructors
are kept and delegate with a null field definition, which falls back to
the supplier path.

Motivated by apollographql/federation-jvm#469.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…docs

The override in InstrumentationFieldFetchParameters only delegated to
super and existed to carry Javadoc, so its guidance now lives on
getEnvironment(). Also rename the misnamed getExecutionContext
constructor parameter, keep the base class Javadoc to what it knows
about (the ExecutionStepInfo supplier), and import ExecutionStepInfo in
the test instead of using the fully-qualified name.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The engine-level test now checks that beginFieldExecution and
beginFieldFetching both receive the field definition eagerly, so it
fails if ExecutionStrategy stops passing fieldDef. Previously it only
checked getField() results, which were correct either way.

Reword the getField() Javadoc to say the fallback reads the
ExecutionStepInfo supplier, which is what the code has always done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant