Skip to content

Commit 2dc2e5a

Browse files
committed
Auto-configure RSocketGraphQlClient
This commit contributes a `RSocketGraphQlClient.Builder` component to the context, pre-configured with the `RSocketStrategies`, a customized `RSocketConnector` and the expected data MIME type. See gh-30453
1 parent 9c3cce5 commit 2dc2e5a

File tree

5 files changed

+162
-51
lines changed

5 files changed

+162
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-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.boot.autoconfigure.graphql.rsocket;
18+
19+
import graphql.GraphQL;
20+
import io.rsocket.RSocket;
21+
import io.rsocket.transport.netty.client.TcpClientTransport;
22+
23+
import org.springframework.boot.autoconfigure.AutoConfiguration;
24+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Scope;
30+
import org.springframework.graphql.client.RSocketGraphQlClient;
31+
import org.springframework.messaging.rsocket.RSocketRequester;
32+
import org.springframework.util.MimeTypeUtils;
33+
34+
/**
35+
* {@link EnableAutoConfiguration Auto-configuration} for {@link RSocketGraphQlClient}.
36+
* This auto-configuration creates {@link RSocketGraphQlClient.Builder} prototype beans,
37+
* as the builders are stateful and should not be reused to build client instances with
38+
* different configurations.
39+
*
40+
* @author Brian Clozel
41+
* @since 2.7.0
42+
*/
43+
@AutoConfiguration(after = RSocketRequesterAutoConfiguration.class)
44+
@ConditionalOnClass({ GraphQL.class, RSocketGraphQlClient.class, RSocketRequester.class, RSocket.class,
45+
TcpClientTransport.class })
46+
public class RSocketGraphQlClientAutoConfiguration {
47+
48+
@Bean
49+
@Scope("prototype")
50+
@ConditionalOnMissingBean
51+
public RSocketGraphQlClient.Builder<?> rsocketGraphQlClientBuilder(
52+
RSocketRequester.Builder rsocketRequesterBuilder) {
53+
return RSocketGraphQlClient.builder(rsocketRequesterBuilder.dataMimeType(MimeTypeUtils.APPLICATION_GRAPHQL));
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2012-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.boot.autoconfigure.graphql.rsocket;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.autoconfigure.AutoConfigurations;
22+
import org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration;
24+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.graphql.client.RSocketGraphQlClient;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link RSocketGraphQlClientAutoConfiguration}.
33+
*
34+
* @author Brian Clozel
35+
*/
36+
class RSocketGraphQlClientAutoConfigurationTests {
37+
38+
private static final RSocketGraphQlClient.Builder<?> builderInstance = RSocketGraphQlClient.builder();
39+
40+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
41+
.withConfiguration(AutoConfigurations.of(RSocketStrategiesAutoConfiguration.class,
42+
RSocketRequesterAutoConfiguration.class, RSocketGraphQlClientAutoConfiguration.class));
43+
44+
@Test
45+
void shouldCreateBuilder() {
46+
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(RSocketGraphQlClient.Builder.class));
47+
}
48+
49+
@Test
50+
void shouldGetPrototypeScopedBean() {
51+
this.contextRunner.run((context) -> {
52+
RSocketGraphQlClient.Builder<?> first = context.getBean(RSocketGraphQlClient.Builder.class);
53+
RSocketGraphQlClient.Builder<?> second = context.getBean(RSocketGraphQlClient.Builder.class);
54+
assertThat(first).isNotEqualTo(second);
55+
});
56+
}
57+
58+
@Test
59+
void shouldNotCreateBuilderIfAlreadyPresent() {
60+
this.contextRunner.withUserConfiguration(CustomRSocketGraphQlClientBuilder.class).run((context) -> {
61+
RSocketGraphQlClient.Builder<?> builder = context.getBean(RSocketGraphQlClient.Builder.class);
62+
assertThat(builder).isEqualTo(builderInstance);
63+
});
64+
}
65+
66+
@Configuration(proxyBeanMethods = false)
67+
static class CustomRSocketGraphQlClientBuilder {
68+
69+
@Bean
70+
RSocketGraphQlClient.Builder<?> myRSocketGraphQlClientBuilder() {
71+
return builderInstance;
72+
}
73+
74+
}
75+
76+
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/spring-graphql.adoc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,14 @@ Spring Boot supports many configuration properties under the `spring.graphql.cor
117117

118118
RSocket is also supported as a transport, on top of WebSocket or TCP.
119119
Once the <<messaging#messaging.rsocket.server-auto-configuration,RSocket server is configured>>, we can configure our GraphQL handler on a particular route using configprop:spring.graphql.rsocket.mapping[].
120-
For example, configuring that mapping as `"graphql"` means we can use the `RSocketGraphQlClient` as follows.
120+
For example, configuring that mapping as `"graphql"` means we can use that as a route when sending requests with the `RSocketGraphQlClient`.
121121

122-
For RSocket over TCP:
123-
include::code:RSocketGraphQlClientExample[tag=tcp]
122+
Spring Boot auto-configures a `RSocketGraphQlClient.Builder<?>` bean that you can inject in your components:
124123

125-
For RSocket over WebSocket:
126-
include::code:RSocketGraphQlClientExample[tag=websocket]
124+
include::code:RSocketGraphQlClientExample[tag=builder]
125+
126+
And then send a request:
127+
include::code:RSocketGraphQlClientExample[tag=request]
127128

128129

129130
[[web.graphql.exception-handling]]

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/graphql/transports/rsocket/RSocketGraphQlClientExample.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,29 @@
1616

1717
package org.springframework.boot.docs.web.graphql.transports.rsocket;
1818

19-
import java.net.URI;
2019
import java.time.Duration;
2120

2221
import reactor.core.publisher.Mono;
2322

2423
import org.springframework.graphql.client.RSocketGraphQlClient;
24+
import org.springframework.stereotype.Component;
2525

26+
// tag::builder[]
27+
@Component
2628
public class RSocketGraphQlClientExample {
2729

28-
public void rsocketOverTcp() {
29-
// tag::tcp[]
30-
RSocketGraphQlClient client = RSocketGraphQlClient.builder().tcp("example.spring.io", 8181).route("graphql")
31-
.build();
32-
Mono<Book> book = client.document("{ bookById(id: \"book-1\"){ id name pageCount author } }")
33-
.retrieve("bookById").toEntity(Book.class);
34-
// end::tcp[]
35-
book.block(Duration.ofSeconds(5));
30+
private final RSocketGraphQlClient graphQlClient;
31+
32+
public RSocketGraphQlClientExample(RSocketGraphQlClient.Builder<?> builder) {
33+
this.graphQlClient = builder.tcp("example.spring.io", 8181).route("graphql").build();
3634
}
35+
// end::builder[]
3736

38-
public void rsocketOverWebSocket() {
39-
// tag::websocket[]
40-
RSocketGraphQlClient client = RSocketGraphQlClient.builder()
41-
.webSocket(URI.create("wss://example.spring.io/rsocket")).route("graphql").build();
42-
Mono<Book> book = client.document("{ bookById(id: \"book-1\"){ id name pageCount author } }")
37+
public void rsocketOverTcp() {
38+
// tag::request[]
39+
Mono<Book> book = this.graphQlClient.document("{ bookById(id: \"book-1\"){ id name pageCount author } }")
4340
.retrieve("bookById").toEntity(Book.class);
44-
// end::websocket[]
41+
// end::request[]
4542
book.block(Duration.ofSeconds(5));
4643
}
4744

spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/graphql/transports/rsocket/RSocketGraphQlClientExample.kt

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@
1717
package org.springframework.boot.docs.web.graphql.transports.rsocket
1818

1919
import org.springframework.graphql.client.RSocketGraphQlClient
20-
import java.net.URI
20+
import org.springframework.stereotype.Component
2121
import java.time.Duration
2222

23+
// tag::builder[]
24+
@Component
25+
class RSocketGraphQlClientExample(private val builder: RSocketGraphQlClient.Builder<*>) {
26+
// end::builder[]
2327

24-
class RSocketGraphQlClientExample {
28+
val graphQlClient = builder.tcp("example.spring.io", 8181)
29+
.route("graphql")
30+
.build()
2531

2632
fun rsocketOverTcp() {
27-
// tag::tcp[]
28-
val client = RSocketGraphQlClient.builder()
29-
.tcp("example.spring.io", 8181)
30-
.route("graphql")
31-
.build()
32-
val book = client.document(
33+
// tag::request[]
34+
val book = graphQlClient.document(
3335
"""
3436
{
3537
bookById(id: "book-1"){
@@ -39,31 +41,10 @@ class RSocketGraphQlClientExample {
3941
author
4042
}
4143
}
42-
""")
43-
.retrieve("bookById").toEntity(Book::class.java)
44-
// end::tcp[]
45-
book.block(Duration.ofSeconds(5))
46-
}
47-
48-
fun rsocketOverWebSocket() {
49-
// tag::websocket[]
50-
val client = RSocketGraphQlClient.builder()
51-
.webSocket(URI.create("wss://example.spring.io/rsocket"))
52-
.route("graphql")
53-
.build()
54-
val book = client.document(
5544
"""
56-
{
57-
bookById(id: "book-1"){
58-
id
59-
name
60-
pageCount
61-
author
62-
}
63-
}
64-
""")
45+
)
6546
.retrieve("bookById").toEntity(Book::class.java)
66-
// end::websocket[]
47+
// end::request[]
6748
book.block(Duration.ofSeconds(5))
6849
}
6950

0 commit comments

Comments
 (0)