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
+28-30Lines changed: 28 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,14 +7,14 @@ date = 2019-01-23
7
7
toc = "true"
8
8
+++
9
9
10
-
This is a tutorial for people who never development a GraphQL server with Java. Some Spring Boot and Java knowledge is required. While we give a brief introduction into GraphQL the focus of this tutorial is on developing a GraphQL server in Java.
10
+
This is a tutorial for people who never development a GraphQL server with Java. Some Spring Boot and Java knowledge is required. While we give a brief introduction into GraphQL, the focus of this tutorial is on developing a GraphQL server in Java.
11
11
12
12
13
13
# GraphQL in 3 minutes
14
14
15
15
GraphQL is a query language to retrieve data from a server. It is an alternative to REST, SOAP or gRPC in some way.
16
16
17
-
For example we wanna query the details for specific book from a online store backend.
17
+
For example, we wanna query the details for specific book from a online store backend.
18
18
19
19
With GraphQL you send the following query to server to get the details for the book with the id "123":
20
20
@@ -32,9 +32,13 @@ With GraphQL you send the following query to server to get the details for the b
32
32
}
33
33
{{< / highlight >}}
34
34
35
-
This is not JSON (even if it looks remotely similar), it is a GraphQL query.
35
+
This is not JSON (even if it looks remotely similar), it is a GraphQL query.
36
+
It basically says:
37
+
- query a book with a specific id
38
+
- get me the id, name, pageCount and author from that book
39
+
- for the author I want to know the firstName and lastName
36
40
37
-
But the response is normal JSON:
41
+
The response is normal JSON:
38
42
{{< highlight json "linenos=table" >}}
39
43
{
40
44
"bookById":
@@ -50,7 +54,7 @@ But the response is normal JSON:
50
54
}
51
55
{{< / highlight >}}
52
56
53
-
One very important property of GraphQL is that it is statically typed: the server knows exactly of the shape of every object you can query and any client can actually "inspect" the server and ask for the so called "schema". The schema describes what queries are possible and what fields you can get back.
57
+
One very important property of GraphQL is that it is statically typed: the server knows exactly of the shape of every object you can query and any client can actually "inspect" the server and ask for the so called "schema". The schema describes what queries are possible and what fields you can get back. (Note: when we refer to schema here, we always refer to a "GraphQL Schema", which is not related to other schemas like "JSON Schema")
54
58
55
59
The schema for the above query looks like this:
56
60
@@ -73,7 +77,7 @@ type Author {
73
77
}
74
78
{{< / highlight >}}
75
79
76
-
This tutorial will focus on how to implement a GraphQL server with schema in Java.
80
+
This tutorial will focus on how to implement a GraphQL server with exactly this schema in Java.
77
81
78
82
We barely touched GraphQL. Further information can be found on the official page: https://graphql.github.io/learn/
79
83
@@ -82,7 +86,7 @@ We barely touched GraphQL. Further information can be found on the official page
82
86
[GraphQL Java](https://www.graphql-java.com) is the Java (server) implementation for GraphQL.
83
87
The are several repositories in the GraphQL Java Github org. The most important one is the [GraphQL Java Engine](https://github.com/graphql-java/graphql-java) which is basis for everything else.
84
88
85
-
GraphQL Java Engine itself is only concerned with executing queries. It doesn't deal with any HTTP or JSON related topics. For these aspects we will use the [GraphQL Java Spring Boot](https://github.com/graphql-java/graphql-java-spring) adapter which takes care of exposing our API via Spring Boot over HTTP.
89
+
GraphQL Java Engine itself is only concerned with executing queries. It doesn't deal with any HTTP or JSON related topics. For these aspects, we will use the [GraphQL Java Spring Boot](https://github.com/graphql-java/graphql-java-spring) adapter which takes care of exposing our API via Spring Boot over HTTP.
86
90
87
91
The main steps of creating a GraphQL Java server are:
88
92
@@ -96,7 +100,7 @@ This is in no way a comprehensive API, but it is enough for this tutorial.
96
100
97
101
# Create a Spring Boot app
98
102
99
-
The easiest way to create a Spring Boot app is to use the initializr at https://start.spring.io/.
103
+
The easiest way to create a Spring Boot app is to use the "Spring Initializr" at https://start.spring.io/.
100
104
101
105
Select:
102
106
@@ -109,7 +113,7 @@ For the project metadata we use:
109
113
- Group: `com.graphql-java.tutorial`
110
114
- Artifact: `book-details`
111
115
112
-
As dependency we just select `Web`.
116
+
As dependency, we just select `Web`.
113
117
114
118
A click on `Generate Project` will give you a ready to use Spring Boot app.
115
119
All subsequently mentioned files and paths will be relative to this generated project.
@@ -154,16 +158,16 @@ type Author {
154
158
{{< / highlight >}}
155
159
<p/>
156
160
157
-
This schema defines one top level field (field in the type `Query`): `bookById` which returns the details of a specific book.
161
+
This schema defines one top level field (in the type `Query`): `bookById` which returns the details of a specific book.
158
162
159
163
It also defines the type `Book` which has the fields: `id`, `name`, `pageCount` and `author`.
160
164
`author` is of type `Author`, which is defined after `Book`.
161
165
162
-
> The Domain Specific Language shown above which is used to describe a schema is called Schema Definition Language or SDL.
166
+
> The Domain Specific Language shown above which is used to describe a schema is called Schema Definition Language or SDL. More details about it can be found [here](https://graphql.org/learn/schema/).
163
167
164
168
But so far it is just a normal text. We need to "bring it to live" by reading the file and parsing it.
165
169
166
-
We are creating a new `GraphQLProvider` class in the package `com.graphqljava.tutorial.bookdetails` with an `init` method which will create a `GraphQL` instance:
170
+
We create a new `GraphQLProvider` class in the package `com.graphqljava.tutorial.bookdetails` with an `init` method which will create a `GraphQL` instance:
167
171
168
172
{{< highlight java "linenos=table" >}}
169
173
@@ -192,9 +196,7 @@ public class GraphQLProvider {
192
196
{{< / highlight >}}
193
197
<p/>
194
198
195
-
196
-
We are using Guava to read the file at runtime from our classpath, then create a `GraphQLSchema` and `GraphQL` instance. This `GraphQL` instance is exposed as Spring Bean. The GraphQL Java Spring adapter will use that `GraphQL` instance to expose it the schema over HTTP on the default url `/graphql`.
197
-
199
+
We use Guava to read the file at runtime from our classpath, then create a `GraphQLSchema` and `GraphQL` instance. This `GraphQL` instance is exposed as Spring Bean. The GraphQL Java Spring adapter will use that `GraphQL` instance to make our schema available via HTTP on the default url `/graphql`.
198
200
199
201
What we still need to do is to implement the `buildSchema` method which creates the `GraphQLSchema` instance:
200
202
@@ -214,8 +216,7 @@ What we still need to do is to implement the `buildSchema` method which creates
@@ -235,7 +236,7 @@ What we still need to do is to implement the `buildSchema` method which creates
235
236
Probably the most important concept for a GraphQL Java server is a `DataFetcher`:
236
237
A `DataFetcher` fetches the Data for one field while the query is executed.
237
238
238
-
While GraphQL Java is executing a query it calls the appropriate `DataFetcher` for each field it encounters in query.
239
+
While GraphQL Java is executing a query, it calls the appropriate `DataFetcher` for each field it encounters in query.
239
240
A `DataFetcher` is an Interface with a single method, taking a single argument of type `DataFetcherEnvironment`:
240
241
241
242
@@ -246,7 +247,7 @@ public interface DataFetcher<T> {
246
247
{{< / highlight >}}
247
248
<p/>
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
+
Important: **Every** field from the schema has a `DataFetcher` associated with. If you don't specify any `DataFetcher` for a specific field, then the default `PropertyDataFetcher` is used.
250
251
251
252
We are creating a new class `GraphQLDataFetchers` which contains a sample list of books and authors.
252
253
@@ -314,10 +315,9 @@ public class GraphQLDataFetchers {
314
315
## Source of the data
315
316
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
317
-
318
318
## Book DataFetcher
319
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.
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
321
322
322
The "id" in `String bookId = dataFetchingEnvironment.getArgument("id");` is the "id" from the `bookById` query field in the schema:
323
323
@@ -328,27 +328,25 @@ type Query {
328
328
...
329
329
{{< / highlight >}}
330
330
331
-
332
331
## 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.
332
+
Our second method `getAuthorDataFetcher`, returns a `DataFetcher` for getting the author for a specific book.
333
+
Compared to the previously described book `DataFetcher`, we don't have an argument, but we have a book instance.
334
+
The result of the `DataFetcher` from the parent field is made available via `getSource`.
335
+
This is an important concept to understand: the `DataFetcher` for each field in GraphQL are called in a top-down fashion and the parent's result is the `source` property of the child `DataFetcherEnvironment`.
338
336
337
+
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
338
340
339
# Try out 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`.
340
+
This is all that is 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
341
343
342
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
343
345
344
After starting it you will be asked for a URL, enter "http://localhost:8080/graphql".
346
345
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:
346
+
After that, you can query our example API and you should get back the result we mentioned above in the beginning. It should look something like this:
348
347
349
348

350
349
351
-
352
350
# Complete example source code
353
351
354
352
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