This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
forked from yandooo/graphql-spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #514 from graphql-java-kickstart/feature/326-mono-…
…support Autoconfigure Generic wrapper for Mono fix #326
- Loading branch information
Showing
13 changed files
with
203 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...re-webflux/src/main/java/graphql/kickstart/spring/webflux/boot/MonoAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package graphql.kickstart.spring.webflux.boot; | ||
|
||
import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.REACTIVE; | ||
|
||
import graphql.kickstart.tools.SchemaParser; | ||
import graphql.kickstart.tools.SchemaParserOptions.GenericWrapper; | ||
import graphql.kickstart.tools.boot.GraphQLJavaToolsAutoConfiguration; | ||
import java.util.List; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Configuration | ||
@ConditionalOnClass(SchemaParser.class) | ||
@ConditionalOnWebApplication(type = REACTIVE) | ||
@AutoConfigureBefore(GraphQLJavaToolsAutoConfiguration.class) | ||
public class MonoAutoConfiguration { | ||
|
||
@Bean | ||
GenericWrapper monoWrapper(@Autowired(required = false) List<GenericWrapper> genericWrappers) { | ||
if (notWrapsMono(genericWrappers)) { | ||
return GenericWrapper.withTransformer( | ||
Mono.class, | ||
0, | ||
Mono::toFuture, | ||
t -> t | ||
); | ||
} | ||
return null; | ||
} | ||
|
||
private boolean notWrapsMono(List<GenericWrapper> genericWrappers) { | ||
return genericWrappers == null || | ||
genericWrappers.stream().noneMatch(it -> it.getType().isAssignableFrom(Mono.class)); | ||
} | ||
|
||
} |
3 changes: 2 additions & 1 deletion
3
...-kickstart-spring-boot-autoconfigure-webflux/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
graphql.kickstart.spring.webflux.boot.GraphQLSpringWebfluxAutoConfiguration | ||
graphql.kickstart.spring.webflux.boot.GraphQLSpringWebfluxAutoConfiguration,\ | ||
graphql.kickstart.spring.webflux.boot.MonoAutoConfiguration |
14 changes: 14 additions & 0 deletions
14
...autoconfigure-webflux/src/test/java/graphql/kickstart/spring/webflux/boot/HelloQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package graphql.kickstart.spring.webflux.boot; | ||
|
||
import graphql.kickstart.tools.GraphQLQueryResolver; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Component | ||
class HelloQuery implements GraphQLQueryResolver { | ||
|
||
public Mono<String> hello() { | ||
return Mono.just("Hello world"); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...ebflux/src/test/java/graphql/kickstart/spring/webflux/boot/MonoAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package graphql.kickstart.spring.webflux.boot; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
|
||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") | ||
@RequiredArgsConstructor | ||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class MonoAutoConfigurationTest { | ||
|
||
@Autowired | ||
private WebTestClient webTestClient; | ||
|
||
@Test | ||
void monoWrapper() throws JSONException { | ||
val result = webTestClient.post() | ||
.uri("/graphql") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue("{ \"query\": \"query { hello } \"}") | ||
.exchange() | ||
.returnResult(String.class); | ||
val response = result.getResponseBody().blockFirst(); | ||
val json = new JSONObject(response); | ||
assertThat(json.getJSONObject("data").get("hello")).isEqualTo("Hello world"); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...test/java/graphql/kickstart/spring/webflux/boot/MonoGenericWrapperAlreadyDefinedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package graphql.kickstart.spring.webflux.boot; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import graphql.kickstart.tools.SchemaParserOptions.GenericWrapper; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") | ||
@RequiredArgsConstructor | ||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class MonoGenericWrapperAlreadyDefinedTest { | ||
|
||
@Autowired | ||
private WebTestClient webTestClient; | ||
|
||
@Test | ||
void monoWrapper() throws JSONException { | ||
val result = webTestClient.post() | ||
.uri("/graphql") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue("{ \"query\": \"query { hello } \"}") | ||
.exchange() | ||
.returnResult(String.class); | ||
val response = result.getResponseBody().blockFirst(); | ||
val json = new JSONObject(response); | ||
assertThat(json.getJSONObject("data").get("hello")).isEqualTo("Hello world"); | ||
} | ||
|
||
@TestConfiguration | ||
static class MonoConfiguration { | ||
@Bean | ||
GenericWrapper genericWrapper() { | ||
return GenericWrapper.withTransformer( | ||
Mono.class, | ||
0, | ||
Mono::toFuture, | ||
t -> t | ||
); | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...e-webflux/src/test/java/graphql/kickstart/spring/webflux/boot/MySubscriptionResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package graphql.kickstart.spring.webflux.boot; | ||
|
||
import graphql.kickstart.tools.GraphQLSubscriptionResolver; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import java.time.Duration; | ||
import org.reactivestreams.Publisher; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Flux; | ||
|
||
@Service | ||
class MySubscriptionResolver implements GraphQLSubscriptionResolver { | ||
|
||
Publisher<Integer> hello(DataFetchingEnvironment env) { | ||
return Flux.range(0, 100).delayElements(Duration.ofSeconds(1)); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...igure-webflux/src/test/java/graphql/kickstart/spring/webflux/boot/WebfluxApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package graphql.kickstart.spring.webflux.boot; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class WebfluxApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(WebfluxApplication.class, args); | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...-kickstart-spring-boot-autoconfigure-webflux/src/test/resources/query-hello-world.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
query { | ||
hello | ||
} |
7 changes: 7 additions & 0 deletions
7
graphql-kickstart-spring-boot-autoconfigure-webflux/src/test/resources/schema.graphqls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type Query { | ||
hello: String | ||
} | ||
|
||
type Subscription { | ||
hello: Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters