Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3565 from graphql-java/strict-mode-runtime-wiring
strictMode for RuntimeWiring and TypeRuntimeWiring
- Loading branch information
Showing
5 changed files
with
243 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
17 changes: 17 additions & 0 deletions
17
src/main/java/graphql/schema/idl/errors/StrictModeWiringException.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package graphql.schema.idl.errors; | ||
|
||
import graphql.GraphQLException; | ||
import graphql.PublicApi; | ||
import graphql.schema.idl.RuntimeWiring; | ||
import graphql.schema.idl.TypeRuntimeWiring; | ||
|
||
/** | ||
* An exception that is throw when {@link RuntimeWiring.Builder#strictMode()} or {@link TypeRuntimeWiring.Builder#strictMode()} is true and | ||
* something gets redefined. | ||
*/ | ||
@PublicApi | ||
public class StrictModeWiringException extends GraphQLException { | ||
public StrictModeWiringException(String msg) { | ||
super(msg); | ||
} | ||
} |
This file contains 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
88 changes: 88 additions & 0 deletions
88
src/test/groovy/graphql/schema/idl/TypeRuntimeWiringTest.groovy
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package graphql.schema.idl | ||
|
||
import graphql.schema.DataFetcher | ||
import graphql.schema.idl.errors.StrictModeWiringException | ||
import spock.lang.Specification | ||
|
||
class TypeRuntimeWiringTest extends Specification { | ||
|
||
void setup() { | ||
TypeRuntimeWiring.setStrictModeJvmWide(false) | ||
} | ||
|
||
void cleanup() { | ||
TypeRuntimeWiring.setStrictModeJvmWide(false) | ||
} | ||
|
||
DataFetcher DF1 = env -> "x" | ||
DataFetcher DF2 = env -> "y" | ||
|
||
def "strict mode is off by default"() { | ||
when: | ||
def typeRuntimeWiring = TypeRuntimeWiring.newTypeWiring("Foo") | ||
.dataFetcher("foo", DF1) | ||
.dataFetcher("foo", DF2) | ||
.build() | ||
then: | ||
typeRuntimeWiring.getFieldDataFetchers().get("foo") == DF2 | ||
} | ||
|
||
def "strict mode can be turned on"() { | ||
when: | ||
TypeRuntimeWiring.newTypeWiring("Foo") | ||
.strictMode() | ||
.dataFetcher("foo", DF1) | ||
.dataFetcher("foo", DF2) | ||
.build() | ||
then: | ||
def e = thrown(StrictModeWiringException) | ||
e.message == "The field foo already has a data fetcher defined" | ||
} | ||
|
||
def "strict mode can be turned on for maps of fields"() { | ||
when: | ||
TypeRuntimeWiring.newTypeWiring("Foo") | ||
.strictMode() | ||
.dataFetcher("foo", DF1) | ||
.dataFetchers(["foo": DF2]) | ||
.build() | ||
then: | ||
def e = thrown(StrictModeWiringException) | ||
e.message == "The field foo already has a data fetcher defined" | ||
} | ||
|
||
def "strict mode can be turned on JVM wide"() { | ||
|
||
|
||
when: | ||
def inStrictMode = TypeRuntimeWiring.getStrictModeJvmWide() | ||
then: | ||
!inStrictMode | ||
|
||
|
||
when: | ||
TypeRuntimeWiring.setStrictModeJvmWide(true) | ||
inStrictMode = TypeRuntimeWiring.getStrictModeJvmWide() | ||
|
||
TypeRuntimeWiring.newTypeWiring("Foo") | ||
.dataFetcher("foo", DF1) | ||
.dataFetcher("foo", DF2) | ||
.build() | ||
then: | ||
inStrictMode | ||
def e = thrown(StrictModeWiringException) | ||
e.message == "The field foo already has a data fetcher defined" | ||
|
||
when: | ||
TypeRuntimeWiring.setStrictModeJvmWide(false) | ||
inStrictMode = TypeRuntimeWiring.getStrictModeJvmWide() | ||
|
||
TypeRuntimeWiring.newTypeWiring("Foo") | ||
.dataFetcher("foo", DF1) | ||
.dataFetcher("foo", DF2) | ||
.build() | ||
then: | ||
!inStrictMode | ||
noExceptionThrown() | ||
} | ||
} |