Skip to content

Add cheap field definition accessor to InstrumentationFieldFetchParameters - #4544

Merged
bbakerman merged 7 commits into
graphql-java:masterfrom
rhino88:cheap-field-definition-accessor
Nov 17, 2026
Merged

bbakerman merged 7 commits into
graphql-java:masterfrom
rhino88:cheap-field-definition-accessor

Conversation

@rhino88

@rhino88 rhino88 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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>
@bbakerman
bbakerman enabled auto-merge November 17, 2026 05:05
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Test Report

Test Results

Java Version Total Passed Failed Errors Skipped
Java 17 6374 (+4 🟢) 6319 (+4 🟢) 0 (±0) 0 (±0) 55 (±0)
Java 21 6374 (+4 🟢) 6319 (+4 🟢) 0 (±0) 0 (±0) 55 (±0)
Java 25 6374 (+4 🟢) 6319 (+4 🟢) 0 (±0) 0 (±0) 55 (±0)
jcstress 32 (±0) 32 (±0) 0 (±0) 0 (±0) 0 (±0)
Total 19154 (+12 🟢) 18989 (+12 🟢) 0 (±0) 0 (±0) 165 (±0)

Code Coverage (Java 25)

Metric Covered Missed Coverage vs Master
Lines 26791 2697 90.9% ±0.0%
Branches 7683 1170 86.8% ±0.0%
Methods 7417 1090 87.2% ±0.0%

Changed Class Coverage (1 class)

Class Line Branch Method
g.e.i.p.InstrumentationFieldParameters +20.2% 🟢 +100.0% 🟢 +30.0% 🟢

Full HTML report: build artifact jacoco-html-report

Updated: 2026-11-17 22:58:45 UTC

getField() no longer invokes the ExecutionStepInfo supplier now that the
engine supplies the field definition eagerly, which left the supplier
lambda in ExecutionStrategy.resolveFieldWithInfo uncovered and tripped
the per-class coverage gate. Read getExecutionStepInfo() in the end to
end test and check it agrees with getField().
auto-merge was automatically disabled November 17, 2026 21:17

Head branch was pushed to by a user without write access

@bbakerman
bbakerman enabled auto-merge November 17, 2026 22:49
@bbakerman
bbakerman merged commit 0d41290 into graphql-java:master Nov 17, 2026
9 checks passed
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.

2 participants