-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add normalized document provider to allow caching of normalized documents #4048
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
Closed
timward60
wants to merge
3
commits into
graphql-java:master
from
timward60:timward-normalized-document-caching
Closed
Changes from all commits
Commits
Show all changes
3 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
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 |
|---|---|---|
|
|
@@ -19,6 +19,11 @@ | |
| import graphql.language.OperationDefinition; | ||
| import graphql.normalized.ExecutableNormalizedOperation; | ||
| import graphql.normalized.ExecutableNormalizedOperationFactory; | ||
| import graphql.normalized.GraphQlNormalizedOperation; | ||
| import graphql.normalized.nf.NormalizedDocument; | ||
| import graphql.normalized.nf.NormalizedDocumentFactory; | ||
| import graphql.normalized.nf.NormalizedOperation; | ||
| import graphql.normalized.nf.provider.NormalizedDocumentProvider; | ||
| import graphql.schema.GraphQLSchema; | ||
| import graphql.util.FpKit; | ||
| import graphql.util.LockKit; | ||
|
|
@@ -30,9 +35,11 @@ | |
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
||
| @SuppressWarnings("TypeParameterUnusedInFormals") | ||
|
|
@@ -63,9 +70,10 @@ public class ExecutionContext { | |
| private final IncrementalCallState incrementalCallState = new IncrementalCallState(); | ||
| private final ValueUnboxer valueUnboxer; | ||
| private final ResponseMapFactory responseMapFactory; | ||
| private final NormalizedDocumentProvider normalizedDocumentProvider; | ||
|
|
||
| private final ExecutionInput executionInput; | ||
| private final Supplier<ExecutableNormalizedOperation> queryTree; | ||
| private final Supplier<GraphQlNormalizedOperation> queryTree; | ||
| private final boolean propagateErrorsOnNonNullContractFailure; | ||
|
|
||
| private final AtomicInteger isRunning = new AtomicInteger(0); | ||
|
|
@@ -96,11 +104,12 @@ public class ExecutionContext { | |
| this.locale = builder.locale; | ||
| this.valueUnboxer = builder.valueUnboxer; | ||
| this.responseMapFactory = builder.responseMapFactory; | ||
| this.normalizedDocumentProvider = builder.normalizedDocumentProvider; | ||
| this.errors.set(builder.errors); | ||
| this.localContext = builder.localContext; | ||
| this.executionInput = builder.executionInput; | ||
| this.dataLoaderDispatcherStrategy = builder.dataLoaderDispatcherStrategy; | ||
| this.queryTree = FpKit.interThreadMemoize(() -> ExecutableNormalizedOperationFactory.createExecutableNormalizedOperation(graphQLSchema, operationDefinition, fragmentsByName, coercedVariables)); | ||
| this.queryTree = FpKit.interThreadMemoize(() -> this. createGraphQLNormalizedOperation().join()); | ||
|
Member
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. Super nit: there's a space after |
||
| this.propagateErrorsOnNonNullContractFailure = builder.propagateErrorsOnNonNullContractFailure; | ||
| this.engineRunningState = builder.engineRunningState; | ||
| } | ||
|
|
@@ -303,6 +312,11 @@ public ResponseMapFactory getResponseMapFactory() { | |
| return responseMapFactory; | ||
| } | ||
|
|
||
| @Internal | ||
| public NormalizedDocumentProvider getNormalizedDocumentProvider() { | ||
| return normalizedDocumentProvider; | ||
| } | ||
|
|
||
| /** | ||
| * @return the total list of errors for this execution context | ||
| */ | ||
|
|
@@ -336,7 +350,7 @@ public ExecutionStrategy getStrategy(OperationDefinition.Operation operation) { | |
| } | ||
| } | ||
|
|
||
| public Supplier<ExecutableNormalizedOperation> getNormalizedQueryTree() { | ||
| public Supplier<GraphQlNormalizedOperation> getNormalizedQueryTree() { | ||
| return queryTree; | ||
| } | ||
|
|
||
|
|
@@ -368,12 +382,67 @@ public ResultNodesInfo getResultNodesInfo() { | |
| return resultNodesInfo; | ||
| } | ||
|
|
||
| private NormalizedDocument createNormalizedDocument() { | ||
| return NormalizedDocumentFactory.createNormalizedDocument(graphQLSchema, document); | ||
| } | ||
|
|
||
| private CompletableFuture<GraphQlNormalizedOperation> createGraphQLNormalizedOperation() { | ||
| // Check for experimental support for normalized documents | ||
| if (hasNormalizedDocumentSupport()) { | ||
| return createNormalizedOperation() | ||
| .thenApply(Function.identity()); // Cast to interface. | ||
| } | ||
|
|
||
| return CompletableFuture.completedFuture(createExecutableNormalizedOperation()); | ||
| } | ||
|
|
||
| @ExperimentalApi | ||
| private CompletableFuture<NormalizedOperation> createNormalizedOperation() { | ||
| return normalizedDocumentProvider.getNormalizedDocument(executionInput, this::createNormalizedDocument).thenApply(normalizedDocumentEntry -> { | ||
| var normalizedDocument = normalizedDocumentEntry.getDocument(); | ||
|
|
||
| // Search the document for the operation that matches the operationDefinition name, | ||
| // if no match then it could be anonymous query, then fallback to the first operation. | ||
| var normalizedOperations = normalizedDocument.getNormalizedOperations(); | ||
| var normalizedOperation = normalizedOperations.stream() | ||
| .filter(this::isExecutingOperation) | ||
| .findAny() | ||
| .map(NormalizedDocument.NormalizedOperationWithAssumedSkipIncludeVariables::getNormalizedOperation) | ||
| .orElseGet(normalizedDocument::getSingleNormalizedOperation); | ||
|
|
||
| return normalizedOperation; | ||
| }); | ||
| } | ||
|
|
||
| private ExecutableNormalizedOperation createExecutableNormalizedOperation() { | ||
| var executableNormalizedOperation = ExecutableNormalizedOperationFactory.createExecutableNormalizedOperation(graphQLSchema, operationDefinition, fragmentsByName, coercedVariables); | ||
|
|
||
| return executableNormalizedOperation; | ||
| } | ||
|
|
||
| private boolean isExecutingOperation(NormalizedDocument.NormalizedOperationWithAssumedSkipIncludeVariables op) { | ||
| var operation = op.getNormalizedOperation(); | ||
| var operationName = operation.getOperationName(); | ||
| var operationDefinitionName = operationDefinition.getName(); | ||
| if (operationName == null || operationDefinitionName == null) { | ||
| return false; | ||
| } | ||
|
|
||
| return operationName.equals(operationDefinitionName); | ||
| } | ||
|
|
||
| @Internal | ||
| public boolean hasIncrementalSupport() { | ||
| GraphQLContext graphqlContext = getGraphQLContext(); | ||
| return graphqlContext != null && graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT); | ||
| } | ||
|
|
||
| @Internal | ||
| private boolean hasNormalizedDocumentSupport() { | ||
| GraphQLContext graphqlContext = getGraphQLContext(); | ||
| return graphqlContext != null && graphqlContext.getBoolean(ExperimentalApi.ENABLE_NORMALIZED_DOCUMENT_SUPPORT); | ||
| } | ||
timward60 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Internal | ||
| public EngineRunningState getEngineRunningState() { | ||
| return engineRunningState; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.