|
| 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