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
+101-5Lines changed: 101 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ With GraphQL you send the following query to server to get the details for the b
20
20
21
21
{{< highlight scala "linenos=table" >}}
22
22
{
23
-
bookById(id: "123"){
23
+
bookById(id: "book-1"){
24
24
id
25
25
name
26
26
pageCount
@@ -39,11 +39,11 @@ But the response is normal JSON:
39
39
{
40
40
"bookById":
41
41
{
42
-
"id":"123",
42
+
"id":"book-1",
43
43
"name":"Harry Potter and the Philosopher's Stone",
44
44
"pageCount":223,
45
45
"author": {
46
-
"firstName":"J. K.",
46
+
"firstName":"Joanne",
47
47
"lastName":"Rowling"
48
48
}
49
49
}
@@ -248,12 +248,108 @@ public interface DataFetcher<T> {
248
248
249
249
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.
250
250
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.
252
252
253
+
The full implementation looks like that and we will look at it in detail:
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
+

256
350
257
351
258
352
# Complete example source code
259
353
354
+
The complete project with the full source code can be found here: https://github.com/graphql-java/tutorials/tree/master/book-details
0 commit comments