1
1
package io .leangen .graphql .spqr .spring .web ;
2
2
3
- import graphql .GraphQL ;
4
- import io .leangen .graphql .spqr .spring .web .dto .ExecutorParams ;
5
- import io .leangen .graphql .spqr .spring .web .dto .GraphQLRequest ;
6
- import io .leangen .graphql .spqr .spring .web .dto .TransportType ;
7
- import io .leangen .graphql .util .Utils ;
3
+ import java .io .IOException ;
4
+ import java .util .Collections ;
5
+ import java .util .Map ;
6
+
8
7
import org .springframework .http .MediaType ;
9
8
import org .springframework .web .bind .annotation .GetMapping ;
10
9
import org .springframework .web .bind .annotation .PostMapping ;
11
10
import org .springframework .web .bind .annotation .RequestBody ;
12
- import org .springframework .web .bind .annotation .RequestMapping ;
13
- import org .springframework .web .bind .annotation .RequestMethod ;
14
11
import org .springframework .web .bind .annotation .RequestParam ;
15
12
import org .springframework .web .bind .annotation .RestController ;
16
13
17
- import java .util .Map ;
14
+ import com .fasterxml .jackson .core .type .TypeReference ;
15
+ import com .fasterxml .jackson .databind .ObjectMapper ;
16
+
17
+ import graphql .GraphQL ;
18
+ import io .leangen .graphql .spqr .spring .web .dto .ExecutorParams ;
19
+ import io .leangen .graphql .spqr .spring .web .dto .GraphQLRequest ;
20
+ import io .leangen .graphql .spqr .spring .web .dto .TransportType ;
21
+ import io .leangen .graphql .util .Utils ;
18
22
19
23
@ RestController
20
24
public abstract class GraphQLController <R > {
21
25
22
26
protected final GraphQL graphQL ;
23
27
protected final GraphQLExecutor <R > executor ;
28
+ protected final ObjectMapper objectMapper ;
24
29
25
- public GraphQLController (GraphQL graphQL , GraphQLExecutor <R > executor ) {
30
+ public GraphQLController (GraphQL graphQL , GraphQLExecutor <R > executor , ObjectMapper objectMapper ) {
26
31
this .graphQL = graphQL ;
27
32
this .executor = executor ;
33
+ this .objectMapper = objectMapper ;
28
34
}
29
35
30
36
@ PostMapping (
@@ -53,7 +59,8 @@ public Object jsonPost(GraphQLRequest requestBody, GraphQLRequest requestParams,
53
59
String query = Utils .isNotEmpty (requestParams .getQuery ()) ? requestParams .getQuery () : requestBody .getQuery ();
54
60
String operationName = Utils .isNotEmpty (requestParams .getOperationName ()) ? requestParams .getOperationName () : requestBody .getOperationName ();
55
61
Map <String , Object > variables = requestParams .getVariables ().isEmpty () ? requestBody .getVariables () : requestParams .getVariables ();
56
- ExecutorParams <R > params = new ExecutorParams <>(new GraphQLRequest (id , query , operationName , variables ), request , transportType );
62
+ Map <String , Object > extensions = requestParams .getExtensions ().isEmpty () ? requestBody .getExtensions () : requestParams .getExtensions ();
63
+ ExecutorParams <R > params = new ExecutorParams <>(new GraphQLRequest (id , query , operationName , variables , extensions ), request , transportType );
57
64
return executor .execute (graphQL , params );
58
65
}
59
66
@@ -66,13 +73,12 @@ public Object executeGraphQLPost(@RequestBody String queryBody,
66
73
GraphQLRequest originalReq ,
67
74
R request ) {
68
75
String query = Utils .isNotEmpty (originalReq .getQuery ()) ? originalReq .getQuery () : queryBody ;
69
- GraphQLRequest remappedReq = new GraphQLRequest (originalReq .getId (), query , originalReq .getOperationName (), originalReq .getVariables ());
76
+ GraphQLRequest remappedReq = new GraphQLRequest (originalReq .getId (), query , originalReq .getOperationName (), originalReq .getVariables (), originalReq . getExtensions () );
70
77
ExecutorParams <R > params = new ExecutorParams <>(remappedReq , request , TransportType .HTTP );
71
78
return executor .execute (graphQL , params );
72
79
}
73
80
74
- @ RequestMapping (
75
- method = RequestMethod .POST ,
81
+ @ PostMapping (
76
82
value = "${graphql.spqr.http.endpoint:/graphql}" ,
77
83
consumes = {MediaType .APPLICATION_FORM_URLENCODED_VALUE , "application/x-www-form-urlencoded;charset=UTF-8" },
78
84
produces = MediaType .APPLICATION_JSON_VALUE
@@ -88,7 +94,7 @@ public Object executeFormPost(@RequestParam Map<String, String> queryParams,
88
94
String id = Utils .isNotEmpty (idParam ) ? idParam : graphQLRequest .getId ();
89
95
String query = Utils .isNotEmpty (queryParam ) ? queryParam : graphQLRequest .getQuery ();
90
96
String operationName = Utils .isEmpty (operationNameParam ) ? graphQLRequest .getOperationName () : operationNameParam ;
91
- ExecutorParams <R > params = new ExecutorParams <>(new GraphQLRequest (id , query , operationName , graphQLRequest .getVariables ()), request , TransportType .HTTP );
97
+ ExecutorParams <R > params = new ExecutorParams <>(new GraphQLRequest (id , query , operationName , graphQLRequest .getVariables (), graphQLRequest . getExtensions () ), request , TransportType .HTTP );
92
98
93
99
return executor .execute (graphQL , params );
94
100
}
@@ -98,20 +104,41 @@ public Object executeFormPost(@RequestParam Map<String, String> queryParams,
98
104
produces = MediaType .APPLICATION_JSON_VALUE ,
99
105
headers = { "Connection!=Upgrade" , "Connection!=keep-alive, Upgrade" }
100
106
)
101
- public Object executeGet (GraphQLRequest graphQLRequest , R request ) {
102
- return get (graphQLRequest , request , TransportType .HTTP );
107
+ public Object executeGet (String id ,
108
+ String query ,
109
+ String operationName ,
110
+ String variables ,
111
+ String extensions ,
112
+ R request ) {
113
+ return get (new GraphQLRequest (id , query , operationName , parseAsMap (variables ), parseAsMap (extensions )), request , TransportType .HTTP );
114
+ }
115
+
116
+ private Object get (GraphQLRequest graphQLRequest , R request , TransportType transportType ) {
117
+ return executor .execute (graphQL , new ExecutorParams <>(graphQLRequest , request , transportType ));
103
118
}
104
119
105
120
@ GetMapping (
106
121
value = "${graphql.spqr.http.endpoint:/graphql}" ,
107
122
produces = MediaType .TEXT_EVENT_STREAM_VALUE ,
108
123
headers = { "Connection!=Upgrade" , "Connection!=keep-alive, Upgrade" }
109
124
)
110
- public Object executeGetEventStream (GraphQLRequest graphQLRequest , R request ) {
111
- return get (graphQLRequest , request , TransportType .HTTP_EVENT_STREAM );
125
+ public Object executeGetEventStream (String id ,
126
+ String query ,
127
+ String operationName ,
128
+ String variables ,
129
+ String extensions ,
130
+ R request ) {
131
+ return get (new GraphQLRequest (id , query , operationName , parseAsMap (variables ), parseAsMap (extensions )), request , TransportType .HTTP_EVENT_STREAM );
112
132
}
113
133
114
- private Object get (GraphQLRequest graphQLRequest , R request , TransportType transportType ) {
115
- return executor .execute (graphQL , new ExecutorParams <>(graphQLRequest , request , transportType ));
134
+ private Map <String , Object > parseAsMap (String str ) {
135
+ if (str == null || str .trim ().isEmpty ()) {
136
+ return Collections .emptyMap ();
137
+ }
138
+ try {
139
+ return objectMapper .readValue (str , new TypeReference <Map <String , Object >>() {});
140
+ } catch (IOException e ) {
141
+ throw new IllegalArgumentException ("failed to parse: " + str );
142
+ }
116
143
}
117
144
}
0 commit comments