Skip to content

Commit ccefffc

Browse files
Himanshu SharmaHimanshu Sharma
authored andcommitted
Added java changes under Default DataFetchers
1 parent 1a6ad38 commit ccefffc

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/pages/tutorials/getting-started-with-spring-boot.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,28 @@ We only implement two `DataFetchers`. As mentioned above, if you don't specify o
337337

338338
A `PropertyDataFetcher` tries to lookup a property on a Java object in multiple ways. In case of a `java.util.Map` it simply looks up the property by key. This works perfectly fine for us because the keys of the book and author Maps are the same as the fields specified in the schema. For example in the schema we define for the Book type the field `pageCount` and the book `DataFetcher` returns a `Map` with a key `pageCount`. Because the field name is the same as the key in the `Map`("pageCount") the `PropertyDateFetcher` works for us.
339339

340-
Lets assume for a second we have a mismatch and the book `Map` has a key `totalPages` instead of `pageCount`. This would result in a `null` value for `pageCount` for every book, because the `PropertyDataFetcher` can't fetch the right value. In order to fix that you would have to register a new `DataFetcher` for `Book.pageCount` which looks like this:
340+
Lets assume for a second we have a mismatch and the book `Map` has a key `totalPages` instead of `pageCount`.
341+
342+
```java
343+
// In the GraphQLDataFetchers class
344+
// Rename key from 'pageCount' to 'totalPages'
345+
private static List<Map<String, String>> books = Arrays.asList(
346+
ImmutableMap.of("id", "book-1",
347+
"name", "Harry Potter and the Philosopher's Stone",
348+
"totalPages", "223",
349+
"authorId", "author-1"),
350+
ImmutableMap.of("id", "book-2",
351+
"name", "Moby Dick",
352+
"totalPages", "635",
353+
"authorId", "author-2"),
354+
ImmutableMap.of("id", "book-3",
355+
"name", "Interview with the vampire",
356+
"totalPages", "371",
357+
"authorId", "author-3")
358+
);
359+
```
360+
361+
This would result in a `null` value for `pageCount` for every book, because the `PropertyDataFetcher` can't fetch the right value. In order to fix that you would have to register a new `DataFetcher` for `Book.pageCount` which looks like this:
341362

342363
```java
343364
// In the GraphQLProvider class

0 commit comments

Comments
 (0)