From 8cfd508e26dec106cf71cfe25206a1b9db2be39d Mon Sep 17 00:00:00 2001 From: Luca Fruzza Date: Fri, 14 Feb 2020 10:56:33 +0000 Subject: [PATCH 1/2] :sparkles::white_check_mark: add support for testing with graphql fragments --- .../GraphQLToolsSampleApplicationTest.java | 13 ++++++ .../all-comment-fields-fragment.graphql | 4 ++ .../post-get-comments-with-fragment.graphql | 8 ++++ .../spring/boot/test/GraphQLTestTemplate.java | 44 ++++++++++++++----- 4 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 example-graphql-tools/src/test/resources/graphql/all-comment-fields-fragment.graphql create mode 100644 example-graphql-tools/src/test/resources/graphql/post-get-comments-with-fragment.graphql 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..35566ffc 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 @@ -16,22 +16,20 @@ import java.io.IOException; import java.io.InputStream; + import java.nio.charset.StandardCharsets; +import java.util.List; public class GraphQLTestTemplate { - @Autowired - private ResourceLoader resourceLoader; - @Autowired(required = false) - private TestRestTemplate restTemplate; - @Value("${graphql.servlet.mapping:/graphql}") - private String graphqlMapping; + @Autowired private ResourceLoader resourceLoader; + @Autowired(required = false) private TestRestTemplate restTemplate; + @Value("${graphql.servlet.mapping:/graphql}") private String graphqlMapping; private ObjectMapper objectMapper = new ObjectMapper(); private HttpHeaders headers = new HttpHeaders(); - private String createJsonQuery(String graphql, ObjectNode variables) - throws JsonProcessingException { + private String createJsonQuery(String graphql, ObjectNode variables) throws JsonProcessingException { ObjectNode wrapper = objectMapper.createObjectNode(); wrapper.put("query", graphql); @@ -53,7 +51,7 @@ private String loadResource(Resource resource) throws IOException { /** * Add an HTTP header that will be sent with each request this sends. * - * @param name Name (key) of HTTP header to add. + * @param name Name (key) of HTTP header to add. * @param value Value of HTTP header to add. */ public void addHeader(String name, String value) { @@ -77,11 +75,10 @@ public void clearHeaders() { } /** - * @deprecated Use {@link #postForResource(String)} instead - * * @param graphqlResource path to the classpath resource containing the GraphQL query * @return GraphQLResponse containing the result of query execution * @throws IOException if the resource cannot be loaded from the classpath + * @deprecated Use {@link #postForResource(String)} instead */ public GraphQLResponse perform(String graphqlResource) throws IOException { return postForResource(graphqlResource); @@ -93,6 +90,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 +111,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 +137,4 @@ private GraphQLResponse postRequest(HttpEntity request) { return new GraphQLResponse(response); } -} +} \ No newline at end of file From 4f5d7d2c72f080324b6617e75c234117b6f39883 Mon Sep 17 00:00:00 2001 From: Luca Fruzza Date: Fri, 14 Feb 2020 11:01:25 +0000 Subject: [PATCH 2/2] :art: revert linting changes --- .../spring/boot/test/GraphQLTestTemplate.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) 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 35566ffc..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 @@ -16,20 +16,23 @@ import java.io.IOException; import java.io.InputStream; - import java.nio.charset.StandardCharsets; import java.util.List; public class GraphQLTestTemplate { - @Autowired private ResourceLoader resourceLoader; - @Autowired(required = false) private TestRestTemplate restTemplate; - @Value("${graphql.servlet.mapping:/graphql}") private String graphqlMapping; + @Autowired + private ResourceLoader resourceLoader; + @Autowired(required = false) + private TestRestTemplate restTemplate; + @Value("${graphql.servlet.mapping:/graphql}") + private String graphqlMapping; private ObjectMapper objectMapper = new ObjectMapper(); private HttpHeaders headers = new HttpHeaders(); - private String createJsonQuery(String graphql, ObjectNode variables) throws JsonProcessingException { + private String createJsonQuery(String graphql, ObjectNode variables) + throws JsonProcessingException { ObjectNode wrapper = objectMapper.createObjectNode(); wrapper.put("query", graphql); @@ -51,7 +54,7 @@ private String loadResource(Resource resource) throws IOException { /** * Add an HTTP header that will be sent with each request this sends. * - * @param name Name (key) of HTTP header to add. + * @param name Name (key) of HTTP header to add. * @param value Value of HTTP header to add. */ public void addHeader(String name, String value) { @@ -75,10 +78,11 @@ public void clearHeaders() { } /** + * @deprecated Use {@link #postForResource(String)} instead + * * @param graphqlResource path to the classpath resource containing the GraphQL query * @return GraphQLResponse containing the result of query execution * @throws IOException if the resource cannot be loaded from the classpath - * @deprecated Use {@link #postForResource(String)} instead */ public GraphQLResponse perform(String graphqlResource) throws IOException { return postForResource(graphqlResource);