Skip to content

Commit afc3fae

Browse files
committed
Update tutorial for Java 17 and Spring Boot 3
1 parent 56ef5e6 commit afc3fae

2 files changed

Lines changed: 44 additions & 54 deletions

File tree

src/pages/about.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ hide_table_of_contents: true
66

77
# About
88

9-
The GraphQL Java project is run by [Andreas Marek](https://twitter.com/andimarek) and [Brad Baker](https://github.com/bbakerman).
9+
The GraphQL Java project is run by [Andreas Marek](https://twitter.com/andimarek), [Brad Baker](https://github.com/bbakerman), and [Donna Zhou](https://github.com/dondonz).
1010

1111
Andreas Marek is responsible for the content of this website.
1212

src/pages/tutorials/getting-started-with-spring-boot.md

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ id: tutorial-getting-started
55
---
66
# Getting started with Spring for GraphQL
77

8-
In this tutorial, you will create a GraphQL server in Java using [Spring for GraphQL](https://docs.spring.io/spring-graphql/docs/current/reference/html/). It requires a little Spring and Java knowledge. While we give a brief introduction to GraphQL, the focus of this tutorial is developing a GraphQL server in Java.
8+
In this tutorial, you will create a GraphQL server in Java using [Spring for GraphQL](https://docs.spring.io/spring-graphql/reference/). It requires a little Spring and Java knowledge. While we give a brief introduction to GraphQL, the focus of this tutorial is developing a GraphQL server in Java.
99

1010
If you're looking to learn more after this tutorial, we (the maintainers) have written a book! [**GraphQL with Java and Spring**](https://leanpub.com/graphql-java) includes everything you need to know to build a production ready GraphQL service with Spring for GraphQL, the official Spring integration built on top of the GraphQL Java engine. It's available on [Leanpub](https://leanpub.com/graphql-java) and [Amazon](https://www.amazon.com/GraphQL-Java-Spring-Andreas-Marek-ebook/dp/B0C96ZYWPF/).
1111

@@ -85,7 +85,7 @@ We've barely scratched the surface of what's possible with GraphQL. Further info
8585
[GraphQL Java](https://www.graphql-java.com) is the Java (server) implementation for GraphQL.
8686
There 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 the basis for everything else.
8787

88-
The GraphQL Java Engine is only concerned with executing queries. It doesn't deal with any HTTP or JSON related topics. For these aspects, we will use [Spring for GraphQL](https://docs.spring.io/spring-graphql/docs/current/reference/html/) which takes care of exposing our API via Spring Boot over HTTP.
88+
The GraphQL Java Engine is only concerned with executing queries. It doesn't deal with any HTTP or JSON related topics. For these aspects, we will use [Spring for GraphQL](https://docs.spring.io/spring-graphql/reference/) which takes care of exposing our API via Spring Boot over HTTP.
8989

9090
The main steps of creating a GraphQL Java server are:
9191

@@ -104,17 +104,17 @@ The easiest way to create a Spring Boot app is to use the [Spring Initializr](ht
104104
Select:
105105

106106
- Gradle Project
107-
- Java
108-
- Spring Boot 2.7.x
107+
- Spring Boot 3
108+
- Java 17 or higher (Java 17 is the baseline version for Spring Boot 3)
109109

110110
For the project metadata, use:
111111

112112
- Group: `com.graphql-java.tutorial`
113113
- Artifact: `bookDetails`
114114

115-
For dependencies, use:
115+
For dependencies, select:
116116

117-
- Spring Web
117+
- Spring Web, and
118118
- Spring for GraphQL
119119

120120
Then click on `Generate` for a ready to use Spring Boot app.
@@ -160,92 +160,81 @@ It is very important to understand that GraphQL doesn't dictate in any way where
160160
This is the power of GraphQL: it can come from a static in-memory list, from a database or an external service.
161161

162162
### Create the Book class
163+
163164
Add the following to `bookDetails/Book.java`
164165
```java
165-
public class Book {
166-
167-
private String id;
168-
private String name;
169-
private int pageCount;
170-
private String authorId;
171-
172-
public Book(String id, String name, int pageCount, String authorId) {
173-
this.id = id;
174-
this.name = name;
175-
this.pageCount = pageCount;
176-
this.authorId = authorId;
177-
}
178-
166+
package com.graphqljava.tutorial.bookDetails;
167+
168+
import java.util.Arrays;
169+
import java.util.List;
170+
171+
record Book(String id, String name, int pageCount, String authorId) {
172+
179173
private static List<Book> books = Arrays.asList(
180-
new Book("book-1", "Harry Potter and the Philosopher's Stone", 223, "author-1"),
181-
new Book("book-2", "Moby Dick", 635, "author-2"),
182-
new Book("book-3", "Interview with the vampire", 371, "author-3")
174+
new Book("book-1", "Harry Potter and the Philosopher's Stone", 223, "author-1"),
175+
new Book("book-2", "Moby Dick", 635, "author-2"),
176+
new Book("book-3", "Interview with the vampire", 371, "author-3")
183177
);
184178

185179
public static Book getById(String id) {
186-
return books.stream().filter(book -> book.getId().equals(id)).findFirst().orElse(null);
187-
}
188-
189-
public String getId() {
190-
return id;
180+
return books.stream().filter(book -> book.id().equals(id)).findFirst().orElse(null);
191181
}
192182

193-
public String getAuthorId() {
194-
return authorId;
195-
}
196183
}
197184
```
198185

199186
### Create the Author class
200187
Add the following to `bookDetails/Author.java`
201188
```java
202-
public class Author {
189+
package com.graphqljava.tutorial.bookDetails;
203190

204-
private String id;
205-
private String firstName;
206-
private String lastName;
191+
import java.util.Arrays;
192+
import java.util.List;
207193

208-
public Author(String id, String firstName, String lastName) {
209-
this.id = id;
210-
this.firstName = firstName;
211-
this.lastName = lastName;
212-
}
194+
record Author(String id, String firstName, String lastName) {
213195

214196
private static List<Author> authors = Arrays.asList(
215-
new Author("author-1", "Joanne", "Rowling"),
216-
new Author("author-2", "Herman", "Melville"),
217-
new Author("author-3", "Anne", "Rice")
197+
new Author("author-1", "Joanne", "Rowling"),
198+
new Author("author-2", "Herman", "Melville"),
199+
new Author("author-3", "Anne", "Rice")
218200
);
219201

220202
public static Author getById(String id) {
221-
return authors.stream().filter(author -> author.getId().equals(id)).findFirst().orElse(null);
203+
return authors.stream().filter(author -> author.id().equals(id)).findFirst().orElse(null);
222204
}
223205

224-
public String getId() {
225-
return id;
226-
}
227206
}
228207
```
229208

230209
## Adding code to fetch data
231-
Spring for GraphQL provides an [annotation-based programming model](https://docs.spring.io/spring-graphql/docs/current/reference/html/#controllers) to declare handler methods to fetch the data for specific GraphQL fields.
210+
Spring for GraphQL provides an [annotation-based programming model](https://docs.spring.io/spring-graphql/reference/controllers.html) to declare handler methods to fetch the data for specific GraphQL fields.
232211

233212
Add the following to `bookDetails/BookController.java`
234213

235214
```java
215+
package com.graphqljava.tutorial.bookDetails;
216+
217+
import org.springframework.graphql.data.method.annotation.Argument;
218+
import org.springframework.graphql.data.method.annotation.QueryMapping;
219+
import org.springframework.graphql.data.method.annotation.SchemaMapping;
220+
import org.springframework.stereotype.Controller;
221+
236222
@Controller
237-
public class BookController {
223+
class BookController {
224+
238225
@QueryMapping
239226
public Book bookById(@Argument String id) {
240227
return Book.getById(id);
241228
}
242229

243230
@SchemaMapping
244231
public Author author(Book book) {
245-
return Author.getById(book.getAuthorId());
232+
return Author.getById(book.authorId());
246233
}
234+
247235
}
248236
```
237+
249238
The `@QueryMapping` annotation binds this method to a query, a field under the Query type.
250239
The query field is then determined from the method name, `bookById`. It could also be declared on the annotation.
251240
Spring for GraphQL uses `RuntimeWiring.Builder` to register the handler method as a `graphql.schema.DataFetcher` for the query field `bookById`.
@@ -259,7 +248,7 @@ The `@SchemaMapping` annotation maps a handler method to a field in the GraphQL
259248
The field name defaults to the method name, and the type name defaults to the simple class name of the source/parent object injected into the method. In this example, the field defaults to `author` and the type defaults to `Book`.
260249
The type and field can be specified in the annotation.
261250

262-
For more, see the [documentation for the Spring for GraphQL annotated controller feature](https://docs.spring.io/spring-graphql/docs/current/reference/html/#controllers).
251+
For more, see the [documentation for the Spring for GraphQL annotated controller feature](https://docs.spring.io/spring-graphql/reference/controllers.html).
263252

264253
That's all the code we need! Let's run our first query.
265254

@@ -270,9 +259,10 @@ GraphiQL is a useful visual interface for writing and executing queries, and muc
270259

271260
```
272261
spring.graphql.graphiql.enabled=true
273-
spring.graphql.graphiql.path=/graphiql
274262
```
275263

264+
This will enable GraphiQL at the path `/graphiql` by default. You can change this path by configuring `spring.graphql.graphiql.path`.
265+
276266
### Boot the application
277267
Start your Spring application.
278268

@@ -315,7 +305,7 @@ The source code for this tutorial can be found on [GitHub](https://github.com/gr
315305
Read the GraphQL Java [documentation](https://www.graphql-java.com/documentation/getting-started).
316306

317307
### More Spring for GraphQL examples
318-
See samples in the [1.0.x branch](https://github.com/spring-projects/spring-graphql/tree/1.0.x/samples), which will soon be [moved into](https://github.com/spring-projects/spring-graphql/issues/208) a separate repository.
308+
See the [Spring for GraphQL documentation for more samples](https://docs.spring.io/spring-graphql/reference/samples.html).
319309

320310
### GitHub Discussions
321311
We also use [GitHub Discussions](https://github.com/graphql-java/graphql-java/discussions) for any questions or problems.

0 commit comments

Comments
 (0)