You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/blog/getting-started-with-spring-boot.md
+29-1Lines changed: 29 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -337,6 +337,29 @@ This is an important concept to understand: the `DataFetcher` for each field in
337
337
338
338
We then use the previously fetched book to get the `authorId` and look for that specific author in the same way we look for a specific book.
339
339
340
+
## Default DataFetchers
341
+
We only implement two `DataFetchers`. As mentioned above, if you don't specify one, the default `PropertyDataFetcher` is used. In our case it means `Book.id`, `Book.name`, `Book.pageCount`, `Author.id`, `Author.firstName` and `Author.lastName` all have a default `PropertyDataFetcher` associated with it.
342
+
343
+
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.
344
+
345
+
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:
346
+
347
+
{{< highlight java "linenos=table" >}}
348
+
...
349
+
public DataFetcher getPageCountDataFetcher() {
350
+
return dataFetchingEnvironment -> {
351
+
Map<String,String> book = dataFetchingEnvironment.getSource();
352
+
return book.get("totalPages");
353
+
};
354
+
}
355
+
...
356
+
{{< / highlight >}}
357
+
358
+
<p/>
359
+
This `DataFetcher` would fix that problem by looking up the right key in the book `Map`.
360
+
(Again: we don't need that for our example, because we don't have naming mismatch)
361
+
362
+
340
363
# Try out the API
341
364
This is all you actually need to build a working GraphQL API. After the starting the Spring Boot application the API is available on `http://localhost:8080/graphql`.
342
365
@@ -348,7 +371,12 @@ After that, you can query our example API and you should get back the result we
348
371
349
372

350
373
351
-
# Complete example source code
374
+
# Complete example source code and more information
352
375
353
376
The complete project with the full source code can be found here: https://github.com/graphql-java/tutorials/tree/master/book-details
354
377
378
+
More information about GraphQL Java can be found in the [documentation](https://www.graphql-java.com/documentation/).
379
+
380
+
We also have [spectrum chat](https://spectrum.chat/graphql-java) for any question or problems.
0 commit comments