Skip to content

Commit ab34aa4

Browse files
committed
tutorial wip
1 parent 49b5078 commit ab34aa4

File tree

2 files changed

+101
-5
lines changed

2 files changed

+101
-5
lines changed

content/blog/getting-started-with-spring-boot.md

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ With GraphQL you send the following query to server to get the details for the b
2020

2121
{{< highlight scala "linenos=table" >}}
2222
{
23-
bookById(id: "123"){
23+
bookById(id: "book-1"){
2424
id
2525
name
2626
pageCount
@@ -39,11 +39,11 @@ But the response is normal JSON:
3939
{
4040
"bookById":
4141
{
42-
"id":"123",
42+
"id":"book-1",
4343
"name":"Harry Potter and the Philosopher's Stone",
4444
"pageCount":223,
4545
"author": {
46-
"firstName":"J. K.",
46+
"firstName":"Joanne",
4747
"lastName":"Rowling"
4848
}
4949
}
@@ -248,12 +248,108 @@ public interface DataFetcher<T> {
248248

249249
Important: **Every** field from the schema has a `DataFetcher` associated with. If you don't specify any `DataFetcher` for a specific field the default `PropertyDataFetcher` is used.
250250

251-
We are creating a new class `GraphQLDataFetchers
251+
We are creating a new class `GraphQLDataFetchers` which contains a sample list of books and authors.
252252

253+
The full implementation looks like that and we will look at it in detail:
253254

254255

255-
# Testing the API
256+
{{< highlight java "linenos=table" >}}
257+
@Component
258+
public class GraphQLDataFetchers {
259+
260+
private static List<Map<String, String>> books = Arrays.asList(
261+
ImmutableMap.of("id", "book-1",
262+
"name", "Moby Dick",
263+
"pageCount", "635",
264+
"authorId", "author-1"),
265+
ImmutableMap.of("id", "book-2",
266+
"name", "Harry Potter and the Philosopher's Stone",
267+
"pageCount", "223",
268+
"authorId", "author-2"),
269+
ImmutableMap.of("id", "book-3",
270+
"name", "Interview with the vampire",
271+
"pageCount", "371",
272+
"authorId", "author-3")
273+
);
274+
275+
private static List<Map<String, String>> authors = Arrays.asList(
276+
ImmutableMap.of("id", "author-1",
277+
"firstName", "Herman",
278+
"lastName", "Melville"),
279+
ImmutableMap.of("id", "author-2",
280+
"firstName", "Joanne",
281+
"lastName", "Rowling"),
282+
ImmutableMap.of("id", "author-3",
283+
"firstName", "Anne",
284+
"lastName", "Rice")
285+
);
286+
287+
public DataFetcher getBookByIdDataFetcher() {
288+
return dataFetchingEnvironment -> {
289+
String bookId = dataFetchingEnvironment.getArgument("id");
290+
return books
291+
.stream()
292+
.filter(book -> book.get("id").equals(bookId))
293+
.findFirst()
294+
.orElse(null);
295+
};
296+
}
297+
298+
public DataFetcher getAuthorDataFetcher() {
299+
return dataFetchingEnvironment -> {
300+
Map<String,String> book = dataFetchingEnvironment.getSource();
301+
String authorId = book.get("authorId");
302+
return authors
303+
.stream()
304+
.filter(author -> author.get("id").equals(authorId))
305+
.findFirst()
306+
.orElse(null);
307+
};
308+
}
309+
}
310+
311+
{{< / highlight >}}
312+
<p/>
313+
314+
## Source of the data
315+
We are getting our books and authors from a static list inside the class. This is made just for this tutorial. It is very important to understand that GraphQL doesn't dictate in anyway where the data comes from. This is the power of GraphQL: it can come from a static in memory list, from a database or an external service
316+
317+
318+
## Book DataFetcher
319+
Our first method `getBookByIdDataFetcher` returns a `DataFetcher` implementation which takes a `DataFetcherEnvironment` and returns a book.
320+
In our case this means we need to get the `id` argument from the `bookById` field and find the book with this specific id. If we can't find it we just return null.
321+
322+
The "id" in `String bookId = dataFetchingEnvironment.getArgument("id");` is the "id" from the `bookById` query field in the schema:
323+
324+
{{< highlight scala "linenos=table" >}}
325+
type Query {
326+
bookById(id: ID): Book
327+
}
328+
...
329+
{{< / highlight >}}
330+
331+
332+
## Author DataFetcher
333+
Our second method `getAuthorDataFetcher` returns a `DataFetcher` for getting the author for a specific book.
334+
The difference to the book `DataFetcher` is that we don't have an argument, but we have a book instance.
335+
The result of the `DataFetcher` from the parent field is made available via `getSource`. This is an important concept to understand: the `DataFetcher` for each field in GraphQL are called from top to down and the previous result is the `source` property of the `DataFetcherEnvironment`.
336+
337+
We then use the previously fetched book to get the `authorId` and then look for that specific author in the same way we look for a specific book.
338+
339+
340+
# Try our the API
341+
This is all needed to actually build a working GraphQL API. After the starting the Spring Boot application the API is available on `http://localhost:8080/graphql`.
342+
343+
The easiest way to try out and explore a GraphQL API is to use a tool like [GraphQL Playground](https://github.com/prisma/graphql-playground). Download it and run it.
344+
345+
After starting it you will be asked for a URL, enter "http://localhost:8080/graphql".
346+
347+
After that you can query our example API and you should get back the result we mentioned in the beginning. It should look like this:
348+
349+
![GraphQL Playground](/images/playground.png)
256350

257351

258352
# Complete example source code
259353

354+
The complete project with the full source code can be found here: https://github.com/graphql-java/tutorials/tree/master/book-details
355+

static/images/playground.png

183 KB
Loading

0 commit comments

Comments
 (0)