Skip to content

Commit 36d76a4

Browse files
committed
Merge branch '1.3.x'
2 parents 8dce170 + 3360e4a commit 36d76a4

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

spring-graphql/src/main/java/org/springframework/graphql/server/webflux/GraphQlWebSocketHandler.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,8 @@
2828
import java.util.concurrent.atomic.AtomicReference;
2929

3030
import graphql.ExecutionResult;
31+
import graphql.GraphQLError;
32+
import graphql.GraphqlErrorBuilder;
3133
import org.apache.commons.logging.Log;
3234
import org.apache.commons.logging.LogFactory;
3335
import org.reactivestreams.Publisher;
@@ -36,6 +38,8 @@
3638
import reactor.core.publisher.Mono;
3739

3840
import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
41+
import org.springframework.graphql.execution.ErrorType;
42+
import org.springframework.graphql.execution.SubscriptionPublisherException;
3943
import org.springframework.graphql.server.WebGraphQlHandler;
4044
import org.springframework.graphql.server.WebGraphQlResponse;
4145
import org.springframework.graphql.server.WebSocketGraphQlInterceptor;
@@ -256,7 +260,20 @@ private Flux<WebSocketMessage> handleResponse(WebSocketSession session, String i
256260
CloseStatus status = new CloseStatus(4409, "Subscriber for " + id + " already exists");
257261
return GraphQlStatus.close(session, status);
258262
}
259-
return Mono.fromCallable(() -> this.codecDelegate.encodeError(session, id, ex));
263+
List<GraphQLError> errors;
264+
if (ex instanceof SubscriptionPublisherException subscriptionEx) {
265+
errors = subscriptionEx.getErrors();
266+
}
267+
else {
268+
if (logger.isErrorEnabled()) {
269+
logger.error("Unresolved " + ex.getClass().getSimpleName() + " for request id " + id, ex);
270+
}
271+
errors = Collections.singletonList(GraphqlErrorBuilder.newError()
272+
.message("Subscription error")
273+
.errorType(ErrorType.INTERNAL_ERROR)
274+
.build());
275+
}
276+
return Mono.fromCallable(() -> this.codecDelegate.encodeError(session, id, errors));
260277
});
261278
}
262279

spring-graphql/src/main/java/org/springframework/graphql/server/webflux/WebSocketCodecDelegate.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,20 +16,16 @@
1616

1717
package org.springframework.graphql.server.webflux;
1818

19-
import java.util.Collections;
2019
import java.util.List;
2120
import java.util.Map;
2221

2322
import graphql.GraphQLError;
24-
import graphql.GraphqlErrorBuilder;
2523

2624
import org.springframework.core.ResolvableType;
2725
import org.springframework.core.codec.Decoder;
2826
import org.springframework.core.codec.Encoder;
2927
import org.springframework.core.io.buffer.DataBuffer;
3028
import org.springframework.core.io.buffer.DataBufferUtils;
31-
import org.springframework.graphql.execution.ErrorType;
32-
import org.springframework.graphql.execution.SubscriptionPublisherException;
3329
import org.springframework.graphql.server.support.GraphQlWebSocketMessage;
3430
import org.springframework.http.MediaType;
3531
import org.springframework.http.codec.CodecConfigurer;
@@ -105,13 +101,7 @@ WebSocketMessage encodeNext(WebSocketSession session, String id, Map<String, Obj
105101
return encode(session, GraphQlWebSocketMessage.next(id, responseMap));
106102
}
107103

108-
WebSocketMessage encodeError(WebSocketSession session, String id, Throwable ex) {
109-
List<GraphQLError> errors = ((ex instanceof SubscriptionPublisherException) ?
110-
((SubscriptionPublisherException) ex).getErrors() :
111-
Collections.singletonList(GraphqlErrorBuilder.newError()
112-
.message("Subscription error")
113-
.errorType(ErrorType.INTERNAL_ERROR)
114-
.build()));
104+
WebSocketMessage encodeError(WebSocketSession session, String id, List<GraphQLError> errors) {
115105
return encode(session, GraphQlWebSocketMessage.error(id, errors));
116106
}
117107

spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlWebSocketHandler.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,19 @@ private Flux<TextMessage> handleResponse(WebSocketSession session, String id, We
347347
GraphQlStatus.closeSession(session, status);
348348
return Flux.empty();
349349
}
350-
List<GraphQLError> errors = ((ex instanceof SubscriptionPublisherException) ?
351-
((SubscriptionPublisherException) ex).getErrors() :
352-
Collections.singletonList(GraphqlErrorBuilder.newError()
353-
.message("Subscription error")
354-
.errorType(ErrorType.INTERNAL_ERROR)
355-
.build()));
350+
List<GraphQLError> errors;
351+
if (ex instanceof SubscriptionPublisherException subscriptionEx) {
352+
errors = subscriptionEx.getErrors();
353+
}
354+
else {
355+
if (logger.isErrorEnabled()) {
356+
logger.error("Unresolved " + ex.getClass().getSimpleName() + " for request id " + id, ex);
357+
}
358+
errors = Collections.singletonList(GraphqlErrorBuilder.newError()
359+
.message("Subscription error")
360+
.errorType(ErrorType.INTERNAL_ERROR)
361+
.build());
362+
}
356363
return Mono.just(encode(GraphQlWebSocketMessage.error(id, errors)));
357364
});
358365
}

0 commit comments

Comments
 (0)