-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
.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") |
There was a problem hiding this comment.
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"() { |
There was a problem hiding this comment.
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
Test Results 311 files 311 suites 47s ⏱️ Results for commit 8d5436a. ♻️ This comment has been updated with latest results. |
*/ | ||
@Deprecated(since = "2025-02-15", forRemoval = true) |
There was a problem hiding this comment.
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)); | |||
} |
There was a problem hiding this comment.
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) | ||
) |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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"() { |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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"() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invert the test
…me type as long as they are not overlapping
…a non-overlapping child field
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.