1616
1717import java .io .IOException ;
1818import java .io .InputStream ;
19+
1920import java .nio .charset .StandardCharsets ;
21+ import java .util .List ;
2022
2123public 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