Skip to content
Merged
17 changes: 11 additions & 6 deletions src/main/java/graphql/GraphQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,17 @@ private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInpu
executionInputRef.set(transformedInput);
return parseAndValidate(executionInputRef, graphQLSchema, instrumentationState);
};
PreparsedDocumentEntry preparsedDoc = preparsedDocumentProvider.getDocument(executionInput, computeFunction);
if (preparsedDoc.hasErrors()) {
return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDoc.getErrors()));
}

return execute(executionInputRef.get(), preparsedDoc.getDocument(), graphQLSchema, instrumentationState);
CompletableFuture<PreparsedDocumentEntry> preparsedDoc = preparsedDocumentProvider.getDocumentAsync(executionInput, computeFunction);
return preparsedDoc.thenCompose(preparsedDocumentEntry -> {
if (preparsedDocumentEntry.hasErrors()) {
return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDocumentEntry.getErrors()));
}
try {
return execute(executionInputRef.get(), preparsedDocumentEntry.getDocument(), graphQLSchema, instrumentationState);
} catch (AbortExecutionException e){
return CompletableFuture.completedFuture(e.toExecutionResult());
}
});
}

private PreparsedDocumentEntry parseAndValidate(AtomicReference<ExecutionInput> executionInputRef, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import graphql.ExecutionInput;
import graphql.PublicSpi;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

/**
Expand All @@ -22,8 +23,27 @@ public interface PreparsedDocumentProvider {
* @param executionInput The {@link graphql.ExecutionInput} containing the query
* @param parseAndValidateFunction If the query has not be pre-parsed, this function MUST be called to parse and validate it
* @return an instance of {@link PreparsedDocumentEntry}
* <p>
* @deprecated - use {@link #getDocumentAsync(ExecutionInput executionInput, Function parseAndValidateFunction)}
*/
@Deprecated
PreparsedDocumentEntry getDocument(ExecutionInput executionInput, Function<ExecutionInput, PreparsedDocumentEntry> parseAndValidateFunction);

/**
* This is called to get a "cached" pre-parsed query and if its not present, then the "parseAndValidateFunction"
* can be called to parse and validate the query.
* <p>
* Note - the "parseAndValidateFunction" MUST be called if you dont have a per parsed version of the query because it not only parses
* and validates the query, it invokes {@link graphql.execution.instrumentation.Instrumentation} calls as well for parsing and validation.
* if you dont make a call back on this then these wont happen.
*
* @param executionInput The {@link graphql.ExecutionInput} containing the query
* @param parseAndValidateFunction If the query has not be pre-parsed, this function MUST be called to parse and validate it
* @return a promise to an {@link PreparsedDocumentEntry}
*/
default CompletableFuture<PreparsedDocumentEntry> getDocumentAsync(ExecutionInput executionInput, Function<ExecutionInput, PreparsedDocumentEntry> parseAndValidateFunction) {
return CompletableFuture.completedFuture(getDocument(executionInput, parseAndValidateFunction));
}
Comment thread
heoYH marked this conversation as resolved.
Resolved
}


Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import graphql.PublicSpi;
import graphql.execution.preparsed.PreparsedDocumentEntry;

import java.util.concurrent.CompletableFuture;

/**
* This interface is used to abstract an actual cache that can cache parsed persistent queries.
*/
Expand All @@ -26,6 +28,30 @@ public interface PersistedQueryCache {
* @param onCacheMiss the call back should it be a valid query id but its not currently not in the cache
* @return a parsed and validated PreparsedDocumentEntry where {@link graphql.execution.preparsed.PreparsedDocumentEntry#getDocument()} is set
* @throws graphql.execution.preparsed.persisted.PersistedQueryNotFound if the query id is not know at all and you have no query text
*
* @deprecated - use {@link #getPersistedQueryDocumentAsync(Object persistedQueryId, ExecutionInput executionInput, PersistedQueryCacheMiss onCacheMiss)}
*/
@Deprecated
PreparsedDocumentEntry getPersistedQueryDocument(Object persistedQueryId, ExecutionInput executionInput, PersistedQueryCacheMiss onCacheMiss) throws PersistedQueryNotFound;

/**
* This is called to get a persisted query from cache.
* <p>
* If its present in cache then it must return a PreparsedDocumentEntry where {@link graphql.execution.preparsed.PreparsedDocumentEntry#getDocument()}
* is already parsed and validated. This will be passed onto the graphql engine as is.
* <p>
* If its a valid query id but its no present in cache, (cache miss) then you need to call back the "onCacheMiss" function with associated query text.
* This will be compiled and validated by the graphql engine and the a PreparsedDocumentEntry will be passed back ready for you to cache it.
* <p>
* If its not a valid query id then throw a {@link graphql.execution.preparsed.persisted.PersistedQueryNotFound} to indicate this.
*
* @param persistedQueryId the persisted query id
* @param executionInput the original execution input
* @param onCacheMiss the call back should it be a valid query id but its not currently not in the cache
* @return a promise to parsed and validated {@link PreparsedDocumentEntry} where {@link graphql.execution.preparsed.PreparsedDocumentEntry#getDocument()} is set
* @throws graphql.execution.preparsed.persisted.PersistedQueryNotFound if the query id is not know at all and you have no query text
*/
default CompletableFuture<PreparsedDocumentEntry> getPersistedQueryDocumentAsync(Object persistedQueryId, ExecutionInput executionInput, PersistedQueryCacheMiss onCacheMiss) throws PersistedQueryNotFound{
return CompletableFuture.completedFuture(getPersistedQueryDocument(persistedQueryId, executionInput, onCacheMiss));
}
Comment thread
heoYH marked this conversation as resolved.
Resolved
}
16 changes: 11 additions & 5 deletions src/main/java/graphql/nextgen/GraphQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,17 @@ private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInpu
executionInputRef.set(transformedInput);
return parseAndValidate(executionInputRef, graphQLSchema, instrumentationState);
};
PreparsedDocumentEntry preparsedDoc = preparsedDocumentProvider.getDocument(executionInput, computeFunction);
if (preparsedDoc.hasErrors()) {
return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDoc.getErrors()));
}
return execute(executionInputRef.get(), preparsedDoc.getDocument(), graphQLSchema, instrumentationState);
CompletableFuture<PreparsedDocumentEntry> preparsedDoc = preparsedDocumentProvider.getDocumentAsync(executionInput, computeFunction);
return preparsedDoc.thenCompose(preparsedDocumentEntry -> {
if (preparsedDocumentEntry.hasErrors()) {
return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDocumentEntry.getErrors()));
}
try {
return execute(executionInputRef.get(), preparsedDocumentEntry.getDocument(), graphQLSchema, instrumentationState);
} catch (AbortExecutionException e) {
return CompletableFuture.completedFuture(e.toExecutionResult());
}
});
}

private PreparsedDocumentEntry parseAndValidate(AtomicReference<ExecutionInput> executionInputRef, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import graphql.execution.instrumentation.SimpleInstrumentation
import graphql.execution.instrumentation.TestingInstrumentation
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters
import graphql.language.Document
import graphql.parser.Parser
import spock.lang.Specification

import java.util.function.Function

import static graphql.ExecutionInput.newExecutionInput

class PreparsedDocumentProviderTest extends Specification {

def 'Preparse of simple serial execution'() {
Expand Down Expand Up @@ -220,4 +223,28 @@ class PreparsedDocumentProviderTest extends Specification {
resultB.data == [hero: [name: "R2-D2"]]
instrumentationB.capturedInput.getQuery() == queryB
}

def "sync method and async method result is same"() {
given:
def provider = new TestingPreparsedDocumentProvider()
def queryA = """
query A {
hero {
id
}
}
"""
def engineParser = {
ExecutionInput ei ->
def doc = new Parser().parseDocument(ei.getQuery())
return new PreparsedDocumentEntry(doc)
}
when:
def syncMethod = provider.getDocument(newExecutionInput(queryA).build(), engineParser)
def asyncMethod = provider.getDocumentAsync(newExecutionInput(queryA).build(), engineParser)

then:
asyncMethod != null
asyncMethod.get().equals(syncMethod)
}
}