This repository was archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
add ExecutionInputCustomizer #9
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
19 changes: 19 additions & 0 deletions
19
...va-spring-webflux/src/main/java/graphql/spring/web/reactive/ExecutionInputCustomizer.java
This file contains hidden or 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,19 @@ | ||
package graphql.spring.web.reactive; | ||
|
||
import graphql.ExecutionInput; | ||
import graphql.PublicApi; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Lets you customize the #ExecutionInput before the query is executed. | ||
* You can for example set a context object or define a root value. | ||
* <p> | ||
* This is only used if you use the default {@link GraphQLInvocation}. | ||
*/ | ||
@PublicApi | ||
public interface ExecutionInputCustomizer { | ||
|
||
Mono<ExecutionInput> customizeExecutionInput(ExecutionInput executionInput, ServerWebExchange webRequest); | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking it should not be a Mono - but - perhaps it should since you might call out to somewhere to get a special "context token" etc... So good idea! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea ... it is all about giving the user the possibilities, so I think making it async is a good thing. |
This file contains hidden or 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
18 changes: 18 additions & 0 deletions
18
...src/main/java/graphql/spring/web/reactive/components/DefaultExecutionInputCustomizer.java
This file contains hidden or 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,18 @@ | ||
package graphql.spring.web.reactive.components; | ||
|
||
import graphql.ExecutionInput; | ||
import graphql.Internal; | ||
import graphql.spring.web.reactive.ExecutionInputCustomizer; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Component | ||
@Internal | ||
public class DefaultExecutionInputCustomizer implements ExecutionInputCustomizer { | ||
|
||
@Override | ||
public Mono<ExecutionInput> customizeExecutionInput(ExecutionInput executionInput, ServerWebExchange webRequest) { | ||
return Mono.just(executionInput); | ||
} | ||
} |
This file contains hidden or 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
62 changes: 62 additions & 0 deletions
62
...ux/src/test/java/graphql/spring/web/reactive/components/DefaultGraphQLInvocationTest.java
This file contains hidden or 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,62 @@ | ||
package graphql.spring.web.reactive.components; | ||
|
||
import graphql.ExecutionInput; | ||
import graphql.ExecutionResult; | ||
import graphql.GraphQL; | ||
import graphql.spring.web.reactive.ExecutionInputCustomizer; | ||
import graphql.spring.web.reactive.GraphQLInvocationData; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import static java.util.concurrent.CompletableFuture.completedFuture; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class DefaultGraphQLInvocationTest { | ||
|
||
|
||
@Test | ||
public void testCustomizerIsCalled() { | ||
|
||
String query = "query myQuery {foo}"; | ||
String operationName = "myQuery"; | ||
Map<String, Object> variables = new LinkedHashMap<>(); | ||
|
||
DefaultGraphQLInvocation defaultGraphQLInvocation = new DefaultGraphQLInvocation(); | ||
ExecutionInputCustomizer executionInputCustomizer = mock(ExecutionInputCustomizer.class); | ||
defaultGraphQLInvocation.executionInputCustomizer = executionInputCustomizer; | ||
GraphQL graphQL = mock(GraphQL.class); | ||
defaultGraphQLInvocation.graphQL = graphQL; | ||
ExecutionResult executionResult = mock(ExecutionResult.class); | ||
when(graphQL.executeAsync(any(ExecutionInput.class))).thenReturn(completedFuture(executionResult)); | ||
|
||
GraphQLInvocationData graphQLInvocationData = new GraphQLInvocationData(query, operationName, variables); | ||
ServerWebExchange serverWebExchange = mock(ServerWebExchange.class); | ||
|
||
ArgumentCaptor<ExecutionInput> captor1 = ArgumentCaptor.forClass(ExecutionInput.class); | ||
ArgumentCaptor<ServerWebExchange> captor2 = ArgumentCaptor.forClass(ServerWebExchange.class); | ||
ExecutionInput executionInputResult = mock(ExecutionInput.class); | ||
when(executionInputCustomizer.customizeExecutionInput(captor1.capture(), captor2.capture())).thenReturn(Mono.just(executionInputResult)); | ||
|
||
Mono<ExecutionResult> invoke = defaultGraphQLInvocation.invoke(graphQLInvocationData, serverWebExchange); | ||
|
||
assertThat(captor1.getValue().getQuery()).isEqualTo(query); | ||
assertThat(captor1.getValue().getOperationName()).isEqualTo(operationName); | ||
assertThat(captor1.getValue().getVariables()).isSameAs(variables); | ||
|
||
invoke.block(); | ||
|
||
verify(graphQL).executeAsync(executionInputResult); | ||
|
||
} | ||
|
||
|
||
} |
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
...java-spring-webmvc/src/main/java/graphql/spring/web/servlet/ExecutionInputCustomizer.java
This file contains hidden or 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,20 @@ | ||
package graphql.spring.web.servlet; | ||
|
||
import graphql.ExecutionInput; | ||
import graphql.PublicApi; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Lets you customize the #ExecutionInput before the query is executed. | ||
* You can for example set a context object or define a root value. | ||
* <p> | ||
* This is only used if you use the default {@link GraphQLInvocation}. | ||
*/ | ||
@PublicApi | ||
public interface ExecutionInputCustomizer { | ||
|
||
CompletableFuture<ExecutionInput> customizeExecutionInput(ExecutionInput executionInput, WebRequest webRequest); | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
.../src/main/java/graphql/spring/web/servlet/components/DefaultExecutionInputCustomizer.java
This file contains hidden or 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,19 @@ | ||
package graphql.spring.web.servlet.components; | ||
|
||
import graphql.ExecutionInput; | ||
import graphql.Internal; | ||
import graphql.spring.web.servlet.ExecutionInputCustomizer; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Component | ||
@Internal | ||
public class DefaultExecutionInputCustomizer implements ExecutionInputCustomizer { | ||
|
||
@Override | ||
public CompletableFuture<ExecutionInput> customizeExecutionInput(ExecutionInput executionInput, WebRequest webRequest) { | ||
return CompletableFuture.completedFuture(executionInput); | ||
} | ||
} |
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
...mvc/src/test/java/graphql/spring/web/servlet/components/DefaultGraphQLInvocationTest.java
This file contains hidden or 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,59 @@ | ||
package graphql.spring.web.servlet.components; | ||
|
||
import graphql.ExecutionInput; | ||
import graphql.ExecutionResult; | ||
import graphql.GraphQL; | ||
import graphql.spring.web.servlet.ExecutionInputCustomizer; | ||
import graphql.spring.web.servlet.GraphQLInvocationData; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static java.util.concurrent.CompletableFuture.completedFuture; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class DefaultGraphQLInvocationTest { | ||
|
||
|
||
@Test | ||
public void testCustomizerIsCalled() { | ||
|
||
String query = "query myQuery {foo}"; | ||
String operationName = "myQuery"; | ||
Map<String, Object> variables = new LinkedHashMap<>(); | ||
|
||
DefaultGraphQLInvocation defaultGraphQLInvocation = new DefaultGraphQLInvocation(); | ||
ExecutionInputCustomizer executionInputCustomizer = mock(ExecutionInputCustomizer.class); | ||
defaultGraphQLInvocation.executionInputCustomizer = executionInputCustomizer; | ||
GraphQL graphQL = mock(GraphQL.class); | ||
defaultGraphQLInvocation.graphQL = graphQL; | ||
ExecutionResult executionResult = mock(ExecutionResult.class); | ||
when(graphQL.executeAsync(any(ExecutionInput.class))).thenReturn(completedFuture(executionResult)); | ||
|
||
GraphQLInvocationData graphQLInvocationData = new GraphQLInvocationData(query, operationName, variables); | ||
WebRequest webRequest = mock(WebRequest.class); | ||
|
||
ArgumentCaptor<ExecutionInput> captor1 = ArgumentCaptor.forClass(ExecutionInput.class); | ||
ArgumentCaptor<WebRequest> captor2 = ArgumentCaptor.forClass(WebRequest.class); | ||
ExecutionInput executionInputResult = mock(ExecutionInput.class); | ||
when(executionInputCustomizer.customizeExecutionInput(captor1.capture(), captor2.capture())).thenReturn(completedFuture(executionInputResult)); | ||
|
||
CompletableFuture<ExecutionResult> invoke = defaultGraphQLInvocation.invoke(graphQLInvocationData, webRequest); | ||
|
||
assertThat(captor1.getValue().getQuery()).isEqualTo(query); | ||
assertThat(captor1.getValue().getOperationName()).isEqualTo(operationName); | ||
assertThat(captor1.getValue().getVariables()).isSameAs(variables); | ||
|
||
verify(graphQL).executeAsync(executionInputResult); | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.