forked from spring-io/spring-asciidoctor-backends
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arguably a better way to encapsulate support for WebInterceptor vs a base class, more testable, and reduced public API surface.
- Loading branch information
1 parent
40a79ab
commit 1f1ce72
Showing
11 changed files
with
149 additions
and
33 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
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
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
2 changes: 1 addition & 1 deletion
2
...rg/springframework/boot/graphql/Book.java → ...ava/org/springframework/graphql/Book.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.springframework.boot.graphql; | ||
package org.springframework.graphql; | ||
|
||
public class Book { | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...ork/boot/graphql/GraphQLDataFetchers.java → ...ramework/graphql/GraphQLDataFetchers.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
113 changes: 113 additions & 0 deletions
113
...g-graphql-web/src/test/java/org/springframework/graphql/WebInterceptorExecutionTests.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,113 @@ | ||
/* | ||
* Copyright 2020-2020 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 org.springframework.graphql; | ||
|
||
import java.io.File; | ||
import java.net.URI; | ||
import java.time.Duration; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import graphql.ExecutionInput; | ||
import graphql.GraphQL; | ||
import graphql.schema.GraphQLSchema; | ||
import graphql.schema.idl.RuntimeWiring; | ||
import graphql.schema.idl.SchemaGenerator; | ||
import graphql.schema.idl.SchemaParser; | ||
import graphql.schema.idl.TypeDefinitionRegistry; | ||
import org.junit.jupiter.api.Test; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.util.ResourceUtils; | ||
|
||
import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Unit tests for {@link WebInterceptorExecution}. | ||
*/ | ||
public class WebInterceptorExecutionTests { | ||
|
||
@Test | ||
void testInterceptorInvocation() throws Exception { | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
List<WebInterceptor> interceptors = Arrays.asList( | ||
new TestWebInterceptor(sb, 1), new TestWebInterceptor(sb, 2), new TestWebInterceptor(sb, 3)); | ||
|
||
String query = "{" + | ||
" bookById(id: \\\"book-1\\\"){ " + | ||
" id" + | ||
" name" + | ||
" pageCount" + | ||
" author" + | ||
" }" + | ||
"}"; | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
Map body = mapper.reader().readValue("{\"query\": \"" + query + "\"}", Map.class); | ||
WebInput webInput = new WebInput(URI.create("/graphql"), new HttpHeaders(), body); | ||
|
||
WebOutput webOutput = new WebInterceptorExecution(createGraphQL(), interceptors) | ||
.execute(webInput).block(); | ||
|
||
assertEquals(":pre1:pre2:pre3:post3:post2:post1", sb.toString()); | ||
assertTrue(webOutput.isDataPresent()); | ||
} | ||
|
||
private static GraphQL createGraphQL() throws Exception { | ||
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring() | ||
.type(newTypeWiring("Query").dataFetcher("bookById", GraphQLDataFetchers.getBookByIdDataFetcher())) | ||
.build(); | ||
|
||
File file = ResourceUtils.getFile("classpath:books/schema.graphqls"); | ||
TypeDefinitionRegistry registry = new SchemaParser().parse(file); | ||
SchemaGenerator generator = new SchemaGenerator(); | ||
GraphQLSchema schema = generator.makeExecutableSchema(registry, runtimeWiring); | ||
|
||
return GraphQL.newGraphQL(schema).build(); | ||
} | ||
|
||
|
||
private static class TestWebInterceptor implements WebInterceptor { | ||
|
||
private final StringBuilder output; | ||
|
||
private final int index; | ||
|
||
public TestWebInterceptor(StringBuilder output, int index) { | ||
this.output = output; | ||
this.index = index; | ||
} | ||
|
||
@Override | ||
public Mono<ExecutionInput> preHandle(ExecutionInput executionInput, WebInput webInput) { | ||
this.output.append(":pre").append(this.index); | ||
return Mono.delay(Duration.ofMillis(50)).map(aLong -> executionInput); | ||
} | ||
|
||
@Override | ||
public Mono<WebOutput> postHandle(WebOutput webOutput) { | ||
this.output.append(":post").append(this.index); | ||
return Mono.delay(Duration.ofMillis(50)).map(aLong -> webOutput); | ||
} | ||
} | ||
|
||
} |