Skip to content

Change runtime wiring redefinition checks to be strict by default #3824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 31, 2025

Conversation

dondonz
Copy link
Member

@dondonz dondonz commented Feb 15, 2025

The problem

As I reported in #3760 I was stumped by my code not working in prod where integration tests seemed fine. It turned out that the type wiring that I thought was brand new, was actually redefining another type in a long forgotten part of the codebase.

This was a particularly tough bug because the order that Spring for GraphQL loaded the type wirings was non-deterministic, so you never knew which type wiring you got in prod. Of course Spring for GraphQL hasn't done anything wrong here, it is hard to distinguish which of two different wirings you want to use.

I think I haven't noticed this as a problem before, because when writing small tests in this repo, the order that datafetchers (and other elements) are redefined/clobbered are predictable.

The solution

It turns out we already had a "strict mode" option to catch a number of type runtime wiring redefinitions, including type redefinition, as well as redefinitions on datafetchers, default datafetchers, scalars, and enums. However, it's not enabled by default so it's usually not switched on.

For most users, I think having strict checks for redefinition is the better default option. It is very strange to have two type runtime wirings for the same type, and one can clobber the other in an unpredictable way.

The only time we should allow more than 1 TypeRuntimeWiring definition for the same type is when adding DataFetchers for distinct child fields. By definition, we can only have one type resolver and one enum values provider in a TypeRuntimeWiring, so these must not be redefined.

This PR changes the default to strict checks set to true.

This will be a "breaking change", but it is possible to return to the previous behaviour.

@dondonz dondonz added this to the 23.x breaking changes milestone Feb 15, 2025
@dondonz dondonz added the breaking change requires a new major version to be relased label Feb 15, 2025
.type(newTypeWiring("Page").dataFetcher("text", resolve("The page full text", 100)))
.type(newTypeWiring("Comment").dataFetcher("content", resolve("Full content", 100)))
.type(newTypeWiring("Comment").dataFetcher("author", resolve([name: "Author name"], 10)))
.type(newTypeWiring("Post")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some test updates, extra datafetcher registrations should happen inside the same newTypeWiring builder

@@ -193,15 +193,14 @@ class RuntimeWiringTest extends Specification {
newWiring.fieldVisibility == fieldVisibility
}

def "strict mode can stop certain redefinitions"() {
def "strict mode, on by default, can stop certain redefinitions"() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating tests and adding new ones to lock in the change in default behaviour

Copy link
Contributor

github-actions bot commented Feb 15, 2025

Test Results

  311 files    311 suites   47s ⏱️
3 575 tests 3 569 ✅ 6 💤 0 ❌
3 664 runs  3 658 ✅ 6 💤 0 ❌

Results for commit 8d5436a.

♻️ This comment has been updated with latest results.

*/
@Deprecated(since = "2025-02-15", forRemoval = true)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting forRemoval = true to give clear indication this is going to be removed soon

@@ -307,6 +320,9 @@ public Builder type(TypeRuntimeWiring typeRuntimeWiring) {

DataFetcher<?> defaultDataFetcher = typeRuntimeWiring.getDefaultDataFetcher();
if (defaultDataFetcher != null) {
if (strictMode && defaultDataFetchers.containsKey(typeName)) {
throw new StrictModeWiringException(format("The type %s already has a default data fetcher defined", typeName));
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An edge case that wasn't previously covered: if a type wiring's default datafetcher was redefined at the runtime wiring level (and not the type wiring level), previously didn't raise an error

See test below to lock in the new behaviour

.type(newTypeWiring("User")
.dataFetcher("friends", friendsDF)
.dataFetcher("mates", friendsDF)
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set datafetchers so it doesn't count as type redefinition

DataFetcher DF1 = env -> "x"
DataFetcher DF2 = env -> "x"
TypeResolver TR1 = env -> null
EnumValuesProvider EVP1 = name -> null

when:
RuntimeWiring.newRuntimeWiring()
.strictMode()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these lines, as the default is strictness on

@@ -248,34 +243,91 @@ class RuntimeWiringTest extends Specification {
e5.message == "The type Foo has already has a default data fetcher defined"
}

def "overwrite default data fetchers if they are null"() {

def "strict mode, if set to off, won't stop certain redefinitions"() {
Copy link
Member Author

@dondonz dondonz Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests to lock in the strict mode off case


then:
def error = thrown(StrictModeWiringException)
error.message == "The type Foo already has a default data fetcher defined"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covering a new edge case, where the default datafetcher is redefined at the runtime wiring level

We already had a test for when it is redefined inside the type wiring

}

void cleanup() {
TypeRuntimeWiring.setStrictModeJvmWide(false)
TypeRuntimeWiring.setStrictModeJvmWide(true)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to tests to reflect the new default

def "strict mode can be turned on JVM wide"() {


def "strict mode can be turned off and on JVM wide"() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invert the test

@dondonz dondonz changed the title WIP Make strict runtime wiring default WIP Change runtime wiring redefinition checks to be strict by default Feb 15, 2025
@dondonz dondonz changed the title WIP Change runtime wiring redefinition checks to be strict by default Change runtime wiring redefinition checks to be strict by default Feb 16, 2025
@dondonz dondonz merged commit cd99d17 into master Mar 31, 2025
2 checks passed
@dondonz dondonz deleted the make-strict-runtime-wiring-default branch March 31, 2025 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change requires a new major version to be relased
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants