1
1
package com .graphql .spring .boot .test ;
2
2
3
+ import static java .util .Objects .isNull ;
3
4
import static java .util .Objects .nonNull ;
5
+ import static java .util .Objects .requireNonNull ;
4
6
5
7
import com .fasterxml .jackson .core .JsonProcessingException ;
6
8
import com .fasterxml .jackson .databind .ObjectMapper ;
12
14
import java .util .Arrays ;
13
15
import java .util .Collections ;
14
16
import java .util .List ;
17
+ import java .util .Map ;
15
18
import java .util .function .IntFunction ;
19
+ import java .util .regex .Matcher ;
20
+ import java .util .regex .Pattern ;
16
21
import lombok .Getter ;
17
22
import lombok .NonNull ;
18
23
import org .springframework .beans .factory .annotation .Value ;
32
37
/** Helper class to test GraphQL queries and mutations. */
33
38
public class GraphQLTestTemplate {
34
39
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
+
35
45
private final ResourceLoader resourceLoader ;
36
46
private final TestRestTemplate restTemplate ;
37
47
private final String graphqlMapping ;
@@ -57,7 +67,9 @@ private String createJsonQuery(String graphql, String operation, ObjectNode vari
57
67
if (nonNull (operation )) {
58
68
wrapper .put ("operationName" , operation );
59
69
}
60
- wrapper .set ("variables" , variables );
70
+ if (nonNull (variables )) {
71
+ wrapper .set ("variables" , variables );
72
+ }
61
73
return objectMapper .writeValueAsString (wrapper );
62
74
}
63
75
@@ -72,6 +84,16 @@ private String loadResource(Resource resource) throws IOException {
72
84
}
73
85
}
74
86
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
+
75
97
/**
76
98
* Add an HTTP header that will be sent with each request this sends.
77
99
*
@@ -411,6 +433,95 @@ public GraphQLResponse postFiles(
411
433
return postRequest (RequestFactory .forMultipart (values , headers ));
412
434
}
413
435
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
+
414
525
/**
415
526
* Performs a GraphQL request with the provided payload.
416
527
*
0 commit comments