|
| 1 | ++++ |
| 2 | +title = "Getting started with GraphQL Java and Spring Boot" |
| 3 | +author = "Andreas Marek" |
| 4 | +tags = [] |
| 5 | +categories = [] |
| 6 | +date = 2018-10-22T01:00:00+10:00 |
| 7 | ++++ |
| 8 | + |
| 9 | +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 | + |
| 11 | + |
| 12 | +# GraphQL in 3 minutes |
| 13 | + |
| 14 | +GraphQL is a query language to retrieve data from a server. It is an alternative to REST, SOAP or gRPC in some way. |
| 15 | + |
| 16 | +For example we wanna query the details for specific book from a online store backend. |
| 17 | + |
| 18 | +With GraphQL you send the following query to server to get the details for the book with the id "123": |
| 19 | + |
| 20 | +{{< highlight graphql "linenos=table" >}} |
| 21 | +{ |
| 22 | + book(id: "123"){ |
| 23 | + id |
| 24 | + name |
| 25 | + pageCount |
| 26 | + author { |
| 27 | + firstName |
| 28 | + lastName |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | +{{< / highlight >}} |
| 33 | + |
| 34 | +This is not JSON (even if it looks remotely similar), but it is a GraphQL query. |
| 35 | + |
| 36 | +But the response is normal JSON: |
| 37 | +{{< highlight json "linenos=table" >}} |
| 38 | +{ |
| 39 | + "id":"123", |
| 40 | + "name":"Harry Potter and the Philosopher's Stone", |
| 41 | + "pageCount":223, |
| 42 | + "author": { |
| 43 | + "firstName":"J. K.", |
| 44 | + "lastName":"Rowling" |
| 45 | + } |
| 46 | +} |
| 47 | +{{< / highlight >}} |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +The schema for the above query looks like this: |
| 52 | + |
| 53 | +{{< highlight graphql "linenos=table" >}} |
| 54 | +type Query { |
| 55 | + book(id: ID): Book |
| 56 | +} |
| 57 | + |
| 58 | +type Book { |
| 59 | + id: ID |
| 60 | + name: String |
| 61 | + pageCount: Int |
| 62 | + author: Person |
| 63 | +} |
| 64 | + |
| 65 | +type Person { |
| 66 | + firstName: String |
| 67 | + lastName: String |
| 68 | +} |
| 69 | +{{< / highlight >}} |
| 70 | + |
| 71 | +This tutorial will focus on how to implement a GraphQL server in Java. |
| 72 | + |
| 73 | +We barely touched GraphQL. Further information can be found on the official page: https://graphql.github.io/learn/ |
| 74 | + |
| 75 | + |
| 76 | +# GraphQL Java Overview |
| 77 | + |
| 78 | +[GraphQL Java](https://www.graphql-java.com) is the Java (server) implementation for GraphQL. The [GraphQL Java Github Repo](https://github.com/graphql-java/graphql-java) contains the actual source code. |
| 79 | + |
| 80 | +GraphQL Java itself is only concerned with executing queries. It doesn't deal with any HTTP or JSON related topics. For these aspects we will use [Spring Boot](https://spring.io/projects/spring-boot). |
| 81 | + |
| 82 | +The main steps of creating a GraphQL Java server are: |
| 83 | + |
| 84 | +- Defining a GraphQL Schema. |
| 85 | +- Defining on how the actual data for a query is fetched. |
| 86 | +- Exposing the server via HTTP (via Spring Boot). |
| 87 | + |
| 88 | + |
| 89 | +# Our example app: an online store for books |
| 90 | + |
| 91 | +Our example app we will build is a simple online store for books. |
| 92 | +We assume the very simple user flow: |
| 93 | + |
| 94 | +the user comes to our page and sees all available books for order. |
| 95 | +They can select a specific book and look up the details. If they like it they can order it. |
| 96 | + |
| 97 | +We will build a GraphQL server which will cover the following use cases: |
| 98 | + |
| 99 | +1. get a list of available books |
| 100 | +1. get specific book details |
| 101 | +1. order a book |
| 102 | + |
| 103 | +We will incrementally build our app. |
| 104 | + |
| 105 | +> **What about the schema/API design?**<br/> |
| 106 | +Schema and API design itself is interesting and challenging but we will focus on implementing the server and not discuss the actual schema design choices. |
| 107 | + |
| 108 | + |
| 109 | +# Step 1: Create a Spring Boot app |
| 110 | + |
| 111 | +The easiest way to create a Spring Boot app is to use the initializr at https://start.spring.io/. |
| 112 | + |
| 113 | +Select: |
| 114 | + |
| 115 | +- Gradle Project |
| 116 | +- Java |
| 117 | +- Spring Boot 2.x |
| 118 | + |
| 119 | +For the project metadata we use: |
| 120 | + |
| 121 | +- Group: `com.graphql-java.tutorial` |
| 122 | +- Artifact: `online-store` |
| 123 | + |
| 124 | +As dependency we just select `Web`. |
| 125 | + |
| 126 | +A click on `Generate Project` will give you a ready to use Spring Boot app. |
| 127 | +All subsequently mentioned files and paths will be relative to this generated project. |
| 128 | + |
| 129 | +We are adding two dependency to our project inside the `dependencies` section of `build.gradle`: |
| 130 | + |
| 131 | +the first one is GraphQL Java itself and the second one is [Google Guava](https://github.com/google/guava). Guava is not strictly needed but it will make our life easier. |
| 132 | + |
| 133 | +{{< highlight groovy "linenos=table" >}} |
| 134 | +dependencies { |
| 135 | + ... |
| 136 | + implementation('com.graphql-java:graphql-java:11.0') |
| 137 | + implementation('com.google.guava:guava:26.0-jre') |
| 138 | +} |
| 139 | +{{< / highlight >}} |
| 140 | + |
| 141 | +## Defining our first schema |
| 142 | + |
| 143 | +We are creating a new file `schema.graphqls` in `src/main/resources` with the following content: |
| 144 | + |
| 145 | +{{< highlight graphql "linenos=table" >}} |
| 146 | +type Query { |
| 147 | + books: [Books] |
| 148 | +} |
| 149 | + |
| 150 | +type Book { |
| 151 | + id: ID |
| 152 | + name: String |
| 153 | + price: Price |
| 154 | + availableCopies: Int |
| 155 | + pageCount: Int |
| 156 | + author: Person |
| 157 | +} |
| 158 | + |
| 159 | +type Price { |
| 160 | + price: String |
| 161 | + currency: String |
| 162 | +} |
| 163 | + |
| 164 | +type Person { |
| 165 | + firstName: String |
| 166 | + lastName: String |
| 167 | +} |
| 168 | + |
| 169 | +{{< / highlight >}} |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
0 commit comments