Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 99c80ce

Browse files
feat: get GraphQL response field as defined by Jackson's JavaType
1 parent 32fe34e commit 99c80ce

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLResponse.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.graphql.spring.boot.test;
22

3+
import com.fasterxml.jackson.databind.JavaType;
34
import com.fasterxml.jackson.databind.JsonNode;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import com.graphql.spring.boot.test.assertions.GraphQLErrorListAssertion;
@@ -44,12 +45,15 @@ public String get(String path) {
4445
}
4546

4647
public <T> T get(String path, Class<T> type) {
47-
return mapper.convertValue(context.read(path, Object.class), type);
48+
return mapper.convertValue(context.read(path), type);
49+
}
50+
51+
public <T> T get(String path, JavaType type) {
52+
return mapper.convertValue(context.read(path), type);
4853
}
4954

5055
public <T> List<T> getList(String path, Class<T> type) {
51-
final List<?> raw = context.read(path, List.class);
52-
return mapper.convertValue(raw, mapper.getTypeFactory().constructCollectionType(List.class, type));
56+
return get(path, mapper.getTypeFactory().constructCollectionType(List.class, type));
5357
}
5458

5559
public ReadContext context() {

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLResponseTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.graphql.spring.boot.test;
22

3+
import com.fasterxml.jackson.databind.JavaType;
34
import com.fasterxml.jackson.databind.ObjectMapper;
45
import com.graphql.spring.boot.test.assertions.GraphQLErrorListAssertion;
56
import com.graphql.spring.boot.test.assertions.GraphQLFieldAssert;
@@ -17,9 +18,14 @@
1718
import org.springframework.beans.factory.annotation.Autowired;
1819
import org.springframework.boot.test.context.SpringBootTest;
1920
import org.springframework.boot.test.json.JsonContentAssert;
21+
import org.springframework.core.io.ClassPathResource;
2022
import org.springframework.http.ResponseEntity;
23+
import org.springframework.util.StreamUtils;
2124

25+
import java.io.IOException;
26+
import java.io.InputStream;
2227
import java.math.BigDecimal;
28+
import java.nio.charset.StandardCharsets;
2329
import java.time.LocalDate;
2430
import java.util.Arrays;
2531
import java.util.Collections;
@@ -121,6 +127,25 @@ public <T> void testGetList(
121127
assertThat(actual).containsExactlyElementsOf(expected);
122128
}
123129

130+
@DisplayName("Should get field as defined by Jackson's JavaType.")
131+
@Test
132+
public void testGetAsJavaType() throws IOException {
133+
// GIVEN
134+
final String dataPath = "$.data.externalList[*].fooList";
135+
final String response = loadClassPathResource("response-with-nested-list.json");
136+
final List<List<String>> expected = Arrays.asList(Arrays.asList("foo1", "foo2"),
137+
Collections.singletonList("foo3"));
138+
final JavaType stringList = objectMapper.getTypeFactory().constructCollectionType(List.class, String.class);
139+
final JavaType listOfStringLists = objectMapper.getTypeFactory().constructCollectionType(List.class,
140+
stringList);
141+
// WHEN
142+
final List<List<String>> actual = new GraphQLResponse(ResponseEntity.ok(response), objectMapper)
143+
.get(dataPath, listOfStringLists);
144+
// THEN
145+
assertThat(actual).containsExactlyElementsOf(expected);
146+
147+
}
148+
124149
@ParameterizedTest
125150
@ValueSource(strings = {
126151
"{\"data\": { \"foo\":\"bar\" } }",
@@ -242,10 +267,17 @@ void testJsonAssertion() {
242267
.containsExactly(graphQLResponse, path);
243268
}
244269

270+
private String loadClassPathResource(String path) throws IOException {
271+
try(InputStream resourceStream = new ClassPathResource(path).getInputStream()) {
272+
return StreamUtils.copyToString(resourceStream, StandardCharsets.UTF_8);
273+
}
274+
}
275+
245276
@Data
246277
@AllArgsConstructor
247278
@NoArgsConstructor
248279
private static class FooBar {
280+
249281
private String foo;
250282
private BigDecimal bar;
251283
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"data": {
3+
"externalList": [
4+
{
5+
"fooList": [
6+
"foo1", "foo2"
7+
]
8+
},
9+
{
10+
"fooList": [
11+
"foo3"
12+
]
13+
}
14+
]
15+
}
16+
}

0 commit comments

Comments
 (0)