@@ -302,47 +302,33 @@ option to configure a name extracting function along with `Class` to GraphQL Obj
302
302
name mappings that should help to cover more corner cases.
303
303
304
304
305
+ [[execution-graphqlsource-operation-caching]]
306
+ ==== Operation Caching
305
307
306
- [[execution-graphqlsource-preparsed-document-provider]]
307
- ==== PreparsedDocumentProvider
308
+ GraphQL Java must _parse_ and _validate_ an operation before executing it. This may impact
309
+ performance significantly. To avoid the need to re-parse and validate, an application may
310
+ configure a `PreparsedDocumentProvider` that caches and reuses Document instances. The
311
+ {graphql-java-docs}/execution/#query-caching[GraphQL Java docs] provide more details on
312
+ query caching through a `PreparsedDocumentProvider`.
308
313
309
- Before operations can be executed by GraphQL Java, their request string must be _parsed_ and _validated_. These
310
- two steps may impact the performance of applications significantly.
311
-
312
- You may configure a `PreparsedDocumentProvider` using `GraphQlSource.Builder#configureGraphQl`. The
313
- `PreparsedDocumentProvider` can intercept these two steps and gives library consumers the tools to
314
- cache, or modify the resulting operation.
315
-
316
- The following snippet uses https://github.com/ben-manes/caffeine[Caffeine] to build a `PreparsedDocumentProvider`
317
- which caches the 2500 most recent operations for a maximum of 1 hour:
314
+ In Spring GraphQL you can register a `PreparsedDocumentProvider` through
315
+ `GraphQlSource.Builder#configureGraphQl`:
316
+ .
318
317
319
318
[source,java,indent=0,subs="verbatim,quotes"]
320
319
----
321
- public class CachingPreparsedDocumentProvider implements PreparsedDocumentProvider {
322
-
323
- private final Cache<String, PreparsedDocumentEntry> cache = Caffeine
324
- .newBuilder()
325
- .maximumSize(2500)
326
- .build();
320
+ // Typically, accessed through Spring Boot's GraphQlSourceBuilderCustomizer
321
+ GraphQlSource.Builder builder = ...
327
322
328
- @Override
329
- public PreparsedDocumentEntry getDocument(ExecutionInput executionInput,
330
- Function<ExecutionInput, PreparsedDocumentEntry> parseAndValidateFunction) {
331
- return cache.get(executionInput.getQuery(), operationKey -> parseAndValidateFunction.apply(executionInput));
332
- }
323
+ // Create provider
324
+ PreparsedDocumentProvider provider = ...
333
325
334
- }
326
+ builder.schemaResources(..)
327
+ .configureRuntimeWiring(..)
328
+ .configureGraphQl(graphQLBuilder -> graphQLBuilder.preparsedDocumentProvider(provider))
335
329
----
336
330
337
- Please note that caching in the preceding snippet only works when you parameterize your operation using variables:
338
- [source,graphql,indent=0,subs="verbatim,quotes"]
339
- ----
340
- query HelloTo($to: String!) {
341
- sayHello(to: $to) {
342
- greeting
343
- }
344
- }
345
- ----
331
+
346
332
347
333
[[execution-reactive-datafetcher]]
348
334
=== Reactive `DataFetcher`
@@ -466,7 +452,7 @@ problem.
466
452
467
453
GraphQL Java provides a `DataLoader` mechanism for batch loading of related entities.
468
454
You can find the full details in the
469
- https://www. graphql-java.com/documentation/v16 /batching/[GraphQL Java docs]. Below is a
455
+ { graphql-java-docs} /batching/[GraphQL Java docs]. Below is a
470
456
summary of how it works:
471
457
472
458
1. Register ``DataLoader``'s in the `DataLoaderRegistry` that can load entities, given unique keys.
0 commit comments