Skip to content

Commit

Permalink
Better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bbakerman committed Apr 15, 2024
1 parent 5a53ec4 commit bb82efb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/main/java/graphql/schema/idl/TypeRuntimeWiring.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ public Builder strictMode() {
public Builder dataFetcher(String fieldName, DataFetcher dataFetcher) {
assertNotNull(dataFetcher, () -> "you must provide a data fetcher");
assertNotNull(fieldName, () -> "you must tell us what field");
if (strictMode && fieldDataFetchers.containsKey(fieldName)) {
throw new StrictModeWiringException(format("The field %s already has a data fetcher defined", fieldName));
if (strictMode) {
assertFieldStrictly(fieldName);
}
fieldDataFetchers.put(fieldName, dataFetcher);
return this;
Expand All @@ -161,15 +161,19 @@ public Builder dataFetchers(Map<String, DataFetcher> dataFetchersMap) {
assertNotNull(dataFetchersMap, () -> "you must provide a data fetchers map");
if (strictMode) {
dataFetchersMap.forEach((fieldName, df) -> {
if (fieldDataFetchers.containsKey(fieldName)) {
throw new StrictModeWiringException(format("The field %s already has a data fetcher defined", fieldName));
}
assertFieldStrictly(fieldName);
});
}
fieldDataFetchers.putAll(dataFetchersMap);
return this;
}

private void assertFieldStrictly(String fieldName) {
if (fieldDataFetchers.containsKey(fieldName)) {
throw new StrictModeWiringException(format("The field %s already has a data fetcher defined", fieldName));
}
}

/**
* All fields in a type need a data fetcher of some sort and this method is called to provide the default data fetcher
* that will be used for this type if no specific one has been provided per field.
Expand Down
18 changes: 16 additions & 2 deletions src/test/groovy/graphql/schema/idl/TypeRuntimeWiringTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,20 @@ class TypeRuntimeWiringTest extends Specification {
.dataFetcher("foo", DF2)
.build()
then:
thrown(StrictModeWiringException)
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"() {
Expand All @@ -57,7 +70,8 @@ class TypeRuntimeWiringTest extends Specification {
.build()
then:
inStrictMode
thrown(StrictModeWiringException)
def e = thrown(StrictModeWiringException)
e.message == "The field foo already has a data fetcher defined"

when:
TypeRuntimeWiring.setStrictModeJvmWide(false)
Expand Down

0 comments on commit bb82efb

Please sign in to comment.