Repository navigation
Conversation
xuorig
force-pushed
the
perf/oneof-validation-single-level
branch
from
November 6, 2026 16:12
b503320 to
e436ca7
Compare
Contributor
Test ReportTest Results
Code Coverage (Java 25)
Changed Class Coverage (2 classes)
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hey folks, we've been seeing a pretty high cost with oneOf validation on our graphql gateway. I've launched an LLM at the problem, and while it does improve things, I'm not sure if you're OK with the added complexity here. Wanted to discuss it here first.
The TL;DR is that we can avoid a lot of work in the common case by validating only one level and not recursing down on variables that were already validated, with the edge case of default values.
Let me know what you think.
AI Summary
Problem
While coercing variables,
ValuesResolverConversion.externalValueToInternalValueImplcallsValuesResolverOneOfValidation.validateOneOfInputTypes(inputObjectType, coercedMap, ...)after convertingevery input object. That validator is a recursive walk: it descends into lists and into every field
defined on each input object type (
inputObjectType.getFields()+objectMap.get(...)), including fieldsthat were not provided.
Conversion itself is already recursive (
externalValueToInternalValueForObjectcallsexternalValueToInternalValueImplper field,externalValueToInternalValueForListcalls it per element), andeach of those nested calls runs the walk again over its own subtree. So:
dis validatedd + 1times, and the total work isO(depth x size)instead ofO(size);@oneOfanywhere below them pay the full cost, because the walk has to look at every field tofind out.
We noticed this in a production CPU profile of a GraphQL federation gateway, where it accounted for ~5% of total
CPU:
validateOneOfInputTypes <- validateOneOfInputTypesself-recursion, dominated byHashMap.getNodeself timefrom the per-field map lookups.
Fix
In the variable conversion path, each input object is now checked at its own level only, right after it has
been converted:
@oneOf, the existing single-object check (validateOneOfInputTypesInternal) runs on the justcoerced map;
externalValueToInternalValueImpl, whichalready checked them at their own level (and so on, recursively);
externalValueToInternalValueImpl, so those (and only those) are still walked recursively with the existingvalidateOneOfInputTypes. This is rare and only walks the default value's subtree.The literal/argument path (
ValuesResolver.getArgumentValuesImpl) is unchanged and keeps its single recursive walkper argument: literal coercion does not validate per level, so that walk is not redundant there.
The change is ~20 lines in
ValuesResolverConversionplus makingvalidateOneOfInputTypesInternalpackage-private. No public API, signature or schema changes.
Why this is equivalent
Every input object map in a coerced variable value is produced by exactly one of:
externalValueToInternalValueImplon an input object type (variables, list elements, provided object fields,external defaults,
ValuesResolver.externalValueToInternalValue,valueToInternalValuefor external values,DefaultValuesAreValidschema validation). Each such map is now validated at that point, which is exactlywhen the old code first validated it (its own
Implcall ran the walk on its own subtree before returning).The parent re-walks could only re-visit maps that had already passed.
defaultValueToInternalValuefor a literal or internal default. These were previously only covered by theparent's walk, and are still walked recursively. This gap is real and not covered by schema validation:
internal defaults (
GraphQLInputObjectField.Builder.defaultValue(Object)) are not validated byDefaultValuesAreValidat all, and a literal default such aschild: OneOf = {a: null}passesValidationUtil.isValidLiteralValue(it only checks the key count) but must fail withOneOfNullValueExceptionat coercion time. Both are covered by new tests, which fail if the default handlingis removed.
Other points checked:
externalValueToInternalValueForListconverts eachelement through
externalValueToInternalValueImpl, including the singleton case, so each element is validatedat its own level. Tested for top-level and nested lists.
and a oneOf
{a: null}is still rejected by the single-level check.InputInterceptor: the check runs on the coerced map, after interception, exactly as before.getFields(), but hidden fields are never in the coercedmap, so they were no-ops.
Implcall before its parent continued with the next sibling, the first error raised is the same in bothversions: a nested oneOf violation still wins over a later sibling's coercion error, and an earlier sibling's
coercion error still wins over a later oneOf violation (tested). For an object with literal/internal defaults,
the object's own oneOf check still runs before its defaults are walked, matching the old walk's order.
master, i.e. they pin the existing behaviour rather than a new one.Prior attempt: #4516
#4516 ("Skip OneOf input validation walk when no @OneOf type is reachable") added a schema-owned
OneOfreachability map computed at schema construction (GraphQLSchemaconstructors,FastBuilder,transformWithoutTypes), a new@InternalGraphQLSchema.inputTypeContainsOneOf, and threaded an optionalGraphQLSchemathroughValuesResolver,ValuesResolverConversion,DirectivesResolverand others. The authorclosed it because local testing did not show a benefit worth the complexity.
That approach only short-circuits types from which no
@oneOfis reachable. When a oneOf is reachable (which isthe case for any schema where oneOf matters), every level still ran the full recursive walk, so the
O(depth x size)re-walk remained. It also introduced new schema state that every schema construction path hasto keep correct.
This change instead removes the redundant re-walk itself:
@oneOftype is reachable (see theoneOfLeaf=truerows below, which Skip OneOf input validation walk when no @oneOf type is reachable #4516 couldnot have improved);
Benchmark
New
benchmark.OneOfVariableCoercionBenchmarkcallsValuesResolver.coerceVariableValueson a variable made ofnested input objects
depthlevels deep. Each level has 8 optionalStringfields of which 3 are provided, plus anextfield; the innermost level holds aLeaf { a: Int, b: String }input object, either plain(
oneOfLeaf=false, no oneOf reachable anywhere) or@oneOf(oneOfLeaf=true).singleObjectcoerces one suchvalue,
listOfObjectsa[Level0!]list of 20 of them.Settings: JMH 1.37,
AverageTime, 2 forks, 3 warmup iterations x 2 s, 5 measurement iterations x 2 s(
./gradlew jmh -PjmhInclude=benchmark.OneOfVariableCoercionBenchmark), JDK 25.0.2 (Zulu), Apple M2 Max."Before" is the same benchmark run against
master'ssrc/main.With the fix, the cost is the same whether or not a oneOf is reachable, and it scales with the size of the value
instead of size x depth.
Tests
New Spock tests in
ValuesResolverTest:through non-oneOf parents, for oneOf nested in oneOf, inside lists of oneOf, inside lists of non-oneOf parents,
in a top-level list variable, and for single values coerced into (nested) lists;
./gradlew test(JDK 25 toolchain, the project default): 6088 tests, 0 failures, 0 errors, 7 skipped. Thisincludes the existing
ValuesResolver*, oneOf,graphql.executionand ArchUnit (JMH fork limit) tests.Side observation (unchanged by this PR)
DefaultValuesAreValid.isValidExternalValueonly catchesCoercingParseValueExceptionandNonNullableValueCoercedAsNullException, so an external default with a oneOf violation makes schema building throwthe raw
OneOfTooManyKeysException/OneOfNullValueExceptionrather than anInvalidSchemaException. This is thesame on
master; the new test pins the current behaviour.