44import java .io .IOException ;
55import java .io .InputStream ;
66import java .nio .charset .StandardCharsets ;
7+ import java .util .List ;
78import java .util .Map ;
89import lombok .RequiredArgsConstructor ;
910import lombok .SneakyThrows ;
1314import org .springframework .http .MediaType ;
1415import org .springframework .util .StreamUtils ;
1516import org .springframework .web .reactive .function .client .WebClient ;
17+ import reactor .core .publisher .Flux ;
1618import reactor .core .publisher .Mono ;
1719
1820@ Slf4j
@@ -29,23 +31,12 @@ public <T> Mono<T> post(String resource, Class<T> returnType) {
2931
3032 @ Override
3133 public <T > Mono <T > post (String resource , Map <String , Object > variables , Class <T > returnType ) {
32- GraphQLRequest request = GraphQLRequest .builder ()
33- .query (loadQuery (resource ))
34- .variables (variables )
35- .build ();
36-
37- return webClient .post ()
38- .contentType (MediaType .APPLICATION_JSON )
39- .bodyValue (request )
40- .retrieve ()
41- .bodyToMono (GraphQLResponse .class )
42- .flatMap (it -> Mono .justOrEmpty (it .getFirstObject ()))
43- .map (it -> readValue (it , returnType ));
34+ return execute (resource , variables ).map (it -> readValue (it , returnType ));
4435 }
4536
4637 @ SneakyThrows
4738 private <T > T readValue (Object value , Class <T > returnType ) {
48- log .debug ("Read value: '{}' " , value );
39+ log .trace ("Read value: {} " , value );
4940 return objectMapper .convertValue (value , returnType );
5041 }
5142
@@ -60,4 +51,33 @@ private String loadResource(Resource resource) throws IOException {
6051 }
6152 }
6253
54+ @ Override
55+ public <T > Flux <T > flux (String resource , Class <T > returnType ) {
56+ return flux (resource , null , returnType );
57+ }
58+
59+ @ Override
60+ @ SuppressWarnings ("unchecked" )
61+ public <T > Flux <T > flux (String resource , Map <String , Object > variables , Class <T > returnType ) {
62+ Mono <Object > responseObject = execute (resource , variables );
63+
64+ return responseObject .map (List .class ::cast )
65+ .flatMapMany (Flux ::fromIterable )
66+ .map (it -> readValue (it , returnType ));
67+ }
68+
69+ private Mono <Object > execute (String resource , Map <String , Object > variables ) {
70+ GraphQLRequest request = GraphQLRequest .builder ()
71+ .query (loadQuery (resource ))
72+ .variables (variables )
73+ .build ();
74+
75+ return webClient .post ()
76+ .contentType (MediaType .APPLICATION_JSON )
77+ .bodyValue (request )
78+ .retrieve ()
79+ .bodyToMono (GraphQLResponse .class )
80+ .flatMap (it -> Mono .justOrEmpty (it .getFirstObject ()));
81+ }
82+
6383}
0 commit comments