11package com .graphql .spring .boot .test ;
22
3+ import static java .util .Objects .isNull ;
34import static java .util .Objects .nonNull ;
5+ import static java .util .Objects .requireNonNull ;
46
57import com .fasterxml .jackson .core .JsonProcessingException ;
68import com .fasterxml .jackson .databind .ObjectMapper ;
1214import java .util .Arrays ;
1315import java .util .Collections ;
1416import java .util .List ;
17+ import java .util .Map ;
1518import java .util .function .IntFunction ;
19+ import java .util .regex .Matcher ;
20+ import java .util .regex .Pattern ;
1621import lombok .Getter ;
1722import lombok .NonNull ;
1823import org .springframework .beans .factory .annotation .Value ;
3237/** Helper class to test GraphQL queries and mutations. */
3338public class GraphQLTestTemplate {
3439
40+ private static final Pattern GRAPHQL_OP_NAME_PATTERN = Pattern .compile (
41+ "(query|mutation|subscription)\\ s+([a-z0-9]+)\\ s*[({]" ,
42+ (Pattern .CASE_INSENSITIVE | Pattern .MULTILINE )
43+ );
44+
3545 private final ResourceLoader resourceLoader ;
3646 private final TestRestTemplate restTemplate ;
3747 private final String graphqlMapping ;
@@ -57,7 +67,9 @@ private String createJsonQuery(String graphql, String operation, ObjectNode vari
5767 if (nonNull (operation )) {
5868 wrapper .put ("operationName" , operation );
5969 }
60- wrapper .set ("variables" , variables );
70+ if (nonNull (variables )) {
71+ wrapper .set ("variables" , variables );
72+ }
6173 return objectMapper .writeValueAsString (wrapper );
6274 }
6375
@@ -72,6 +84,16 @@ private String loadResource(Resource resource) throws IOException {
7284 }
7385 }
7486
87+ private String getOperationName (String graphql ) {
88+ if (isNull (graphql )) {
89+ return null ;
90+ }
91+
92+ Matcher matcher = GRAPHQL_OP_NAME_PATTERN .matcher (graphql );
93+
94+ return (matcher .find () ? matcher .group (2 ) : null );
95+ }
96+
7597 /**
7698 * Add an HTTP header that will be sent with each request this sends.
7799 *
@@ -411,6 +433,95 @@ public GraphQLResponse postFiles(
411433 return postRequest (RequestFactory .forMultipart (values , headers ));
412434 }
413435
436+ /**
437+ * Performs a GraphQL request using the provided GraphQL query string.
438+ *
439+ * Operation name will be derived from the provided GraphQL query string.
440+ *
441+ * @param graphql the GraphQL query
442+ * @return {@link GraphQLResponse} containing the result of the query execution
443+ * @throws IOException if the request json cannot be created because of issues with one of the
444+ * provided arguments
445+ */
446+ public GraphQLResponse postForString (String graphql ) throws IOException {
447+ return postForString (graphql , getOperationName (graphql ), ((ObjectNode ) null ));
448+ }
449+
450+ /**
451+ * Performs a GraphQL request using the provided GraphQL query string and operation name.
452+ *
453+ * @param graphql the GraphQL query
454+ * @param operation the name of the GraphQL operation to be executed
455+ * @return {@link GraphQLResponse} containing the result of the query execution
456+ * @throws IOException if the request json cannot be created because of issues with one of the
457+ * provided arguments
458+ */
459+ public GraphQLResponse postForString (String graphql , String operation ) throws IOException {
460+ return postForString (graphql , operation , ((ObjectNode ) null ));
461+ }
462+
463+ /**
464+ * Performs a GraphQL request using the provided GraphQL query string and variables.
465+ *
466+ * Operation name will be derived from the provided GraphQL query string.
467+ *
468+ * @param graphql the GraphQL query
469+ * @param variables the input variables for the GraphQL query
470+ * @return {@link GraphQLResponse} containing the result of the query execution
471+ * @throws IOException if the request json cannot be created because of issues with one of the
472+ * provided arguments
473+ */
474+ public GraphQLResponse postForString (String graphql , Map <String , ?> variables ) throws IOException {
475+ return postForString (graphql , getOperationName (graphql ), variables );
476+ }
477+
478+ /**
479+ * Performs a GraphQL request using the provided GraphQL query string, operation name, and
480+ * variables.
481+ *
482+ * @param graphql the GraphQL query
483+ * @param operation the name of the GraphQL operation to be executed
484+ * @param variables the input variables for the GraphQL query
485+ * @return {@link GraphQLResponse} containing the result of the query execution
486+ * @throws IOException if the request json cannot be created because of issues with one of the
487+ * provided arguments
488+ */
489+ public GraphQLResponse postForString (String graphql , String operation , Map <String , ?> variables ) throws IOException {
490+ return postForString (graphql , operation , ((ObjectNode ) new ObjectMapper ().valueToTree (variables )));
491+ }
492+
493+ /**
494+ * Performs a GraphQL request using the provided GraphQL query string and variables.
495+ *
496+ * Operation name will be derived from the provided GraphQL query string.
497+ *
498+ * @param graphql the GraphQL query
499+ * @param variables the input variables for the GraphQL query
500+ * @return {@link GraphQLResponse} containing the result of the query execution
501+ * @throws IOException if the request json cannot be created because of issues with one of the
502+ * provided arguments
503+ */
504+ public GraphQLResponse postForString (String graphql , ObjectNode variables ) throws IOException {
505+ return post (createJsonQuery (graphql , getOperationName (graphql ), variables ));
506+ }
507+
508+ /**
509+ * Performs a GraphQL request using the provided GraphQL query string, operation name, and
510+ * variables.
511+ *
512+ * @param graphql the GraphQL query
513+ * @param operation the name of the GraphQL operation to be executed
514+ * @param variables the input variables for the GraphQL query
515+ * @return {@link GraphQLResponse} containing the result of the query execution
516+ * @throws IOException if the request json cannot be created because of issues with one of the
517+ * provided arguments
518+ */
519+ public GraphQLResponse postForString (String graphql , String operation , ObjectNode variables ) throws IOException {
520+ requireNonNull (graphql , "GraphQL query string cannot be null" );
521+
522+ return post (createJsonQuery (graphql , operation , variables ));
523+ }
524+
414525 /**
415526 * Performs a GraphQL request with the provided payload.
416527 *
0 commit comments