Skip to content

Commit bb82efb

Browse files
committed
Better testing
1 parent 5a53ec4 commit bb82efb

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/main/java/graphql/schema/idl/TypeRuntimeWiring.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ public Builder strictMode() {
143143
public Builder dataFetcher(String fieldName, DataFetcher dataFetcher) {
144144
assertNotNull(dataFetcher, () -> "you must provide a data fetcher");
145145
assertNotNull(fieldName, () -> "you must tell us what field");
146-
if (strictMode && fieldDataFetchers.containsKey(fieldName)) {
147-
throw new StrictModeWiringException(format("The field %s already has a data fetcher defined", fieldName));
146+
if (strictMode) {
147+
assertFieldStrictly(fieldName);
148148
}
149149
fieldDataFetchers.put(fieldName, dataFetcher);
150150
return this;
@@ -161,15 +161,19 @@ public Builder dataFetchers(Map<String, DataFetcher> dataFetchersMap) {
161161
assertNotNull(dataFetchersMap, () -> "you must provide a data fetchers map");
162162
if (strictMode) {
163163
dataFetchersMap.forEach((fieldName, df) -> {
164-
if (fieldDataFetchers.containsKey(fieldName)) {
165-
throw new StrictModeWiringException(format("The field %s already has a data fetcher defined", fieldName));
166-
}
164+
assertFieldStrictly(fieldName);
167165
});
168166
}
169167
fieldDataFetchers.putAll(dataFetchersMap);
170168
return this;
171169
}
172170

171+
private void assertFieldStrictly(String fieldName) {
172+
if (fieldDataFetchers.containsKey(fieldName)) {
173+
throw new StrictModeWiringException(format("The field %s already has a data fetcher defined", fieldName));
174+
}
175+
}
176+
173177
/**
174178
* All fields in a type need a data fetcher of some sort and this method is called to provide the default data fetcher
175179
* that will be used for this type if no specific one has been provided per field.

src/test/groovy/graphql/schema/idl/TypeRuntimeWiringTest.groovy

+16-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,20 @@ class TypeRuntimeWiringTest extends Specification {
3535
.dataFetcher("foo", DF2)
3636
.build()
3737
then:
38-
thrown(StrictModeWiringException)
38+
def e = thrown(StrictModeWiringException)
39+
e.message == "The field foo already has a data fetcher defined"
40+
}
41+
42+
def "strict mode can be turned on for maps of fields"() {
43+
when:
44+
TypeRuntimeWiring.newTypeWiring("Foo")
45+
.strictMode()
46+
.dataFetcher("foo", DF1)
47+
.dataFetchers(["foo": DF2])
48+
.build()
49+
then:
50+
def e = thrown(StrictModeWiringException)
51+
e.message == "The field foo already has a data fetcher defined"
3952
}
4053

4154
def "strict mode can be turned on JVM wide"() {
@@ -57,7 +70,8 @@ class TypeRuntimeWiringTest extends Specification {
5770
.build()
5871
then:
5972
inStrictMode
60-
thrown(StrictModeWiringException)
73+
def e = thrown(StrictModeWiringException)
74+
e.message == "The field foo already has a data fetcher defined"
6175

6276
when:
6377
TypeRuntimeWiring.setStrictModeJvmWide(false)

0 commit comments

Comments
 (0)