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

Commit 8cfd508

Browse files
committed
✨✅ add support for testing with graphql fragments
1 parent 75682d8 commit 8cfd508

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import static org.junit.Assert.assertEquals;
1818
import static org.junit.Assert.assertTrue;
1919

20+
import java.util.ArrayList;
21+
import java.util.List;
22+
2023
@RunWith(SpringRunner.class)
2124
@GraphQLTest
2225
public class GraphQLToolsSampleApplicationTest {
@@ -33,6 +36,16 @@ public void get_comments() throws IOException {
3336
assertEquals("1", response.get("$.data.post.id"));
3437
}
3538

39+
@Test
40+
public void get_comments_withFragments() throws IOException {
41+
List<String> fragments = new ArrayList<>();
42+
fragments.add("graphql/all-comment-fields-fragment.graphql");
43+
GraphQLResponse response = graphQLTestTemplate.postForResource("graphql/post-get-comments-with-fragment.graphql", fragments);
44+
assertNotNull(response);
45+
assertTrue(response.isOk());
46+
assertEquals("1", response.get("$.data.post.id"));
47+
}
48+
3649
@Test
3750
public void create_post() throws IOException {
3851
ObjectNode variables = new ObjectMapper().createObjectNode();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fragment AllCommentFields on Comment {
2+
id
3+
description
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
query {
2+
post(id: "1") {
3+
id
4+
comments {
5+
...AllCommentFields
6+
}
7+
}
8+
}

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@
1616

1717
import java.io.IOException;
1818
import java.io.InputStream;
19+
1920
import java.nio.charset.StandardCharsets;
21+
import java.util.List;
2022

2123
public class GraphQLTestTemplate {
2224

23-
@Autowired
24-
private ResourceLoader resourceLoader;
25-
@Autowired(required = false)
26-
private TestRestTemplate restTemplate;
27-
@Value("${graphql.servlet.mapping:/graphql}")
28-
private String graphqlMapping;
25+
@Autowired private ResourceLoader resourceLoader;
26+
@Autowired(required = false) private TestRestTemplate restTemplate;
27+
@Value("${graphql.servlet.mapping:/graphql}") private String graphqlMapping;
2928

3029
private ObjectMapper objectMapper = new ObjectMapper();
3130
private HttpHeaders headers = new HttpHeaders();
3231

33-
private String createJsonQuery(String graphql, ObjectNode variables)
34-
throws JsonProcessingException {
32+
private String createJsonQuery(String graphql, ObjectNode variables) throws JsonProcessingException {
3533

3634
ObjectNode wrapper = objectMapper.createObjectNode();
3735
wrapper.put("query", graphql);
@@ -53,7 +51,7 @@ private String loadResource(Resource resource) throws IOException {
5351
/**
5452
* Add an HTTP header that will be sent with each request this sends.
5553
*
56-
* @param name Name (key) of HTTP header to add.
54+
* @param name Name (key) of HTTP header to add.
5755
* @param value Value of HTTP header to add.
5856
*/
5957
public void addHeader(String name, String value) {
@@ -77,11 +75,10 @@ public void clearHeaders() {
7775
}
7876

7977
/**
80-
* @deprecated Use {@link #postForResource(String)} instead
81-
*
8278
* @param graphqlResource path to the classpath resource containing the GraphQL query
8379
* @return GraphQLResponse containing the result of query execution
8480
* @throws IOException if the resource cannot be loaded from the classpath
81+
* @deprecated Use {@link #postForResource(String)} instead
8582
*/
8683
public GraphQLResponse perform(String graphqlResource) throws IOException {
8784
return postForResource(graphqlResource);
@@ -93,6 +90,16 @@ public GraphQLResponse perform(String graphqlResource, ObjectNode variables) thr
9390
return post(payload);
9491
}
9592

93+
public GraphQLResponse perform(String graphqlResource, ObjectNode variables, List<String> fragmentResources) throws IOException {
94+
StringBuilder sb = new StringBuilder();
95+
for (String fragmentResource : fragmentResources) {
96+
sb.append(loadQuery(fragmentResource));
97+
}
98+
String graphql = sb.append(loadQuery(graphqlResource)).toString();
99+
String payload = createJsonQuery(graphql, variables);
100+
return post(payload);
101+
}
102+
96103
/**
97104
* Loads a GraphQL query from the given classpath resource and sends it to the GraphQL server.
98105
*
@@ -104,6 +111,19 @@ public GraphQLResponse postForResource(String graphqlResource) throws IOExceptio
104111
return perform(graphqlResource, null);
105112
}
106113

114+
/**
115+
* Loads a GraphQL query from the given classpath resource, appending any graphql fragment
116+
* resources provided and sends it to the GraphQL server.
117+
*
118+
* @param graphqlResource path to the classpath resource containing the GraphQL query
119+
* @param fragmentResources an ordered list of classpaths containing GraphQL fragment definitions.
120+
* @return GraphQLResponse containing the result of query execution
121+
* @throws IOException if the resource cannot be loaded from the classpath
122+
*/
123+
public GraphQLResponse postForResource(String graphqlResource, List<String> fragmentResources) throws IOException {
124+
return perform(graphqlResource, null, fragmentResources);
125+
}
126+
107127
public GraphQLResponse postMultipart(String query, String variables) {
108128
return postRequest(RequestFactory.forMultipart(query, variables, headers));
109129
}
@@ -117,4 +137,4 @@ private GraphQLResponse postRequest(HttpEntity<Object> request) {
117137
return new GraphQLResponse(response);
118138
}
119139

120-
}
140+
}

0 commit comments

Comments
 (0)