diff --git a/example-graphql-tools/src/test/java/com/graphql/sample/boot/GraphQLToolsSampleApplicationTest.java b/example-graphql-tools/src/test/java/com/graphql/sample/boot/GraphQLToolsSampleApplicationTest.java index bf8c134b..5ca07c00 100644 --- a/example-graphql-tools/src/test/java/com/graphql/sample/boot/GraphQLToolsSampleApplicationTest.java +++ b/example-graphql-tools/src/test/java/com/graphql/sample/boot/GraphQLToolsSampleApplicationTest.java @@ -17,6 +17,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.List; + @RunWith(SpringRunner.class) @GraphQLTest public class GraphQLToolsSampleApplicationTest { @@ -33,6 +36,16 @@ public void get_comments() throws IOException { assertEquals("1", response.get("$.data.post.id")); } + @Test + public void get_comments_withFragments() throws IOException { + List fragments = new ArrayList<>(); + fragments.add("graphql/all-comment-fields-fragment.graphql"); + GraphQLResponse response = graphQLTestTemplate.postForResource("graphql/post-get-comments-with-fragment.graphql", fragments); + assertNotNull(response); + assertTrue(response.isOk()); + assertEquals("1", response.get("$.data.post.id")); + } + @Test public void create_post() throws IOException { ObjectNode variables = new ObjectMapper().createObjectNode(); diff --git a/example-graphql-tools/src/test/resources/graphql/all-comment-fields-fragment.graphql b/example-graphql-tools/src/test/resources/graphql/all-comment-fields-fragment.graphql new file mode 100644 index 00000000..202dcff1 --- /dev/null +++ b/example-graphql-tools/src/test/resources/graphql/all-comment-fields-fragment.graphql @@ -0,0 +1,4 @@ +fragment AllCommentFields on Comment { + id + description +} \ No newline at end of file diff --git a/example-graphql-tools/src/test/resources/graphql/post-get-comments-with-fragment.graphql b/example-graphql-tools/src/test/resources/graphql/post-get-comments-with-fragment.graphql new file mode 100644 index 00000000..90e18ca5 --- /dev/null +++ b/example-graphql-tools/src/test/resources/graphql/post-get-comments-with-fragment.graphql @@ -0,0 +1,8 @@ +query { + post(id: "1") { + id + comments { + ...AllCommentFields + } + } +} diff --git a/graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestTemplate.java b/graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestTemplate.java index 972d7ec4..69b0058a 100644 --- a/graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestTemplate.java +++ b/graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestTemplate.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.util.List; public class GraphQLTestTemplate { @@ -93,6 +94,16 @@ public GraphQLResponse perform(String graphqlResource, ObjectNode variables) thr return post(payload); } + public GraphQLResponse perform(String graphqlResource, ObjectNode variables, List fragmentResources) throws IOException { + StringBuilder sb = new StringBuilder(); + for (String fragmentResource : fragmentResources) { + sb.append(loadQuery(fragmentResource)); + } + String graphql = sb.append(loadQuery(graphqlResource)).toString(); + String payload = createJsonQuery(graphql, variables); + return post(payload); + } + /** * Loads a GraphQL query from the given classpath resource and sends it to the GraphQL server. * @@ -104,6 +115,19 @@ public GraphQLResponse postForResource(String graphqlResource) throws IOExceptio return perform(graphqlResource, null); } + /** + * Loads a GraphQL query from the given classpath resource, appending any graphql fragment + * resources provided and sends it to the GraphQL server. + * + * @param graphqlResource path to the classpath resource containing the GraphQL query + * @param fragmentResources an ordered list of classpaths containing GraphQL fragment definitions. + * @return GraphQLResponse containing the result of query execution + * @throws IOException if the resource cannot be loaded from the classpath + */ + public GraphQLResponse postForResource(String graphqlResource, List fragmentResources) throws IOException { + return perform(graphqlResource, null, fragmentResources); + } + public GraphQLResponse postMultipart(String query, String variables) { return postRequest(RequestFactory.forMultipart(query, variables, headers)); } @@ -117,4 +141,4 @@ private GraphQLResponse postRequest(HttpEntity request) { return new GraphQLResponse(response); } -} +} \ No newline at end of file