Skip to content

Commit 568e1b4

Browse files
committed
PR feedback
1 parent 2be7bbe commit 568e1b4

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

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

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ date = 2019-01-23
77
toc = "true"
88
+++
99

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.
1111

1212

1313
# GraphQL in 3 minutes
1414

1515
GraphQL is a query language to retrieve data from a server. It is an alternative to REST, SOAP or gRPC in some way.
1616

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.
1818

1919
With GraphQL you send the following query to server to get the details for the book with the id "123":
2020

@@ -32,9 +32,13 @@ With GraphQL you send the following query to server to get the details for the b
3232
}
3333
{{< / highlight >}}
3434

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
3640

37-
But the response is normal JSON:
41+
The response is normal JSON:
3842
{{< highlight json "linenos=table" >}}
3943
{
4044
"bookById":
@@ -50,7 +54,7 @@ But the response is normal JSON:
5054
}
5155
{{< / highlight >}}
5256

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")
5458

5559
The schema for the above query looks like this:
5660

@@ -73,7 +77,7 @@ type Author {
7377
}
7478
{{< / highlight >}}
7579

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.
7781

7882
We barely touched GraphQL. Further information can be found on the official page: https://graphql.github.io/learn/
7983

@@ -82,7 +86,7 @@ We barely touched GraphQL. Further information can be found on the official page
8286
[GraphQL Java](https://www.graphql-java.com) is the Java (server) implementation for GraphQL.
8387
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.
8488

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.
8690

8791
The main steps of creating a GraphQL Java server are:
8892

@@ -96,7 +100,7 @@ This is in no way a comprehensive API, but it is enough for this tutorial.
96100

97101
# Create a Spring Boot app
98102

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/.
100104

101105
Select:
102106

@@ -109,7 +113,7 @@ For the project metadata we use:
109113
- Group: `com.graphql-java.tutorial`
110114
- Artifact: `book-details`
111115

112-
As dependency we just select `Web`.
116+
As dependency, we just select `Web`.
113117

114118
A click on `Generate Project` will give you a ready to use Spring Boot app.
115119
All subsequently mentioned files and paths will be relative to this generated project.
@@ -154,16 +158,16 @@ type Author {
154158
{{< / highlight >}}
155159
<p/>
156160

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.
158162

159163
It also defines the type `Book` which has the fields: `id`, `name`, `pageCount` and `author`.
160164
`author` is of type `Author`, which is defined after `Book`.
161165

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/).
163167
164168
But so far it is just a normal text. We need to "bring it to live" by reading the file and parsing it.
165169

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:
167171

168172
{{< highlight java "linenos=table" >}}
169173

@@ -192,9 +196,7 @@ public class GraphQLProvider {
192196
{{< / highlight >}}
193197
<p/>
194198

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`.
198200

199201
What we still need to do is to implement the `buildSchema` method which creates the `GraphQLSchema` instance:
200202

@@ -214,8 +216,7 @@ What we still need to do is to implement the `buildSchema` method which creates
214216
.type(newTypeWiring("Query")
215217
.dataFetcher("bookById", graphQLDataFetchers.getBookByIdDataFetcher()))
216218
.type(newTypeWiring("Book")
217-
.dataFetcher("author", graphQLDataFetchers.getAuthorDataFetcher())
218-
.build())
219+
.dataFetcher("author", graphQLDataFetchers.getAuthorDataFetcher()))
219220
.build();
220221
}
221222
{{< / highlight >}}
@@ -235,7 +236,7 @@ What we still need to do is to implement the `buildSchema` method which creates
235236
Probably the most important concept for a GraphQL Java server is a `DataFetcher`:
236237
A `DataFetcher` fetches the Data for one field while the query is executed.
237238

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.
239240
A `DataFetcher` is an Interface with a single method, taking a single argument of type `DataFetcherEnvironment`:
240241

241242

@@ -246,7 +247,7 @@ public interface DataFetcher<T> {
246247
{{< / highlight >}}
247248
<p/>
248249

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.
250251

251252
We are creating a new class `GraphQLDataFetchers` which contains a sample list of books and authors.
252253

@@ -314,10 +315,9 @@ public class GraphQLDataFetchers {
314315
## Source of the data
315316
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
316317

317-
318318
## Book DataFetcher
319319
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.
321321

322322
The "id" in `String bookId = dataFetchingEnvironment.getArgument("id");` is the "id" from the `bookById` query field in the schema:
323323

@@ -328,27 +328,25 @@ type Query {
328328
...
329329
{{< / highlight >}}
330330

331-
332331
## 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`.
338336

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.
339338

340339
# 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`.
342341

343342
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.
344343

345344
After starting it you will be asked for a URL, enter "http://localhost:8080/graphql".
346345

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:
348347

349348
![GraphQL Playground](/images/playground.png)
350349

351-
352350
# Complete example source code
353351

354352
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

Comments
 (0)