Reactor DataFetcher support

Closes gh-47
This commit is contained in:
Rossen Stoyanchev
2021-04-23 20:57:22 +01:00
parent a57b78e521
commit 1e619263d2
20 changed files with 565 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '2.4.4'
id 'org.springframework.boot' version '2.4.5'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2002-2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql;
import java.time.Duration;
import graphql.schema.DataFetchingEnvironment;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.stereotype.Repository;
/**
* Repository with data fetcher methods.
*/
@Repository
public class DataRepository {
public String getBasic(DataFetchingEnvironment environment) {
return "Hello world!";
}
public Mono<String> getGreeting(DataFetchingEnvironment environment) {
return Mono.deferContextual(context -> {
Object name = context.get("name");
return Mono.delay(Duration.ofMillis(50)).map(aLong -> "Hello " + name);
});
}
public Flux<String> getGreetings(DataFetchingEnvironment environment) {
return Mono.delay(Duration.ofMillis(50)).flatMapMany(aLong ->
Flux.deferContextual(context -> {
String name = context.get("name");
return Flux.just("Hi", "Bonjour", "Hola", "Ciao", "Zdravo").map(s -> s + " " + name);
}));
}
public Flux<String> getGreetingsStream(DataFetchingEnvironment environment) {
return Mono.delay(Duration.ofMillis(50)).flatMapMany(aLong ->
Flux.deferContextual(context -> {
String name = context.get("name");
return Flux.just("Hi", "Bonjour", "Hola", "Ciao", "Zdravo").map(s -> s + " " + name);
}));
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2002-2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql;
import reactor.core.publisher.Mono;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
/**
* WebFilter that inserts a key-value pair into the Reactor context which is
* transferred to and accessible to Reactor-based data fetchers.
*/
public class ReactorContextWebFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange).contextWrite(context -> context.put("name", "007"));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@@ -18,6 +18,7 @@ package io.spring.sample.graphql;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SampleApplication {
@@ -25,4 +26,10 @@ public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
@Bean
ReactorContextWebFilter reactorContextWebFilter() {
return new ReactorContextWebFilter();
}
}

View File

@@ -1,23 +1,51 @@
/*
* Copyright 2002-2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql;
import java.time.Duration;
import graphql.schema.idl.RuntimeWiring;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.boot.RuntimeWiringCustomizer;
import org.springframework.stereotype.Component;
@Component
public class SampleWiring implements RuntimeWiringCustomizer {
private final DataRepository dataRepository;
public SampleWiring(@Autowired DataRepository dataRepository) {
this.dataRepository = dataRepository;
}
@Override
public void customize(RuntimeWiring.Builder builder) {
builder.type("Query", wiringBuilder -> wiringBuilder.dataFetcher("hello",
env -> "Hello world!"));
builder.type("Subscription", wiringBuilder -> wiringBuilder.dataFetcher("greetings",
env -> Flux.just("Hi", "Bonjour", "Hola", "Ciao", "Zdravo")
.delayElements(Duration.ofMillis(500))));
builder.type("Query", typeBuilder ->
typeBuilder.dataFetcher("greeting", this.dataRepository::getBasic));
builder.type("Query", typeBuilder ->
typeBuilder.dataFetcher("greetingMono", this.dataRepository::getGreeting));
builder.type("Query", typeBuilder ->
typeBuilder.dataFetcher("greetingsFlux", this.dataRepository::getGreetings));
builder.type("Subscription", typeBuilder ->
typeBuilder.dataFetcher("greetings", this.dataRepository::getGreetingsStream));
}
}

View File

@@ -1,5 +1,7 @@
type Query {
hello: String
greeting: String
greetingMono : String
greetingsFlux : [String]
}
type Subscription {
greetings: String

View File

@@ -17,7 +17,7 @@
let result;
client.subscribe(
{
query: '{ hello }',
query: '{ greeting }',
},
{
next: (data) => (result = data),

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2002-2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql;
import graphql.GraphQL;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.graphql.WebGraphQLService;
import org.springframework.graphql.test.query.GraphQLTester;
/**
* GraphQL query tests directly via {@link GraphQL}.
*/
@SpringBootTest
public class QueryTests {
private GraphQLTester graphQLTester;
@BeforeEach
public void setUp(@Autowired WebGraphQLService service) {
this.graphQLTester = GraphQLTester.create(webInput ->
service.execute(webInput).contextWrite(context -> context.put("name", "James")));
}
@Test
void greetingMono() {
this.graphQLTester.query("{greetingMono}")
.execute()
.path("greetingMono")
.entity(String.class)
.isEqualTo("Hello James");
}
@Test
void greetingsFlux() {
this.graphQLTester.query("{greetingsFlux}")
.execute()
.path("greetingsFlux")
.entityList(String.class)
.containsExactly("Hi James", "Bonjour James", "Hola James", "Ciao James", "Zdravo James");
}
}

View File

@@ -30,14 +30,15 @@ import org.springframework.graphql.test.query.GraphQLTester;
* GraphQL subscription tests directly via {@link GraphQL}.
*/
@SpringBootTest
public class SubscriptionGraphQLTests {
public class SubscriptionTests {
private GraphQLTester graphQLTester;
@BeforeEach
public void setUp(@Autowired WebGraphQLService service) {
this.graphQLTester = GraphQLTester.create(service);
this.graphQLTester = GraphQLTester.create(webInput ->
service.execute(webInput).contextWrite(context -> context.put("name", "James")));
}
@@ -50,7 +51,11 @@ public class SubscriptionGraphQLTests {
.toFlux("greetings", String.class);
StepVerifier.create(result)
.expectNext("Hi", "Bonjour", "Hola", "Ciao", "Zdravo")
.expectNext("Hi James")
.expectNext("Bonjour James")
.expectNext("Hola James")
.expectNext("Ciao James")
.expectNext("Zdravo James")
.verifyComplete();
}
@@ -64,8 +69,8 @@ public class SubscriptionGraphQLTests {
StepVerifier.create(result)
.consumeNextWith(spec -> spec.path("greetings").valueExists())
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Bonjour\""))
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Hola\""))
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Bonjour James\""))
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Hola James\""))
.expectNextCount(2)
.verifyComplete();
}

View File

@@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '2.4.4'
id 'org.springframework.boot' version '2.4.5'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}