Reformat sample applications
Fixes gh-54
This commit is contained in:
@@ -22,8 +22,8 @@ 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 in Reactor data fetchers.
|
||||
* WebFilter that inserts a key-value pair into the Reactor context which is transferred
|
||||
* to and accessible in Reactor data fetchers.
|
||||
*/
|
||||
public class ContextWebFilter implements WebFilter {
|
||||
|
||||
|
||||
@@ -41,19 +41,17 @@ public class DataRepository {
|
||||
}
|
||||
|
||||
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);
|
||||
}));
|
||||
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);
|
||||
}));
|
||||
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);
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,26 +26,22 @@ 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", typeBuilder ->
|
||||
typeBuilder.dataFetcher("greeting", this.dataRepository::getBasic));
|
||||
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("greetingMono", this.dataRepository::getGreeting));
|
||||
|
||||
builder.type("Query", typeBuilder ->
|
||||
typeBuilder.dataFetcher("greetingsFlux", this.dataRepository::getGreetings));
|
||||
builder.type("Query",
|
||||
typeBuilder -> typeBuilder.dataFetcher("greetingsFlux", this.dataRepository::getGreetings));
|
||||
|
||||
builder.type("Subscription", typeBuilder ->
|
||||
typeBuilder.dataFetcher("greetings", this.dataRepository::getGreetingsStream));
|
||||
builder.type("Subscription",
|
||||
typeBuilder -> typeBuilder.dataFetcher("greetings", this.dataRepository::getGreetingsStream));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,29 +32,21 @@ public class QueryTests {
|
||||
|
||||
private GraphQlTester graphQlTester;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setUp(@Autowired WebGraphQlHandler handler) {
|
||||
this.graphQlTester = GraphQlTester.create(webInput ->
|
||||
handler.handle(webInput).contextWrite(context -> context.put("name", "James")));
|
||||
this.graphQlTester = GraphQlTester
|
||||
.create(webInput -> handler.handle(webInput).contextWrite(context -> context.put("name", "James")));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void greetingMono() {
|
||||
this.graphQlTester.query("{greetingMono}")
|
||||
.execute()
|
||||
.path("greetingMono")
|
||||
.entity(String.class)
|
||||
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)
|
||||
this.graphQlTester.query("{greetingsFlux}").execute().path("greetingsFlux").entityList(String.class)
|
||||
.containsExactly("Hi James", "Bonjour James", "Hola James", "Ciao James", "Zdravo James");
|
||||
}
|
||||
|
||||
|
||||
@@ -34,44 +34,31 @@ public class SubscriptionTests {
|
||||
|
||||
private GraphQlTester graphQlTester;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setUp(@Autowired WebGraphQlHandler handler) {
|
||||
this.graphQlTester = GraphQlTester.create(webInput ->
|
||||
handler.handle(webInput).contextWrite(context -> context.put("name", "James")));
|
||||
this.graphQlTester = GraphQlTester
|
||||
.create(webInput -> handler.handle(webInput).contextWrite(context -> context.put("name", "James")));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void subscriptionWithEntityPath() {
|
||||
String query = "subscription { greetings }";
|
||||
|
||||
Flux<String> result = this.graphQlTester.query(query)
|
||||
.executeSubscription()
|
||||
.toFlux("greetings", String.class);
|
||||
Flux<String> result = this.graphQlTester.query(query).executeSubscription().toFlux("greetings", String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext("Hi James")
|
||||
.expectNext("Bonjour James")
|
||||
.expectNext("Hola James")
|
||||
.expectNext("Ciao James")
|
||||
.expectNext("Zdravo James")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(result).expectNext("Hi James").expectNext("Bonjour James").expectNext("Hola James")
|
||||
.expectNext("Ciao James").expectNext("Zdravo James").verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void subscriptionWithResponseSpec() {
|
||||
String query = "subscription { greetings }";
|
||||
|
||||
Flux<GraphQlTester.ResponseSpec> result = this.graphQlTester.query(query)
|
||||
.executeSubscription()
|
||||
.toFlux();
|
||||
Flux<GraphQlTester.ResponseSpec> result = this.graphQlTester.query(query).executeSubscription().toFlux();
|
||||
|
||||
StepVerifier.create(result)
|
||||
.consumeNextWith(spec -> spec.path("greetings").valueExists())
|
||||
StepVerifier.create(result).consumeNextWith(spec -> spec.path("greetings").valueExists())
|
||||
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Bonjour James\""))
|
||||
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Hola James\""))
|
||||
.expectNextCount(2)
|
||||
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Hola James\"")).expectNextCount(2)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,13 +12,12 @@ import static org.springframework.web.context.request.RequestAttributes.SCOPE_RE
|
||||
@Component
|
||||
public class GreetingDataWiring implements RuntimeWiringCustomizer {
|
||||
|
||||
|
||||
@Override
|
||||
public void customize(RuntimeWiring.Builder builder) {
|
||||
builder.type("Query", typeWiring ->
|
||||
typeWiring.dataFetcher("greeting", env -> {
|
||||
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
||||
return "Hello " + attributes.getAttribute(RequestAttributeFilter.NAME_ATTRIBUTE, SCOPE_REQUEST);
|
||||
}));
|
||||
builder.type("Query", typeWiring -> typeWiring.dataFetcher("greeting", env -> {
|
||||
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
||||
return "Hello " + attributes.getAttribute(RequestAttributeFilter.NAME_ATTRIBUTE, SCOPE_REQUEST);
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,9 +33,9 @@ public class RequestAttributeFilter implements Filter {
|
||||
|
||||
public static final String NAME_ATTRIBUTE = RequestAttributeFilter.class.getName() + ".name";
|
||||
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
request.setAttribute(NAME_ATTRIBUTE, "007");
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@@ -24,15 +24,13 @@ import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
|
||||
/**
|
||||
* {@link ThreadLocalAccessor} to expose a thread-bound RequestAttributes object
|
||||
* to data fetchers in Spring GraphQL.
|
||||
* {@link ThreadLocalAccessor} to expose a thread-bound RequestAttributes object to data
|
||||
* fetchers in Spring GraphQL.
|
||||
*/
|
||||
@Component
|
||||
public class RequestAttributesAccessor implements ThreadLocalAccessor {
|
||||
|
||||
private static final String ATTRIBUTES_KEY =
|
||||
RequestAttributesAccessor.class.getName() + ".requestAttributes";
|
||||
|
||||
private static final String ATTRIBUTES_KEY = RequestAttributesAccessor.class.getName() + ".requestAttributes";
|
||||
|
||||
@Override
|
||||
public void extractValues(Map<String, Object> container) {
|
||||
|
||||
@@ -53,4 +53,5 @@ public class Project {
|
||||
public void setReleases(List<Release> releases) {
|
||||
this.releases = releases;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,17 +15,13 @@ public class ProjectDataWiring implements RuntimeWiringCustomizer {
|
||||
|
||||
@Override
|
||||
public void customize(RuntimeWiring.Builder builder) {
|
||||
builder
|
||||
.type("Query", typeWiring ->
|
||||
typeWiring.dataFetcher("project", env -> {
|
||||
String slug = env.getArgument("slug");
|
||||
return client.fetchProject(slug);
|
||||
}))
|
||||
.type("Project", typeWiring ->
|
||||
typeWiring.dataFetcher("releases", env -> {
|
||||
Project project = env.getSource();
|
||||
return client.fetchProjectReleases(project.getSlug());
|
||||
})
|
||||
);
|
||||
builder.type("Query", typeWiring -> typeWiring.dataFetcher("project", env -> {
|
||||
String slug = env.getArgument("slug");
|
||||
return client.fetchProject(slug);
|
||||
})).type("Project", typeWiring -> typeWiring.dataFetcher("releases", env -> {
|
||||
Project project = env.getSource();
|
||||
return client.fetchProjectReleases(project.getSlug());
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@ import java.util.Arrays;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
|
||||
public enum ProjectStatus {
|
||||
|
||||
ACTIVE, COMMUNITY, INCUBATING, ATTIC;
|
||||
|
||||
@JsonCreator
|
||||
public static ProjectStatus fromName(String name) {
|
||||
return Arrays.stream(ProjectStatus.values())
|
||||
.filter(type -> type.name().equals(name))
|
||||
.findFirst().orElse(ProjectStatus.ACTIVE);
|
||||
return Arrays.stream(ProjectStatus.values()).filter(type -> type.name().equals(name)).findFirst()
|
||||
.orElse(ProjectStatus.ACTIVE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -51,4 +51,5 @@ public class Release {
|
||||
public void setCurrent(boolean current) {
|
||||
this.current = current;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@ import java.util.Arrays;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
|
||||
public enum ReleaseStatus {
|
||||
|
||||
GENERAL_AVAILABILITY, MILESTONE, SNAPSHOT;
|
||||
|
||||
@JsonCreator
|
||||
public static ReleaseStatus fromName(String name) {
|
||||
return Arrays.stream(ReleaseStatus.values())
|
||||
.filter(type -> type.name().equals(name))
|
||||
.findFirst().orElse(ReleaseStatus.GENERAL_AVAILABILITY);
|
||||
return Arrays.stream(ReleaseStatus.values()).filter(type -> type.name().equals(name)).findFirst()
|
||||
.orElse(ReleaseStatus.GENERAL_AVAILABILITY);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,27 +19,26 @@ import org.springframework.web.client.RestTemplate;
|
||||
@Component
|
||||
public class SpringProjectsClient {
|
||||
|
||||
private static final TypeReferences.CollectionModelType<Release> releaseCollection =
|
||||
new TypeReferences.CollectionModelType<Release>() { };
|
||||
private static final TypeReferences.CollectionModelType<Release> releaseCollection = new TypeReferences.CollectionModelType<Release>() {
|
||||
};
|
||||
|
||||
private final Traverson traverson;
|
||||
|
||||
public SpringProjectsClient(RestTemplateBuilder builder) {
|
||||
RestTemplate restTemplate = builder.messageConverters(Traverson.getDefaultMessageConverters(MediaTypes.HAL_JSON)).build();
|
||||
RestTemplate restTemplate = builder
|
||||
.messageConverters(Traverson.getDefaultMessageConverters(MediaTypes.HAL_JSON)).build();
|
||||
this.traverson = new Traverson(URI.create("https://spring.io/api/"), MediaTypes.HAL_JSON);
|
||||
this.traverson.setRestOperations(restTemplate);
|
||||
}
|
||||
|
||||
public Project fetchProject(String projectSlug) {
|
||||
return this.traverson.follow("projects")
|
||||
.follow(Hop.rel("project").withParameter("id", projectSlug))
|
||||
return this.traverson.follow("projects").follow(Hop.rel("project").withParameter("id", projectSlug))
|
||||
.toObject(Project.class);
|
||||
}
|
||||
|
||||
public List<Release> fetchProjectReleases(String projectSlug) {
|
||||
CollectionModel<Release> releases = this.traverson.follow("projects")
|
||||
.follow(Hop.rel("project").withParameter("id", projectSlug))
|
||||
.follow(Hop.rel("releases"))
|
||||
.follow(Hop.rel("project").withParameter("id", projectSlug)).follow(Hop.rel("releases"))
|
||||
.toObject(releaseCollection);
|
||||
return new ArrayList(releases.getContent());
|
||||
}
|
||||
|
||||
@@ -20,9 +20,11 @@ public class ArtifactRepositoriesInitializer implements ApplicationRunner {
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
List<ArtifactRepository> repositoryList = Arrays.asList(
|
||||
new ArtifactRepository("spring-releases", "Spring Releases", "https://repo.spring.io/libs-releases"),
|
||||
new ArtifactRepository("spring-milestones", "Spring Milestones", "https://repo.spring.io/libs-milestones"),
|
||||
new ArtifactRepository("spring-snapshots", "Spring Snapshots", "https://repo.spring.io/libs-snapshots")
|
||||
);
|
||||
new ArtifactRepository("spring-milestones", "Spring Milestones",
|
||||
"https://repo.spring.io/libs-milestones"),
|
||||
new ArtifactRepository("spring-snapshots", "Spring Snapshots",
|
||||
"https://repo.spring.io/libs-snapshots"));
|
||||
repositories.saveAll(repositoryList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -55,4 +55,5 @@ public class ArtifactRepository {
|
||||
public void setSnapshotsEnabled(boolean snapshotsEnabled) {
|
||||
this.snapshotsEnabled = snapshotsEnabled;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,8 +15,9 @@ public class ArtifactRepositoryDataWiring implements RuntimeWiringCustomizer {
|
||||
|
||||
@Override
|
||||
public void customize(RuntimeWiring.Builder builder) {
|
||||
builder.type("Query", typeWiring -> typeWiring
|
||||
.dataFetcher("artifactRepositories", env -> this.repositories.findAll())
|
||||
.dataFetcher("artifactRepository", env -> this.repositories.findById(env.getArgument("id"))));
|
||||
builder.type("Query",
|
||||
typeWiring -> typeWiring.dataFetcher("artifactRepositories", env -> this.repositories.findAll())
|
||||
.dataFetcher("artifactRepository", env -> this.repositories.findById(env.getArgument("id"))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,50 +39,28 @@ public class MockMvcGraphQlTests {
|
||||
|
||||
@Test
|
||||
void jsonPath() {
|
||||
String query = "{" +
|
||||
" project(slug:\"spring-framework\") {" +
|
||||
" releases {" +
|
||||
" version" +
|
||||
" }" +
|
||||
" }" +
|
||||
"}";
|
||||
String query = "{" + " project(slug:\"spring-framework\") {" + " releases {" + " version" + " }"
|
||||
+ " }" + "}";
|
||||
|
||||
this.graphQlTester.query(query)
|
||||
.execute()
|
||||
.path("project.releases[*].version")
|
||||
.entityList(String.class)
|
||||
this.graphQlTester.query(query).execute().path("project.releases[*].version").entityList(String.class)
|
||||
.hasSizeGreaterThan(1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonContent() {
|
||||
String query = "{" +
|
||||
" project(slug:\"spring-framework\") {" +
|
||||
" repositoryUrl" +
|
||||
" }" +
|
||||
"}";
|
||||
String query = "{" + " project(slug:\"spring-framework\") {" + " repositoryUrl" + " }" + "}";
|
||||
|
||||
this.graphQlTester.query(query)
|
||||
.execute()
|
||||
.path("project")
|
||||
this.graphQlTester.query(query).execute().path("project")
|
||||
.matchesJson("{\"repositoryUrl\":\"http://github.com/spring-projects/spring-framework\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void decodedResponse() {
|
||||
String query = "{" +
|
||||
" project(slug:\"spring-framework\") {" +
|
||||
" releases {" +
|
||||
" version" +
|
||||
" }" +
|
||||
" }" +
|
||||
"}";
|
||||
String query = "{" + " project(slug:\"spring-framework\") {" + " releases {" + " version" + " }"
|
||||
+ " }" + "}";
|
||||
|
||||
this.graphQlTester.query(query)
|
||||
.execute()
|
||||
.path("project")
|
||||
.entity(Project.class)
|
||||
this.graphQlTester.query(query).execute().path("project").entity(Project.class)
|
||||
.satisfies(project -> assertThat(project.getReleases()).hasSizeGreaterThan(1));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user