Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fragment AllCommentFields on Comment {
id
description
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query {
post(id: "1") {
id
comments {
...AllCommentFields
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class GraphQLTestTemplate {

Expand Down Expand Up @@ -93,6 +94,16 @@ public GraphQLResponse perform(String graphqlResource, ObjectNode variables) thr
return post(payload);
}

public GraphQLResponse perform(String graphqlResource, ObjectNode variables, List<String> 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.
*
Expand All @@ -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<String> fragmentResources) throws IOException {
return perform(graphqlResource, null, fragmentResources);
}

public GraphQLResponse postMultipart(String query, String variables) {
return postRequest(RequestFactory.forMultipart(query, variables, headers));
}
Expand All @@ -117,4 +141,4 @@ private GraphQLResponse postRequest(HttpEntity<Object> request) {
return new GraphQLResponse(response);
}

}
}