Skip to content

Commit a9cd0a9

Browse files
jord1erstoyanchev
authored andcommitted
Update docs for PreparsedDocumentSupport
See gh-233
1 parent 1f9afa9 commit a9cd0a9

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

spring-graphql-docs/src/docs/asciidoc/index.adoc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,46 @@ name mappings that should help to cover more corner cases.
285285

286286

287287

288+
[[execution-graphqlsource-preparsed-document-provider]]
289+
==== PreparsedDocumentProvider
290+
291+
Before operations can be executed by GraphQL Java, their request string must be _parsed_ and _validated_. These
292+
two steps may impact the performance of applications significantly.
293+
294+
You may configure a `PreparsedDocumentProvider` using `GraphQlSource.Builder#configureGraphQl`. The
295+
`PreparsedDocumentProvider` can intercept these two steps and gives library consumers the tools to
296+
cache, or modify the resulting operation.
297+
298+
The following snippet uses https://github.com/ben-manes/caffeine[Caffeine] to build a `PreparsedDocumentProvider`
299+
which caches the 2500 most recent operations for a maximum of 1 hour:
300+
301+
[source,java,indent=0,subs="verbatim,quotes"]
302+
----
303+
public class CachingPreparsedDocumentProvider implements PreparsedDocumentProvider {
304+
305+
private final Cache<String, PreparsedDocumentEntry> cache = Caffeine
306+
.newBuilder()
307+
.maximumSize(2500)
308+
.build();
309+
310+
@Override
311+
public PreparsedDocumentEntry getDocument(ExecutionInput executionInput,
312+
Function<ExecutionInput, PreparsedDocumentEntry> parseAndValidateFunction) {
313+
return cache.get(executionInput.getQuery(), operationKey -> parseAndValidateFunction.apply(executionInput));
314+
}
315+
316+
}
317+
----
318+
319+
Please note that caching in the preceding snippet only works when you parameterize your operation using variables:
320+
[source,graphql,indent=0,subs="verbatim,quotes"]
321+
----
322+
query HelloTo($to: String!) {
323+
sayHello(to: $to) {
324+
greeting
325+
}
326+
}
327+
----
288328

289329
[[execution-reactive-datafetcher]]
290330
=== Reactive `DataFetcher`

spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultGraphQlSourceBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class DefaultGraphQlSourceBuilder implements GraphQlSource.Builder {
7777

7878
private Consumer<GraphQL.Builder> graphQlConfigurers = (builder) -> {
7979
};
80-
80+
8181

8282
@Override
8383
public GraphQlSource.Builder schemaResources(Resource... resources) {

0 commit comments

Comments
 (0)