From 1ea64dca5cfadbb289dfee6dc871b2018426bf65 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 26 Apr 2024 12:00:37 +0100 Subject: [PATCH] Add protected encode method to WebFlux AbstractGraphQlHttpHandler This helps to encode directly when needed, e.g. for multipart stream with incremental delivery. See gh-959 --- .../webflux/AbstractGraphQlHttpHandler.java | 18 +++++++++++++++--- .../server/webflux/HttpCodecDelegate.java | 9 ++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/server/webflux/AbstractGraphQlHttpHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/server/webflux/AbstractGraphQlHttpHandler.java index 0fbe2560..60449af4 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/server/webflux/AbstractGraphQlHttpHandler.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/server/webflux/AbstractGraphQlHttpHandler.java @@ -18,6 +18,7 @@ package org.springframework.graphql.server.webflux; import java.util.List; +import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -131,13 +132,24 @@ public abstract class AbstractGraphQlHttpHandler { protected abstract Mono prepareResponse(ServerRequest request, WebGraphQlResponse response); /** - * Encode the GraphQL response if custom codecs were provided, or otherwise - * return the result map. + * Encode the GraphQL response if custom codecs were provided, or return the result map. * @param response the GraphQL response * @return the encoded response or the result map */ protected Object encodeResponseIfNecessary(WebGraphQlResponse response) { - return (this.codecDelegate != null) ? this.codecDelegate.encode(response) : response.toMap(); + Map resultMap = response.toMap(); + return (this.codecDelegate != null) ? encode(resultMap) : resultMap; + } + + /** + * Encode the result map. + *

This method assumes that a {@link CodecConfigurer} has been provided. + * @param resultMap the result to encode + * @return the encoded result map + */ + protected DataBuffer encode(Map resultMap) { + Assert.state(this.codecDelegate != null, "CodecConfigurer was not provided"); + return this.codecDelegate.encode(resultMap); } } diff --git a/spring-graphql/src/main/java/org/springframework/graphql/server/webflux/HttpCodecDelegate.java b/spring-graphql/src/main/java/org/springframework/graphql/server/webflux/HttpCodecDelegate.java index 9d588efc..e3589a5c 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/server/webflux/HttpCodecDelegate.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/server/webflux/HttpCodecDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import org.springframework.core.codec.Decoder; import org.springframework.core.codec.Encoder; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DefaultDataBufferFactory; -import org.springframework.graphql.GraphQlResponse; import org.springframework.graphql.server.support.SerializableGraphQlRequest; import org.springframework.http.MediaType; import org.springframework.http.codec.CodecConfigurer; @@ -77,9 +76,9 @@ final class HttpCodecDelegate { @SuppressWarnings("unchecked") - DataBuffer encode(GraphQlResponse response) { - return ((Encoder>) this.encoder) - .encodeValue(response.toMap(), DefaultDataBufferFactory.sharedInstance, RESPONSE_TYPE, MimeTypeUtils.APPLICATION_JSON, null); + DataBuffer encode(Map resultMap) { + return ((Encoder>) this.encoder).encodeValue( + resultMap, DefaultDataBufferFactory.sharedInstance, RESPONSE_TYPE, MimeTypeUtils.APPLICATION_JSON, null); } @SuppressWarnings("unchecked")