From 194126dc6bb359b437f040e8bda94a90cbea475c Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Thu, 17 Feb 2022 14:42:06 +0000 Subject: [PATCH] Polishing contribution Closes gh-233 --- .../src/docs/asciidoc/attributes.adoc | 1 + .../src/docs/asciidoc/index.adoc | 52 +++++++------------ 2 files changed, 20 insertions(+), 33 deletions(-) diff --git a/spring-graphql-docs/src/docs/asciidoc/attributes.adoc b/spring-graphql-docs/src/docs/asciidoc/attributes.adoc index f41e321d..c27d5c5d 100644 --- a/spring-graphql-docs/src/docs/asciidoc/attributes.adoc +++ b/spring-graphql-docs/src/docs/asciidoc/attributes.adoc @@ -13,6 +13,7 @@ :github-issues: https://github.com/{github-repo}/issues/ :github-main-branch: https://github.com/{github-repo}/tree/main :github-wiki: https://github.com/{github-repo}/wiki +:graphql-java-docs: https://www.graphql-java.com/documentation/v16 :javadoc: https://docs.spring.io/spring-graphql/docs/{spring-graphql-version}/api :spring-framework-ref-docs: https://docs.spring.io/spring-framework/docs/current/reference/html diff --git a/spring-graphql-docs/src/docs/asciidoc/index.adoc b/spring-graphql-docs/src/docs/asciidoc/index.adoc index d3e072ff..a617b180 100644 --- a/spring-graphql-docs/src/docs/asciidoc/index.adoc +++ b/spring-graphql-docs/src/docs/asciidoc/index.adoc @@ -302,47 +302,33 @@ option to configure a name extracting function along with `Class` to GraphQL Obj name mappings that should help to cover more corner cases. +[[execution-graphqlsource-operation-caching]] +==== Operation Caching -[[execution-graphqlsource-preparsed-document-provider]] -==== PreparsedDocumentProvider +GraphQL Java must _parse_ and _validate_ an operation before executing it. This may impact +performance significantly. To avoid the need to re-parse and validate, an application may +configure a `PreparsedDocumentProvider` that caches and reuses Document instances. The +{graphql-java-docs}/execution/#query-caching[GraphQL Java docs] provide more details on +query caching through a `PreparsedDocumentProvider`. -Before operations can be executed by GraphQL Java, their request string must be _parsed_ and _validated_. These -two steps may impact the performance of applications significantly. - -You may configure a `PreparsedDocumentProvider` using `GraphQlSource.Builder#configureGraphQl`. The -`PreparsedDocumentProvider` can intercept these two steps and gives library consumers the tools to -cache, or modify the resulting operation. - -The following snippet uses https://github.com/ben-manes/caffeine[Caffeine] to build a `PreparsedDocumentProvider` -which caches the 2500 most recent operations for a maximum of 1 hour: +In Spring GraphQL you can register a `PreparsedDocumentProvider` through +`GraphQlSource.Builder#configureGraphQl`: +. [source,java,indent=0,subs="verbatim,quotes"] ---- -public class CachingPreparsedDocumentProvider implements PreparsedDocumentProvider { +// Typically, accessed through Spring Boot's GraphQlSourceBuilderCustomizer +GraphQlSource.Builder builder = ... - private final Cache cache = Caffeine - .newBuilder() - .maximumSize(2500) - .build(); +// Create provider +PreparsedDocumentProvider provider = ... - @Override - public PreparsedDocumentEntry getDocument(ExecutionInput executionInput, - Function parseAndValidateFunction) { - return cache.get(executionInput.getQuery(), operationKey -> parseAndValidateFunction.apply(executionInput)); - } - -} +builder.schemaResources(..) + .configureRuntimeWiring(..) + .configureGraphQl(graphQLBuilder -> graphQLBuilder.preparsedDocumentProvider(provider)) ---- -Please note that caching in the preceding snippet only works when you parameterize your operation using variables: -[source,graphql,indent=0,subs="verbatim,quotes"] ----- -query HelloTo($to: String!) { - sayHello(to: $to) { - greeting - } -} ----- + [[execution-reactive-datafetcher]] === Reactive `DataFetcher` @@ -466,7 +452,7 @@ problem. GraphQL Java provides a `DataLoader` mechanism for batch loading of related entities. You can find the full details in the -https://www.graphql-java.com/documentation/v16/batching/[GraphQL Java docs]. Below is a +{graphql-java-docs}/batching/[GraphQL Java docs]. Below is a summary of how it works: 1. Register ``DataLoader``'s in the `DataLoaderRegistry` that can load entities, given unique keys.