Repository navigation
Allow specification of mutation strategy #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dminkovsky
merged 1 commit into
graphql-java:master
from
dminkovsky:custom-mutation-strategy
Dec 19, 2016
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,19 +17,17 @@ | |
| public class Execution { | ||
|
|
||
| private FieldCollector fieldCollector = new FieldCollector(); | ||
| private ExecutionStrategy strategy; | ||
| private ExecutionStrategy queryStrategy; | ||
| private ExecutionStrategy mutationStrategy; | ||
|
|
||
| public Execution(ExecutionStrategy strategy) { | ||
| this.strategy = strategy; | ||
|
|
||
| if (this.strategy == null) { | ||
| this.strategy = new SimpleExecutionStrategy(); | ||
| } | ||
| public Execution(ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy) { | ||
| this.queryStrategy = queryStrategy != null ? queryStrategy : new SimpleExecutionStrategy(); | ||
| this.mutationStrategy = mutationStrategy != null ? mutationStrategy : new SimpleExecutionStrategy(); | ||
| } | ||
|
|
||
| public ExecutionResult execute(GraphQLSchema graphQLSchema, Object root, Document document, String operationName, Map<String, Object> args) { | ||
| ExecutionContextBuilder executionContextBuilder = new ExecutionContextBuilder(new ValuesResolver()); | ||
| ExecutionContext executionContext = executionContextBuilder.build(graphQLSchema, strategy, root, document, operationName, args); | ||
| ExecutionContext executionContext = executionContextBuilder.build(graphQLSchema, queryStrategy, mutationStrategy, root, document, operationName, args); | ||
| return executeOperation(executionContext, root, executionContext.getOperationDefinition()); | ||
| } | ||
|
|
||
|
|
@@ -55,9 +53,9 @@ private ExecutionResult executeOperation( | |
| fieldCollector.collectFields(executionContext, operationRootType, operationDefinition.getSelectionSet(), new ArrayList<String>(), fields); | ||
|
|
||
| if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) { | ||
| return new SimpleExecutionStrategy().execute(executionContext, operationRootType, root, fields); | ||
| return mutationStrategy.execute(executionContext, operationRootType, root, fields); | ||
|
Contributor
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. 👍 |
||
| } else { | ||
| return strategy.execute(executionContext, operationRootType, root, fields); | ||
| return queryStrategy.execute(executionContext, operationRootType, root, fields); | ||
| } | ||
| } | ||
| } | ||
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
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
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,56 @@ | ||
| package graphql.execution | ||
|
|
||
| import graphql.MutationSchema | ||
| import graphql.parser.Parser | ||
| import spock.lang.Specification | ||
|
|
||
| class ExecutionTest extends Specification { | ||
|
|
||
| def parser = new Parser() | ||
| def mutationStrategy = Mock(ExecutionStrategy) | ||
| def queryStrategy = Mock(ExecutionStrategy) | ||
| def execution = new Execution(queryStrategy, mutationStrategy) | ||
|
|
||
| def "query strategy is used for query requests"() { | ||
| given: | ||
| def mutationStrategy = Mock(ExecutionStrategy) | ||
|
|
||
| def queryStrategy = Mock(ExecutionStrategy) | ||
| def execution = new Execution(queryStrategy, mutationStrategy) | ||
|
|
||
| def query = ''' | ||
| query { | ||
| numberHolder { | ||
| theNumber | ||
| } | ||
| } | ||
| ''' | ||
| def document = parser.parseDocument(query) | ||
|
|
||
| when: | ||
| execution.execute(MutationSchema.schema, null, document, null, null) | ||
|
|
||
| then: | ||
| 1 * queryStrategy.execute(*_) | ||
| 0 * mutationStrategy.execute(*_) | ||
| } | ||
|
|
||
| def "mutation strategy is used for mutation requests"() { | ||
| given: | ||
| def query = ''' | ||
| mutation { | ||
| changeTheNumber(newNumber: 1) { | ||
| theNumber | ||
| } | ||
| } | ||
| ''' | ||
| def document = parser.parseDocument(query) | ||
|
|
||
| when: | ||
| execution.execute(MutationSchema.schema, null, document, null, null) | ||
|
|
||
| then: | ||
| 0 * queryStrategy.execute(*_) | ||
| 1 * mutationStrategy.execute(*_) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I wonder if this should be deprecated with the new change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the cod should have a Builder of GraphQL objects so it can be extended more easily over time.
I have a PR in that adds an "Instrumentation SPI" and this is YET another example adding extra aspects to the GrapQL object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out @IamCornholio. Going to leave this for now, and consider @bbakerman's builder PR. Thanks