Skip to content

Commit e2f954a

Browse files
committed
Document DataFetcherResult usage in Controllers
This commit polishes the new "Local Context" section and the existing "Return Values" section to also mention `DataFetcherResult`. Closes gh-1167
1 parent 48c8885 commit e2f954a

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

Diff for: spring-graphql-docs/modules/ROOT/pages/controllers.adoc

+30-7
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,33 @@ See xref:controllers.adoc#controllers.schema-mapping.data-loader[`DataLoader`].
214214

215215
Schema mapping handler methods can return:
216216

217-
- A resolved value of any type.
218-
- `Mono` and `Flux` for asynchronous value(s). Supported for controller methods and for
219-
any `DataFetcher` as described in xref:request-execution.adoc#execution.reactive-datafetcher[Reactive `DataFetcher`].
220-
- Kotlin coroutine and `Flow` are adapted to `Mono` and `Flux`.
221-
- `java.util.concurrent.Callable` to have the value(s) produced asynchronously.
222-
For this to work, `AnnotatedControllerConfigurer` must be configured with an `Executor`.
217+
[cols="1,2"]
218+
|===
219+
| Return Value | Description
220+
221+
| A resolved value `T`
222+
| Any application type directly resolved.
223+
224+
| `Mono<T>` and `Flux<T>`
225+
| For asynchronous value(s).
226+
227+
Supported for controller methods and for any `DataFetcher` as described in xref:request-execution.adoc#execution.reactive-datafetcher[Reactive `DataFetcher`].
228+
229+
| Kotlin `suspend fun` and `Flow`
230+
| They are automatically adapted to `Mono` and `Flux`.
231+
232+
| `java.util.concurrent.Callable<T>`
233+
| For producing value(s) asynchronously.
234+
For this to work, `AnnotatedControllerConfigurer` must be configured with an `Executor`.
235+
On Java 21+, returning `T` directly is enough. Read the paragraph after this table for more details.
236+
237+
| `graphql.execution.DataFetcherResult<P>`
238+
| With `P` being any of the types listed above (`T`, `Mono<T>`, etc.).
239+
240+
This is the "full" GraphQL Java return value, containing not only the "data".
241+
Useful for completing the result with "extensions" or a xref:controllers.adoc#controllers.schema-mapping.localcontext["Local Context"].
242+
243+
|===
223244

224245
On Java 21+, when `AnnotatedControllerConfigurer` is configured with an `Executor`, controller
225246
methods with a blocking method signature are invoked asynchronously. By default, a controller
@@ -664,12 +685,14 @@ for links to relevant issues and a suggested workaround.
664685
The main `GraphQlContext` is global for the entire query and can be used to store and retrieve cross-cutting context data for observability, security and more.
665686
There are times when you would like to pass additional information to child fields data fetchers and avoid polluting the main context.
666687
For such use cases, you should consider a local `GraphQLContext` as it is contained to a subset of the data fetching operations.
667-
A well-known use case is https://www.graphql-java.com/blog/deep-dive-data-fetcher-results[data pre-fetching].
668688

669689
Controller methods can contribute a local context by returning a `DataFetcherResult<T>` that holds the resolved data and the new context:
670690

671691
include-code::LocalContextBookController[tag=localcontext,indent=0]
672692

693+
If you want to see a more detailed example and discussion of using this, have a look at
694+
https://www.graphql-java.com/blog/deep-dive-data-fetcher-results[the "Building efficient data fetchers by looking ahead" section on the graphql-java documentation].
695+
673696

674697
[[controllers.batch-mapping]]
675698
== `@BatchMapping`

0 commit comments

Comments
 (0)