11package graphql .kickstart .spring .webclient .boot ;
22
33import com .fasterxml .jackson .databind .ObjectMapper ;
4- import java .util .List ;
54import java .util .Map ;
65import lombok .RequiredArgsConstructor ;
7- import lombok .SneakyThrows ;
86import lombok .extern .slf4j .Slf4j ;
97import org .springframework .http .MediaType ;
108import org .springframework .web .reactive .function .client .WebClient ;
@@ -25,23 +23,22 @@ public <T> Mono<T> post(String resource, Class<T> returnType) {
2523
2624 @ Override
2725 public <T > Mono <T > post (String resource , Map <String , Object > variables , Class <T > returnType ) {
28- return execute (resource , variables ).map (it -> readValue (it , returnType ));
29- }
30-
31- @ SneakyThrows
32- private <T > T readValue (Object value , Class <T > returnType ) {
33- log .trace ("Read value: {}" , value );
34- return objectMapper .convertValue (value , returnType );
26+ return post (resource , variables )
27+ .flatMap (it -> {
28+ it .validateNoErrors ();
29+ return Mono .justOrEmpty (it .getFirst (returnType ));
30+ });
3531 }
3632
3733 @ Override
38- public Mono <GraphQLResponse > post (GraphQLRequest <?> request ) {
39- WebClient .RequestBodySpec spec = webClient .post ()
40- . contentType ( MediaType . APPLICATION_JSON );
41- request . getHeaders () .forEach ((header , values ) -> spec .header (header , values .toArray (new String [0 ])));
34+ public Mono <GraphQLResponse > post (GraphQLRequest request ) {
35+ WebClient .RequestBodySpec spec = webClient .post (). contentType ( MediaType . APPLICATION_JSON );
36+ request . getHeaders ()
37+ .forEach ((header , values ) -> spec .header (header , values .toArray (new String [0 ])));
4238 return spec .bodyValue (request .getRequestBody ())
43- .retrieve ()
44- .bodyToMono (GraphQLResponse .class );
39+ .retrieve ()
40+ .bodyToMono (String .class )
41+ .map (it -> new GraphQLResponse (it , objectMapper ));
4542 }
4643
4744 @ Override
@@ -52,38 +49,17 @@ public <T> Flux<T> flux(String resource, Class<T> returnType) {
5249 @ Override
5350 @ SuppressWarnings ("unchecked" )
5451 public <T > Flux <T > flux (String resource , Map <String , Object > variables , Class <T > returnType ) {
55- Mono <Object > responseObject = execute (resource , variables );
56-
57- return responseObject .map (List .class ::cast )
58- .flatMapMany (Flux ::fromIterable )
59- .map (it -> readValue (it , returnType ));
60- }
61-
62- @ Override
63- @ SuppressWarnings ("unchecked" )
64- public <T > Flux <T > flux (GraphQLRequest <T > request ) {
65- Mono <Object > responseObject = execute (request );
66- return responseObject .map (List .class ::cast )
67- .flatMapMany (Flux ::fromIterable )
68- .map (it -> readValue (it , request .getReturnType ()));
52+ return post (resource , variables )
53+ .map (it -> it .getFirstList (returnType ))
54+ .flatMapMany (Flux ::fromIterable );
6955 }
7056
71- private Mono <Object > execute (String resource , Map <String , Object > variables ) {
72- GraphQLRequest <?> request = GraphQLRequest .builder (Object . class )
57+ private Mono <GraphQLResponse > post (String resource , Map <String , Object > variables ) {
58+ GraphQLRequest request = GraphQLRequest .builder ()
7359 .resource (resource )
7460 .variables (variables )
7561 .build ();
76- return execute (request );
77- }
78-
79- private Mono <Object > execute (GraphQLRequest <?> request ) {
80- WebClient .RequestBodySpec spec = webClient .post ()
81- .contentType (MediaType .APPLICATION_JSON );
82- request .getHeaders ().forEach ((header , values ) -> spec .header (header , values .toArray (new String [0 ])));
83- return spec .bodyValue (request .getRequestBody ())
84- .retrieve ()
85- .bodyToMono (GraphQLResponse .class )
86- .flatMap (it -> Mono .justOrEmpty (it .getFirstObject ()));
62+ return post (request );
8763 }
8864
8965}
0 commit comments