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

Commit 054c432

Browse files
committed
Added a GraphQLResponse return object for GraphQLTestUtils perform
This will allow for future updates to that response type, e.g. in line with (ResultActions)[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/ResultActions.html]. This will allow more fluid checks of the json response instead of having to manually traverse the `JsonNode` tree. The only reason for this return object at this stage is to prepare the public API for those additional features in the future.
1 parent 6d24e16 commit 054c432

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

example-graphql-tools/src/main/java/com/oembedler/moon/graphql/boot/resolvers/Post.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.oembedler.moon.graphql.boot.resolvers;
22

3-
class Post {
3+
public class Post {
44

55
private Long id;
66
private String text;
@@ -9,6 +9,10 @@ class Post {
99
this.id = id;
1010
}
1111

12+
public Long getId() {
13+
return id;
14+
}
15+
1216
public String getText() {
1317
return text;
1418
}

example-graphql-tools/src/test/java/com/oembedler/moon/graphql/boot/GraphQLToolsSampleApplicationTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.oembedler.moon.graphql.boot;
22

3-
import com.oembedler.moon.graphql.testing.GraphQLTestUtils;
43
import com.fasterxml.jackson.databind.JsonNode;
54
import com.fasterxml.jackson.databind.ObjectMapper;
65
import com.fasterxml.jackson.databind.node.ObjectNode;
6+
import com.oembedler.moon.graphql.boot.resolvers.Post;
7+
import com.oembedler.moon.graphql.testing.GraphQLResponse;
8+
import com.oembedler.moon.graphql.testing.GraphQLTestUtils;
79
import org.junit.Test;
810
import org.junit.runner.RunWith;
911
import org.springframework.beans.factory.annotation.Autowired;
@@ -24,7 +26,9 @@ public class GraphQLToolsSampleApplicationTest {
2426

2527
@Test
2628
public void get_comments() throws IOException {
27-
JsonNode parsedResponse = graphQLTestUtils.perform("graphql/post-get-comments.graphql");
29+
GraphQLResponse response = graphQLTestUtils.perform("graphql/post-get-comments.graphql");
30+
assertNotNull(response);
31+
JsonNode parsedResponse = response.readTree();
2832
assertNotNull(parsedResponse);
2933
assertNotNull(parsedResponse.get("data"));
3034
assertNotNull(parsedResponse.get("data").get("post"));
@@ -35,11 +39,13 @@ public void get_comments() throws IOException {
3539
public void create_post() throws IOException {
3640
ObjectNode variables = new ObjectMapper().createObjectNode();
3741
variables.put("text", "lorem ipsum dolor sit amet");
38-
JsonNode parsedResponse = graphQLTestUtils.perform("graphql/create-post.graphql",variables);
42+
GraphQLResponse response = graphQLTestUtils.perform("graphql/create-post.graphql", variables);
43+
assertNotNull(response);
44+
JsonNode parsedResponse = response.readTree();
3945
assertNotNull(parsedResponse);
4046
assertNotNull(parsedResponse.get("data"));
4147
assertNotNull(parsedResponse.get("data").get("createPost"));
42-
assertNotNull(parsedResponse.get("data").get("createPost").get("id").asText());
48+
assertNotNull(parsedResponse.get("data").get("createPost").get("id"));
4349
}
4450

4551
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.oembedler.moon.graphql.testing;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.springframework.http.ResponseEntity;
6+
7+
import java.io.IOException;
8+
import java.util.Objects;
9+
10+
public class GraphQLResponse {
11+
12+
private ResponseEntity<String> responseEntity;
13+
private ObjectMapper mapper;
14+
15+
public GraphQLResponse(ResponseEntity<String> responseEntity) {
16+
this.responseEntity = Objects.requireNonNull(responseEntity);
17+
this.mapper = new ObjectMapper();
18+
}
19+
20+
public JsonNode readTree() throws IOException {
21+
return mapper.readTree(responseEntity.getBody());
22+
}
23+
24+
}

graphql-spring-boot-starter-test/src/main/java/com/oembedler/moon/graphql/testing/GraphQLTestUtils.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ private String createJsonQuery(String graphql, ObjectNode variables)
3737
return objectMapper.writeValueAsString(wrapper);
3838
}
3939

40-
private JsonNode parse(String payload) throws IOException {
41-
return objectMapper.readTree(payload);
42-
}
43-
4440
private String loadQuery(String location) throws IOException {
4541
Resource resource = resourceLoader.getResource("classpath:" + location);
4642
return loadResource(resource);
@@ -52,11 +48,11 @@ private String loadResource(Resource resource) throws IOException {
5248
}
5349
}
5450

55-
public JsonNode perform(String graphqlResource) throws IOException {
51+
public GraphQLResponse perform(String graphqlResource) throws IOException {
5652
return perform(graphqlResource,null);
5753
}
5854

59-
public JsonNode perform(String graphqlResource, ObjectNode variables) throws IOException {
55+
public GraphQLResponse perform(String graphqlResource, ObjectNode variables) throws IOException {
6056
String graphql = loadQuery(graphqlResource);
6157
String payload = createJsonQuery(graphql,variables);
6258

@@ -66,7 +62,7 @@ public JsonNode perform(String graphqlResource, ObjectNode variables) throws IOE
6662
HttpEntity<String> httpEntity = new HttpEntity<>(payload, headers);
6763
ResponseEntity<String> response = restTemplate.exchange("/graphql", HttpMethod.POST, httpEntity, String.class);
6864

69-
return parse(response.getBody());
65+
return new GraphQLResponse(response);
7066
}
7167

7268
}

0 commit comments

Comments
 (0)