Skip to content

Commit 1462050

Browse files
committed
Add RSocketGraphQlClient and GraphQlRSocketHandler
See gh-339
1 parent e71ea05 commit 1462050

File tree

12 files changed

+702
-18
lines changed

12 files changed

+702
-18
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ configure(moduleProjects) {
6363
mavenBom "org.springframework.data:spring-data-bom:2021.2.0-M4"
6464
mavenBom "org.springframework.security:spring-security-bom:5.7.0-M3"
6565
mavenBom "com.querydsl:querydsl-bom:5.0.0"
66+
mavenBom "io.rsocket:rsocket-bom:1.1.1"
6667
mavenBom "org.jetbrains.kotlin:kotlin-bom:1.5.32"
6768
mavenBom "org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.5.2"
6869
mavenBom "org.junit:junit-bom:5.8.1"

spring-graphql/build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dependencies {
1111
compileOnly 'org.springframework:spring-webflux'
1212
compileOnly 'org.springframework:spring-webmvc'
1313
compileOnly 'org.springframework:spring-websocket'
14+
compileOnly 'org.springframework:spring-messaging'
1415
compileOnly 'javax.servlet:javax.servlet-api'
1516
compileOnly 'javax.validation:validation-api'
1617

@@ -19,6 +20,9 @@ dependencies {
1920
compileOnly 'com.querydsl:querydsl-core'
2021
compileOnly 'org.springframework.data:spring-data-commons'
2122

23+
compileOnly 'io.rsocket:rsocket-core'
24+
compileOnly 'io.rsocket:rsocket-transport-netty'
25+
2226
compileOnly 'com.google.code.findbugs:jsr305'
2327
compileOnly 'org.jetbrains.kotlin:kotlin-stdlib'
2428
compileOnly 'org.jetbrains.kotlinx:kotlinx-coroutines-core'
@@ -29,10 +33,11 @@ dependencies {
2933
testImplementation 'org.assertj:assertj-core'
3034
testImplementation 'org.mockito:mockito-core'
3135
testImplementation 'io.projectreactor:reactor-test'
36+
testImplementation 'org.springframework:spring-messaging'
37+
testImplementation 'org.springframework:spring-test'
3238
testImplementation 'org.springframework:spring-webflux'
3339
testImplementation 'org.springframework:spring-webmvc'
3440
testImplementation 'org.springframework:spring-websocket'
35-
testImplementation 'org.springframework:spring-test'
3641
testImplementation 'org.springframework.data:spring-data-commons'
3742
testImplementation 'org.springframework.data:spring-data-keyvalue'
3843
testImplementation 'org.springframework.data:spring-data-jpa'
@@ -49,6 +54,7 @@ dependencies {
4954
testImplementation 'com.querydsl:querydsl-collections'
5055
testImplementation 'javax.servlet:javax.servlet-api'
5156
testImplementation 'com.squareup.okhttp3:mockwebserver:3.14.9'
57+
testImplementation 'io.rsocket:rsocket-transport-local'
5258
testImplementation 'javax.validation:validation-api'
5359
testImplementation 'com.jayway.jsonpath:json-path'
5460
testImplementation 'com.fasterxml.jackson.core:jackson-databind'

spring-graphql/src/main/java/org/springframework/graphql/client/AbstractGraphQlClientBuilder.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
*/
5151
public abstract class AbstractGraphQlClientBuilder<B extends AbstractGraphQlClientBuilder<B>> implements GraphQlClient.Builder<B> {
5252

53-
private static final boolean jackson2Present = ClassUtils.isPresent(
53+
protected static final boolean jackson2Present = ClassUtils.isPresent(
5454
"com.fasterxml.jackson.databind.ObjectMapper", AbstractGraphQlClientBuilder.class.getClassLoader());
5555

5656

@@ -111,6 +111,20 @@ protected void setJsonCodecs(Encoder<?> encoder, Decoder<?> decoder) {
111111
this.jsonDecoder = decoder;
112112
}
113113

114+
/**
115+
* Variant of {@link #setJsonCodecs} for setting each codec individually.
116+
*/
117+
protected void setJsonEncoder(Encoder<?> encoder) {
118+
this.jsonEncoder = encoder;
119+
}
120+
121+
/**
122+
* Variant of {@link #setJsonCodecs} for setting each codec individually.
123+
*/
124+
protected void setJsonDecoder(Decoder<?> decoder) {
125+
this.jsonDecoder = decoder;
126+
}
127+
114128
/**
115129
* Return the configured interceptors. For subclasses that look for a
116130
* transport specific interceptor extensions.
@@ -126,8 +140,8 @@ protected List<GraphQlClientInterceptor> getInterceptors() {
126140
protected GraphQlClient buildGraphQlClient(GraphQlTransport transport) {
127141

128142
if (jackson2Present) {
129-
this.jsonEncoder = (this.jsonEncoder == null ? Jackson2Configurer.encoder() : this.jsonEncoder);
130-
this.jsonDecoder = (this.jsonDecoder == null ? Jackson2Configurer.decoder() : this.jsonDecoder);
143+
this.jsonEncoder = (this.jsonEncoder == null ? DefaultJackson2Codecs.encoder() : this.jsonEncoder);
144+
this.jsonDecoder = (this.jsonDecoder == null ? DefaultJackson2Codecs.decoder() : this.jsonDecoder);
131145
}
132146

133147
return new DefaultGraphQlClient(
@@ -178,7 +192,7 @@ private Decoder<?> getDecoder() {
178192
}
179193

180194

181-
private static class Jackson2Configurer {
195+
protected static class DefaultJackson2Codecs {
182196

183197
static Encoder<?> encoder() {
184198
return new Jackson2JsonEncoder();

spring-graphql/src/main/java/org/springframework/graphql/client/CodecDelegate.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.graphql.client;
1717

18+
import java.util.List;
19+
import java.util.stream.Stream;
20+
1821
import org.springframework.core.ResolvableType;
1922
import org.springframework.core.codec.Decoder;
2023
import org.springframework.core.codec.Encoder;
@@ -32,8 +35,7 @@
3235
import org.springframework.web.reactive.socket.WebSocketSession;
3336

3437
/**
35-
* Delegate that can be embedded in a class to help with encoding and decoding
36-
* GraphQL over WebSocket messages.
38+
* Helper class for encoding and decoding GraphQL messages.
3739
*
3840
* @author Rossen Stoyanchev
3941
* @since 1.0.0
@@ -61,22 +63,40 @@ final class CodecDelegate {
6163
this.encoder = findJsonEncoder(configurer);
6264
}
6365

66+
static Encoder<?> findJsonEncoder(CodecConfigurer configurer) {
67+
return findJsonEncoder(configurer.getWriters().stream()
68+
.filter(writer -> writer instanceof EncoderHttpMessageWriter)
69+
.map(writer -> ((EncoderHttpMessageWriter<?>) writer).getEncoder()));
70+
}
71+
6472
static Decoder<?> findJsonDecoder(CodecConfigurer configurer) {
65-
return configurer.getReaders().stream()
66-
.filter((reader) -> reader.canRead(MESSAGE_TYPE, MediaType.APPLICATION_JSON))
67-
.map((reader) -> ((DecoderHttpMessageReader<?>) reader).getDecoder())
68-
.findFirst()
69-
.orElseThrow(() -> new IllegalArgumentException("No JSON Decoder"));
73+
return findJsonDecoder(configurer.getReaders().stream()
74+
.filter(reader -> reader instanceof DecoderHttpMessageReader)
75+
.map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()));
7076
}
7177

72-
static Encoder<?> findJsonEncoder(CodecConfigurer configurer) {
73-
return configurer.getWriters().stream()
74-
.filter((writer) -> writer.canWrite(MESSAGE_TYPE, MediaType.APPLICATION_JSON))
75-
.map((writer) -> ((EncoderHttpMessageWriter<?>) writer).getEncoder())
78+
static Encoder<?> findJsonEncoder(List<Encoder<?>> encoders) {
79+
return findJsonEncoder(encoders.stream());
80+
}
81+
82+
static Decoder<?> findJsonDecoder(List<Decoder<?>> decoders) {
83+
return findJsonDecoder(decoders.stream());
84+
}
85+
86+
private static Encoder<?> findJsonEncoder(Stream<Encoder<?>> stream) {
87+
return stream
88+
.filter(encoder -> encoder.canEncode(MESSAGE_TYPE, MediaType.APPLICATION_JSON))
7689
.findFirst()
7790
.orElseThrow(() -> new IllegalArgumentException("No JSON Encoder"));
7891
}
7992

93+
private static Decoder<?> findJsonDecoder(Stream<Decoder<?>> decoderStream) {
94+
return decoderStream
95+
.filter(decoder -> decoder.canDecode(MESSAGE_TYPE, MediaType.APPLICATION_JSON))
96+
.findFirst()
97+
.orElseThrow(() -> new IllegalArgumentException("No JSON Decoder"));
98+
}
99+
80100

81101
public CodecConfigurer getCodecConfigurer() {
82102
return this.codecConfigurer;

spring-graphql/src/main/java/org/springframework/graphql/client/DefaultHttpGraphQlClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public Builder webClient(Consumer<WebClient.Builder> configurer) {
130130
@Override
131131
public HttpGraphQlClient build() {
132132

133+
// Pass the codecs to the parent for response decoding
133134
this.webClientBuilder.codecs(configurer ->
134135
setJsonCodecs(
135136
CodecDelegate.findJsonEncoder(configurer),
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.client;
18+
19+
import java.net.URI;
20+
import java.util.function.Consumer;
21+
22+
import io.rsocket.transport.ClientTransport;
23+
import io.rsocket.transport.netty.client.TcpClientTransport;
24+
import io.rsocket.transport.netty.client.WebsocketClientTransport;
25+
26+
import org.springframework.lang.Nullable;
27+
import org.springframework.messaging.rsocket.RSocketRequester;
28+
import org.springframework.messaging.rsocket.RSocketStrategies;
29+
import org.springframework.util.Assert;
30+
import org.springframework.util.MimeType;
31+
32+
33+
/**
34+
* Default {@link RSocketGraphQlClient} implementation that builds the underlying
35+
* {@code RSocketGraphQlTransport} to use.
36+
*
37+
* @author Rossen Stoyanchev
38+
* @since 1.0.0
39+
*/
40+
final class DefaultRSocketGraphQlClient extends AbstractDelegatingGraphQlClient implements RSocketGraphQlClient {
41+
42+
private final RSocketRequester.Builder requesterBuilder;
43+
44+
private final ClientTransport clientTransport;
45+
46+
private final String route;
47+
48+
private final Consumer<AbstractGraphQlClientBuilder<?>> builderInitializer;
49+
50+
51+
DefaultRSocketGraphQlClient(
52+
GraphQlClient graphQlClient, RSocketRequester.Builder requesterBuilder,
53+
ClientTransport clientTransport, String route,
54+
Consumer<AbstractGraphQlClientBuilder<?>> builderInitializer) {
55+
56+
super(graphQlClient);
57+
58+
this.requesterBuilder = requesterBuilder;
59+
this.clientTransport = clientTransport;
60+
this.route = route;
61+
this.builderInitializer = builderInitializer;
62+
}
63+
64+
65+
@Override
66+
public RSocketGraphQlClient.Builder<?> mutate() {
67+
Builder builder = new Builder(this.requesterBuilder);
68+
builder.clientTransport(this.clientTransport);
69+
builder.route(this.route);
70+
this.builderInitializer.accept(builder);
71+
return builder;
72+
}
73+
74+
75+
/**
76+
* Default {@link RSocketGraphQlClient.Builder} implementation.
77+
*/
78+
static final class Builder extends AbstractGraphQlClientBuilder<Builder> implements RSocketGraphQlClient.Builder<Builder> {
79+
80+
private final RSocketRequester.Builder requesterBuilder;
81+
82+
@Nullable
83+
private ClientTransport clientTransport;
84+
85+
private String route;
86+
87+
Builder() {
88+
this(initRSocketRequestBuilder());
89+
}
90+
91+
Builder(RSocketRequester.Builder requesterBuilder) {
92+
Assert.notNull(requesterBuilder, "RSocketRequester.Builder is required");
93+
this.requesterBuilder = requesterBuilder;
94+
this.route = "graphql";
95+
}
96+
97+
private static RSocketRequester.Builder initRSocketRequestBuilder() {
98+
MimeType mimeType = MimeType.valueOf("application/graphql+json");
99+
RSocketRequester.Builder requesterBuilder = RSocketRequester.builder().dataMimeType(mimeType);
100+
if (jackson2Present) {
101+
requesterBuilder.rsocketStrategies(
102+
RSocketStrategies.builder()
103+
.encoder(DefaultJackson2Codecs.encoder())
104+
.decoder(DefaultJackson2Codecs.decoder())
105+
.build());
106+
}
107+
return requesterBuilder;
108+
}
109+
110+
@Override
111+
public Builder tcp(String host, int port) {
112+
this.clientTransport = TcpClientTransport.create(host, port);
113+
return this;
114+
}
115+
116+
@Override
117+
public Builder webSocket(URI uri) {
118+
this.clientTransport = WebsocketClientTransport.create(uri);
119+
return this;
120+
}
121+
122+
@Override
123+
public Builder clientTransport(ClientTransport clientTransport) {
124+
this.clientTransport = clientTransport;
125+
return this;
126+
}
127+
128+
@Override
129+
public Builder dataMimeType(MimeType dataMimeType) {
130+
this.requesterBuilder.dataMimeType(dataMimeType);
131+
return this;
132+
}
133+
134+
@Override
135+
public Builder route(String route) {
136+
Assert.notNull(route, "'route' is required");
137+
this.route = route;
138+
return this;
139+
}
140+
141+
@Override
142+
public Builder rsocketRequester(Consumer<RSocketRequester.Builder> requesterConsumer) {
143+
requesterConsumer.accept(this.requesterBuilder);
144+
return this;
145+
}
146+
147+
@Override
148+
public RSocketGraphQlClient build() {
149+
150+
Assert.state(this.clientTransport != null, "Neither WebSocket nor TCP networking configured");
151+
RSocketRequester requester = this.requesterBuilder.transport(this.clientTransport);
152+
RSocketGraphQlTransport graphQlTransport = new RSocketGraphQlTransport(this.route, requester);
153+
154+
// Pass the codecs to the parent for response decoding
155+
this.requesterBuilder.rsocketStrategies(builder -> {
156+
builder.decoders(decoders -> setJsonDecoder(CodecDelegate.findJsonDecoder(decoders)));
157+
builder.encoders(encoders -> setJsonEncoder(CodecDelegate.findJsonEncoder(encoders)));
158+
});
159+
160+
return new DefaultRSocketGraphQlClient(
161+
super.buildGraphQlClient(graphQlTransport),
162+
this.requesterBuilder, this.clientTransport, this.route, getBuilderInitializer());
163+
}
164+
}
165+
166+
}

spring-graphql/src/main/java/org/springframework/graphql/client/HttpGraphQlTransport.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131

3232
/**
3333
* Transport to execute GraphQL requests over HTTP via {@link WebClient}.
34-
* Supports only single-response requests over HTTP POST. For subscription
35-
* requests, see {@link WebSocketGraphQlTransport}.
34+
*
35+
* <p>Supports only single-response requests over HTTP POST. For subscriptions,
36+
* see {@link WebSocketGraphQlTransport} and {@link RSocketGraphQlTransport}.
3637
*
3738
* @author Rossen Stoyanchev
3839
* @since 1.0.0

0 commit comments

Comments
 (0)