Skip to content

Commit 7199aa2

Browse files
authored
Merge pull request #955 from graphql-java-kickstart/agent-friendly-p12-resolver-plan
Build one resolver plan per field and delete the resolver hierarchy
2 parents d5a0bc2 + e738451 commit 7199aa2

27 files changed

Lines changed: 1246 additions & 924 deletions

‎.claude/skills/verify-return-value-unwrapping/SKILL.md‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: verify-return-value-unwrapping
3-
description: Exhaustively check, up to a bound, that the value of a resolver method, a field with no matching getter or a typed Map reaches graphql-java fully unwrapped. Futures are awaited, generic wrappers are transformed, Optional and DataFetcherResult are handled, and list order, nulls and failures are kept, for every return type nested up to 3 containers deep. Use it before pushing any change to TypeShape (the type walk, ContainerKind, the generic wrapper rules or the Unwrapper), the data fetchers in MethodFieldResolver, PropertyFieldResolver or MapFieldResolver, or the GenericWrapper defaults, and whenever a bug report says graphql-java got a CompletableFuture, Optional or wrapper object as a field value.
3+
description: Exhaustively check, up to a bound, that the value of a resolver method, a field with no matching getter or a typed Map reaches graphql-java fully unwrapped. Futures are awaited, generic wrappers are transformed, Optional and DataFetcherResult are handled, and list order, nulls and failures are kept, for every return type nested up to 3 containers deep. Use it before pushing any change to TypeShape (the type walk, ContainerKind, the generic wrapper rules or the Unwrapper), the data fetchers in ResolverPlan, or the GenericWrapper defaults, and whenever a bug report says graphql-java got a CompletableFuture, Optional or wrapper object as a field value.
44
---
55

66
# Verify return value unwrapping
@@ -38,11 +38,13 @@ and an all-non-null GraphQL type.
3838

3939
Each return type runs as three kinds of resolver, each compiled as a Java class at runtime:
4040

41-
| Kind | Generated resolver | Data fetcher |
41+
| Kind | Generated resolver | Member and data fetcher in `ResolverPlan.kt` |
4242
|---|---|---|
43-
| `method` | `public <type> field()` on the query resolver | `MethodFieldResolver` |
44-
| `property` | `public <type> field;` on the query resolver, which is also what a Kotlin `@JvmField` compiles to | `PropertyFieldResolver` |
45-
| `map` | `extends HashMap<String, <type>>`, bound to `type Holder` and returned by `Query.holder` | `MapFieldResolver` |
43+
| `method` | `public <type> field()` on the query resolver | `MethodMember`, `LightMethodFieldResolverDataFetcher` |
44+
| `property` | `public <type> field;` on the query resolver, which is also what a Kotlin `@JvmField` compiles to | `PropertyMember`, `PropertyFieldResolverDataFetcher` |
45+
| `map` | `extends HashMap<String, <type>>`, bound to `type Holder` and returned by `Query.holder` | `MapMember`, `MapFieldResolverDataFetcher` |
46+
47+
Each kind's `resolverKind` is the noun of a `ResolverKind`, and a `ResolverKind` without a kind here ends in `MODEL ERROR`.
4648

4749
The check builds each schema through the public `SchemaParser` API, which rejects about a fifth of the combinations, such as
4850
`Optional` below the top level. For every accepted one it executes the query once for each value that makes at most
@@ -73,7 +75,7 @@ as `-Dunwrapping.args`, and prints the report. On a failure it also prints the p
7375
|---|---|
7476
| 0 | `PASS`, or `PASS apart from the known findings above` |
7577
| 1 | `FAIL`: a return type that isn't a known finding breaks the property. `NEW`: a known finding matches more accepted return types than its line records |
76-
| 2 | `MODEL ERROR`: the checkout or the check didn't compile, the build rejected a return type the check must exercise, or a `ContainerKind` in `TypeShape.kt` has no `Container` in `Check.java`, so a pass would prove nothing |
78+
| 2 | `MODEL ERROR`: the checkout or the check didn't compile, the build rejected a return type the check must exercise, a `ContainerKind` in `TypeShape.kt` has no `Container` in `Check.java`, or a `ResolverKind` in `ResolverPlan.kt` has no `Kind`, so a pass would prove nothing |
7779
| 3 | `STALE`: a known finding matches a return type that no longer fails, or fewer accepted return types than its line records. Or the run's coverage differs from `pinnedCoverage` in `Check.java` |
7880

7981
Options: `--depth N`, `--budget N`, `--property-budget N`, `--show N` (counterexamples and stale return types printed, default 15), and `--rejected`, which lists
@@ -154,7 +156,7 @@ or delete it when none fail, so that a regression shows up as `FAIL`.
154156
## What this does not cover
155157

156158
- Suspend functions, subscriptions and `Publisher`, arrays, and Kotlin-declared resolver methods. The generated resolvers
157-
are Java methods without parameters, so only `LightMethodFieldResolverDataFetcher` runs, not
159+
are Java methods without parameters, so only `LightMethodFieldResolverDataFetcher` in `ResolverPlan.kt` runs, not
158160
`MethodFieldResolverDataFetcher`.
159161
- Wrappers picked by a subclass of the value (`Box` and `SpecialBox` in `NestedGenericWrapperTest`), wrappers with a type
160162
argument index other than 0, custom `schemaWrapper`s other than the list one, transformers that return a `FutureTask`,
@@ -169,24 +171,22 @@ or delete it when none fail, so that a regression shows up as `FAIL`.
169171

170172
## Coverage and sync
171173

172-
Last confirmed to describe the code at commit `5ce3d1db` (2026-11-05). It covers:
174+
Last confirmed to describe the code at commit `1b351934` (2026-11-05). It covers:
173175

174176
| File | What |
175177
|---|---|
176-
| `src/main/kotlin/graphql/kickstart/tools/resolver/TypeShape.kt` | all of it: the walk, `ContainerKind`, `GenericWrappers`, `resolveTypeVariables`, the `Unwrapper` and its steps |
177-
| `src/main/kotlin/graphql/kickstart/tools/resolver/MethodFieldResolver.kt` | `createDataFetcher` (the `TypeShape.Unwrapper`), `returnShape`, `LightMethodFieldResolverDataFetcher` |
178+
| `src/main/kotlin/graphql/kickstart/tools/resolver/TypeShape.kt` | the walk, `ContainerKind`, `GenericWrappers`, `resolveTypeVariables`, the `Unwrapper` and its steps |
179+
| `src/main/kotlin/graphql/kickstart/tools/resolver/ResolverPlan.kt` | `dataFetcher` (the `TypeShape.Unwrapper` per member), `returnShape`, `ResolverKind`, the members' `declaredValueType`, and the `ResolverDataFetcher` subclasses |
178180
| `src/main/kotlin/graphql/kickstart/tools/SchemaParserOptions.kt` | `GenericWrapper` and the default wrappers in `build()` |
179-
| `src/main/kotlin/graphql/kickstart/tools/resolver/PropertyFieldResolver.kt` | all of it |
180-
| `src/main/kotlin/graphql/kickstart/tools/resolver/MapFieldResolver.kt` | all of it, for Map subclasses with a typed value |
181181

182182
To see whether a change needs another look:
183183

184184
```bash
185-
git log --oneline 5ce3d1db..HEAD -- src/main/kotlin/graphql/kickstart/tools/resolver/TypeShape.kt src/main/kotlin/graphql/kickstart/tools/resolver/MethodFieldResolver.kt src/main/kotlin/graphql/kickstart/tools/SchemaParserOptions.kt src/main/kotlin/graphql/kickstart/tools/resolver/PropertyFieldResolver.kt src/main/kotlin/graphql/kickstart/tools/resolver/MapFieldResolver.kt
185+
git log --oneline 1b351934..HEAD -- src/main/kotlin/graphql/kickstart/tools/resolver/TypeShape.kt src/main/kotlin/graphql/kickstart/tools/resolver/ResolverPlan.kt src/main/kotlin/graphql/kickstart/tools/SchemaParserOptions.kt
186186
```
187187

188188
Move the sync commit forward only after you've read those changes and confirmed that the containers, variants and
189189
expected values in `Check.java` still describe them. For example, a new default wrapper or a new kind of unwrapping
190190
step needs a new `Container` with its variants and its plain value in `build`. A new `ContainerKind` ends in
191-
`MODEL ERROR` until it has one. After an intended change, rerun the check,
191+
`MODEL ERROR` until it has one, and so does a new `ResolverKind` until `Check.java` has a `Kind` with its noun. After an intended change, rerun the check,
192192
and break the new code on purpose once in a scratch worktree to see the check fail before you trust its pass.

‎AGENTS.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ resolver classes. Published to Maven Central. Kotlin, with a few Java files, tar
77

88
- `src/main/kotlin/graphql/kickstart/tools`: `SchemaParser` and its builder and options, `MergedDefinitions.kt` (merges each
99
SDL type with its extensions into a `MergedType`. Other code, apart from the Relay factory, reads SDL types only through
10-
it), `SchemaClassScanner` (matches schema types to classes), `resolver/` (field resolvers and data fetchers), `directive/`,
10+
it), `SchemaClassScanner` (matches schema types to classes), `resolver/` (`ResolverPlan.kt` finds the Java member behind each field and builds its data fetcher, `TypeShape.kt` walks
11+
declared types and unwraps values, `ArgumentConverter.kt` converts arguments), `directive/`,
1112
`relay/`, `proxy/` (finds the class behind a Spring, Guice, Weld or Javassist proxied resolver), `util/`
1213
- `src/test/kotlin`: JUnit tests, one class per feature. A test for an issue goes into the class for its feature.
1314
`EndToEndTest` and `EndToEndSpecHelper` hold the shared schema and resolvers
@@ -93,13 +94,13 @@ happening. "docs only" means nothing does yet, so the rule depends on you readin
9394
| Rule | Enforced by |
9495
|---|---|
9596
| The value of a resolver method, a field with no matching getter or a typed Map is unwrapped by one rule that the schema build and the data fetcher share | `TypeShape`, whose one walk yields both the class the schema build matches and the layers that `TypeShape.Unwrapper` unwraps in the method, field and Map data fetchers. An untyped Map's values reach graphql-java as they are. A container kind can't be added without its placement rule and its value step, since both are abstract on the enum, and `ReturnValueUnwrappingTest` fails until the bounded check builds values for it |
96-
| A resolver method's parameter layout, type variables and source are decided once, at schema build | docs only |
97+
| A resolver method's parameter layout, type variables and source are decided once, at schema build | `ResolverPlan`, which reads the field's member once and hands the scan its type shapes, the data fetcher its parameters and `UnmappedInputFieldChecker` its argument targets. Its member variants are private to `ResolverPlan.kt`, each decides its `ResolverKind`, and none builds a data fetcher. `ResolverPlan.dataFetcher` gives every member a `ResolverDataFetcher`, a sealed class whose constructor takes the `TypeShape.Unwrapper`, so a lambda or a fetcher without one doesn't compile. A new `ResolverKind` doesn't compile until the lookup's `when` handles it, a new `ExtraParameter` doesn't compile without its binder, the fixture tests in `ResolverCompatibilityTest` fail, naming the fixture to add, until they build a sample of each and see its value unwrapped, and `ReturnValueUnwrappingTest` ends in `MODEL ERROR` until the bounded check runs the new kind. `ResolverOwnershipTest` fails on member reflection outside `ResolverPlan.kt` (`java.lang.reflect` calls and signature reads, `MethodHandles`, `kotlin.reflect.full` and `kotlin.reflect.jvm` calls, `KClass` member lookups, a value typed as a `KFunction` or `KProperty`, and commons-lang3's field and method utilities, apart from the type reads in `ScanDictionary.findInputValueTypeInType(String, Class)`), on a member that depends on a data fetcher, on a library data fetcher that isn't a `ResolverDataFetcher` declared in `ResolverPlan.kt`, and on generic-wrapper or `DataFetcherResult` handling outside `TypeShape.kt`. Its exemptions name the exact classes each owning file declares. It doesn't see `Future`, `CompletionStage` or `Optional` checks outside `TypeShape.kt`, `Class.forName` lookups, `Method.getReturnType`, a `KFunction` held as `Any` and called through a cast, reflection a library does for you (graphql-java's `PropertyDataFetcher`, `java.beans.Expression`), a new helper in an owning file that reflects for another file, or a `DataFetcher { }` lambda outside `ResolverPlan.kt`. The fixture tests and the bounded check catch a value these leave wrapped only in the fetchers `ResolverPlan` builds |
9798
| A change to how resolvers are found, bound or unwrapped keeps what a method, a property and a Map field return | `ResolverCompatibilityTest`, which compares field and Map values with a method's, and the bounded check in `ReturnValueUnwrappingTest`, which compares all three with the plain value. Neither has known findings, so a value that differs on a return type they run fails the build. The bounded check also pins how many return types it generates, and for each kind how many the build rejects and how many executions run, so a change that stops running some fails until the same diff updates `pinnedCoverage`. It doesn't judge whether a newly rejected return type should build |
9899
| One output has one builder. Don't add a second path that builds the same thing | docs only |
99100
| Read an SDL type's members merged with its extensions, never from the raw definition | `MergedType` in `MergedDefinitions.kt` and `DefinitionsBoundaryTest`, which fails on a raw member, directive or child-node read, an extension class, an SDL node walk or a `TypeDefinitionRegistry` call outside it. A read by reflection gets past it. `TypeExtensionTest` builds and queries a schema with an extension of every kind, so a build that leaves an extension's fields, interfaces, members, values, input fields or directives out of the built type fails it, however the code read them |
100101
| Every kind of SDL type starts its graphql-java type from the same recipe | `MergedType.prepare`, `DefinitionsBoundaryTest` and `TypeKindRecipeTest` |
101102
| Reuse an existing structure before you add a parallel one | docs only |
102-
| Argument conversion goes through one entry point | docs only |
103+
| Argument conversion goes through one entry point | `ArgumentConverter`, which `ResolverPlan` builds once per argument. `ResolverOwnershipTest` fails on a TokenBuffer or a Jackson `readValue`, `readValueAs`, `convertValue`, `updateValue`, `treeToValue` or `valueToTree` outside `ArgumentConverter.kt`, and on an `ArgumentConverter` built or called outside `ResolverPlan.kt`. Jackson's per-call retry for scalars it can't convert stays inside `ArgumentConverter` |
103104
| An input field's Java type comes from the mapping Jackson uses, not from guessed getters | docs only |
104105
| Every recursive walk over the type graph handles cycles, and a test covers a cycle | docs only |
105106
| Public signatures stay binary compatible, data class `copy()` and `componentN` included | japicmp with `build/japicmp-kotlin-public.groovy` at `mvn verify`, which allows excludes and other narrowing options only at an X.0.0 version |

‎src/main/kotlin/graphql/kickstart/tools/ResolverInfo.kt‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package graphql.kickstart.tools
22

3-
import graphql.kickstart.tools.resolver.FieldResolverScanner
43
import graphql.kickstart.tools.util.GraphQLRootResolver
54
import graphql.kickstart.tools.util.JavaType
65
import graphql.kickstart.tools.util.eraseUnboundedWildcards
76
import org.apache.commons.lang3.reflect.TypeUtils
87
import java.lang.reflect.ParameterizedType
98

109
internal abstract class ResolverInfo {
11-
abstract fun getFieldSearches(): List<FieldResolverScanner.Search>
10+
abstract fun getFieldSearches(): List<ResolverSearch>
1211

1312
fun getRealResolverClass(resolver: GraphQLResolver<*>, options: SchemaParserOptions) =
1413
options.proxyHandlers.find { it.canHandle(resolver) }
@@ -43,10 +42,10 @@ internal class NormalResolverInfo(
4342
return type
4443
}
4544

46-
override fun getFieldSearches(): List<FieldResolverScanner.Search> {
45+
override fun getFieldSearches(): List<ResolverSearch> {
4746
return listOf(
48-
FieldResolverScanner.Search(resolverType, this, resolver, dataClassType),
49-
FieldResolverScanner.Search(dataClassType, this, null)
47+
ResolverSearch(resolverType, this, resolver, dataClassType),
48+
ResolverSearch(dataClassType, this, null)
5049
)
5150
}
5251
}
@@ -60,11 +59,11 @@ internal class MultiResolverInfo(
6059
private val dataClass: JavaType
6160
) : ResolverInfo() {
6261

63-
override fun getFieldSearches(): List<FieldResolverScanner.Search> {
62+
override fun getFieldSearches(): List<ResolverSearch> {
6463
return resolverInfoList
6564
.asSequence()
66-
.map { FieldResolverScanner.Search(it.resolverType, this, it.resolver, it.dataClassType) }
67-
.plus(FieldResolverScanner.Search(dataClass, this, null))
65+
.map { ResolverSearch(it.resolverType, this, it.resolver, it.dataClassType) }
66+
.plus(ResolverSearch(dataClass, this, null))
6867
.toList()
6968
}
7069
}
@@ -75,16 +74,29 @@ internal class RootResolverInfo(
7574
val isSubscription: Boolean = false
7675
) : ResolverInfo() {
7776
override fun getFieldSearches() =
78-
resolvers.map { FieldResolverScanner.Search(getRealResolverClass(it, options), this, it) }
77+
resolvers.map { ResolverSearch(getRealResolverClass(it, options), this, it) }
7978
}
8079

8180
internal class DataClassResolverInfo(private val dataClass: JavaType) : ResolverInfo() {
8281
override fun getFieldSearches() =
83-
listOf(FieldResolverScanner.Search(dataClass, this, null))
82+
listOf(ResolverSearch(dataClass, this, null))
8483
}
8584

8685
internal class MissingResolverInfo : ResolverInfo() {
87-
override fun getFieldSearches(): List<FieldResolverScanner.Search> = listOf()
86+
override fun getFieldSearches(): List<ResolverSearch> = listOf()
87+
}
88+
89+
internal data class ResolverSearch(
90+
val type: JavaType,
91+
val resolverInfo: ResolverInfo,
92+
val source: Any?,
93+
val requiredFirstParameterType: Class<*>? = null
94+
) {
95+
val isSubscription get() = resolverInfo is RootResolverInfo && resolverInfo.isSubscription
96+
97+
val firstArgumentIndex get() = if (requiredFirstParameterType != null) 1 else 0
98+
99+
fun withoutSourceParameter() = copy(requiredFirstParameterType = null)
88100
}
89101

90102
internal class ResolverError(message: String, cause: Throwable? = null) : RuntimeException(message, cause)

‎src/main/kotlin/graphql/kickstart/tools/ScannedSchemaObjects.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package graphql.kickstart.tools
22

3-
import graphql.kickstart.tools.resolver.FieldResolver
3+
import graphql.kickstart.tools.resolver.ResolverPlan
44
import graphql.kickstart.tools.util.JavaType
55
import graphql.language.FieldDefinition
66
import graphql.schema.GraphQLScalarType
@@ -13,7 +13,7 @@ internal data class ScannedSchemaObjects(
1313
val definitions: MergedDefinitions,
1414
val types: Set<MergedType<*>>,
1515
val customScalars: CustomScalarMap,
16-
val fieldResolversByType: Map<MergedType.Object, MutableMap<FieldDefinition, FieldResolver>>,
16+
val resolverPlansByType: Map<MergedType.Object, Map<FieldDefinition, ResolverPlan>>,
1717
val unusedTypes: Set<MergedType<*>>,
1818
val directiveOnlyEnums: Set<MergedType.Enum>
1919
)

0 commit comments

Comments
 (0)