Skip to content

Commit 3c97533

Browse files
committed
Document use of local context in Controllers
See gh-1167
1 parent acb1828 commit 3c97533

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

spring-graphql-docs/modules/ROOT/pages/controllers.adoc

+13-2
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ You can write a controller like this:
273273
[source,java,indent=0,subs="verbatim,quotes"]
274274
----
275275
@Controller
276-
public class BookController {
276+
public class ActivityController {
277277
278278
@QueryMapping
279279
public List<Activity> activities() {
@@ -293,7 +293,7 @@ If necessary, you can take over the mapping for individual subtypes:
293293
[source,java,indent=0,subs="verbatim,quotes"]
294294
----
295295
@Controller
296-
public class BookController {
296+
public class ActivityController {
297297
298298
@QueryMapping
299299
public List<Activity> activities() {
@@ -658,6 +658,17 @@ https://github.com/spring-projects/spring-graphql/issues/344#issuecomment-108281
658658
for links to relevant issues and a suggested workaround.
659659
====
660660

661+
[[controllers.schema-mapping.localcontext]]
662+
=== Local Context
663+
664+
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.
665+
There are times when you would like to pass additional information to child fields data fetchers and avoid polluting the main context.
666+
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].
668+
669+
Controller methods can contribute a local context by returning a `DataFetcherResult<T>` that holds the resolved data and the new context:
670+
671+
include-code::LocalContextBookController[tag=localcontext,indent=0]
661672

662673

663674
[[controllers.batch-mapping]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2020-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.docs.controllers.schemamapping.localcontext;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import graphql.GraphQLContext;
23+
import graphql.execution.DataFetcherResult;
24+
25+
import org.springframework.graphql.data.method.annotation.Argument;
26+
import org.springframework.graphql.data.method.annotation.LocalContextValue;
27+
import org.springframework.graphql.data.method.annotation.QueryMapping;
28+
import org.springframework.graphql.data.method.annotation.SchemaMapping;
29+
import org.springframework.stereotype.Controller;
30+
31+
// tag::localcontext[]
32+
@Controller
33+
public class LocalContextBookController {
34+
35+
@QueryMapping
36+
public DataFetcherResult<Book> bookById(@Argument Long id) {
37+
// Our controller method must return a DataFetcherResult
38+
DataFetcherResult.Builder<Book> resultBuilder = DataFetcherResult.newResult();
39+
BookAndAuthor bookAndAuthor = this.fetchBookAndAuthorById(id);
40+
41+
// Create a new local context and store the author value
42+
GraphQLContext localContext = GraphQLContext.getDefault()
43+
.put("author", bookAndAuthor.author);
44+
return resultBuilder
45+
.data(bookAndAuthor.book)
46+
.localContext(localContext)
47+
.build();
48+
}
49+
50+
@SchemaMapping
51+
public List<Book> related(Book book, @LocalContextValue Author author) {
52+
List<Book> relatedBooks = new ArrayList<>();
53+
relatedBooks.addAll(fetchBooksByAuthor(author));
54+
relatedBooks.addAll(fetchSimilarBooks(book));
55+
return relatedBooks;
56+
}
57+
58+
// end::localcontext[]
59+
60+
private BookAndAuthor fetchBookAndAuthorById(Long id) {
61+
return new BookAndAuthor(new Book(id, "Spring for GraphQL", 12L),
62+
new Author(1L, "Jane Doe"));
63+
}
64+
65+
private List<Book> fetchBooksByAuthor(Author author) {
66+
return List.of(new Book(1, "Spring for GraphQL", 12L));
67+
}
68+
69+
private List<Book> fetchSimilarBooks(Book book) {
70+
return List.of();
71+
}
72+
73+
record BookAndAuthor(Book book, Author author) {}
74+
75+
record Book(long id, String title, long authorId) {}
76+
77+
record Author(long id, String name) {}
78+
79+
}

0 commit comments

Comments
 (0)