dgs-framework
dgs-framework copied to clipboard
bug: [webflux] ServerRequest.bodyToMono returns empty
Expected behavior
ServerRequest.bodyToMono returns its body.
Actual behavior
ServeRequest.bodyToMono returns Mono.empty().
Steps to reproduce
- Use
graphql-dgs-example-java-webfluxexample. - Modify
MyContextBuilder.build()as below
public Mono<MyContext> build(@Nullable Map<String, ?> extensions, @Nullable HttpHeaders headers, @Nullable ServerRequest serverRequest) {
assert serverRequest != null;
return Mono.just(new MyContext())
.doOnNext(body -> System.out.println("before bodyToMono"))
.flatMap(c -> serverRequest.bodyToMono(String.class))
.doOnNext(System.out::println)
.doOnNext(body -> System.out.println("after bodyToMono"))
.map(body -> new MyContext());
}
- Run its server
- Request with
{ usingContext }query inlocalhost:8080/graphiql - See only
before bodyToMonois printed
Thanks for reporting!
The request body is already consumed in DefaultDgsWebfluxHttpHandler by the time your context builder is run, so the bodyToMono call is returning an empty Mono. I think this is expected behavior, as it's necessary for the body to get consumed by the HTTP handler, and unfortunately I don't think there is much that DGS itself can do about it. I think you'd need to implement a WebFilter and cache the request somehow.