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

Commit 4c7cdba

Browse files
authored
Merge pull request #344 from punchcafe/master
add support for testing with graphql fragments
2 parents 75682d8 + 4f5d7d2 commit 4c7cdba

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.IOException;
1818
import java.io.InputStream;
1919
import java.nio.charset.StandardCharsets;
20+
import java.util.List;
2021

2122
public class GraphQLTestTemplate {
2223

@@ -93,6 +94,16 @@ public GraphQLResponse perform(String graphqlResource, ObjectNode variables) thr
9394
return post(payload);
9495
}
9596

97+
public GraphQLResponse perform(String graphqlResource, ObjectNode variables, List<String> fragmentResources) throws IOException {
98+
StringBuilder sb = new StringBuilder();
99+
for (String fragmentResource : fragmentResources) {
100+
sb.append(loadQuery(fragmentResource));
101+
}
102+
String graphql = sb.append(loadQuery(graphqlResource)).toString();
103+
String payload = createJsonQuery(graphql, variables);
104+
return post(payload);
105+
}
106+
96107
/**
97108
* Loads a GraphQL query from the given classpath resource and sends it to the GraphQL server.
98109
*
@@ -104,6 +115,19 @@ public GraphQLResponse postForResource(String graphqlResource) throws IOExceptio
104115
return perform(graphqlResource, null);
105116
}
106117

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

120-
}
144+
}

0 commit comments

Comments
 (0)