Skip to content

Commit f452b7c

Browse files
authored
Merge pull request #144 from graphql-java/tutorial-spring-boot-3-update
Update tutorial for Java 17 and Spring Boot 3
2 parents 9be55e5 + 1e7d4c9 commit f452b7c

16 files changed

Lines changed: 47 additions & 493 deletions

blog/2021-02-12-spec-releases-are-not-important.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,3 @@ It is better to think about certain features you want to discuss instead referri
5050

5151
# Feedback or questions
5252
We use [GitHub Discussions](https://github.com/graphql-java/graphql-java/discussions) for general feedback and questions.
53-
54-
You can also contact us on Twitter: [@graphql_java](https://twitter.com/graphql_java)

blog/2021-08-03-17-released-and-lts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ Going forward we decided to no offer any LTS versions anymore. We will only acti
1717
the latest version (currently 17). We may backport critical bugfixes, but we are not committed to it.
1818

1919
If this is a huge problem for you or your Company and you are willing to help us with maintaining a LTS
20-
version you can reach us at Twitter [GraphQL Java](https://twitter.com/graphql_java) or per mail at
20+
version you can reach us via email at
2121
hello at graphql-java dot com.

docusaurus.config.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ const config = {
103103
label: 'GitHub Discussions',
104104
href: 'https://github.com/graphql-java/graphql-java/discussions',
105105
},
106-
{
107-
label: 'Twitter',
108-
href: 'https://twitter.com/graphql_java',
109-
},
110106
],
111107
},
112108
{

src/components/HomepageFeatures.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/components/HomepageFeatures.module.css

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/pages/about.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ 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://github.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.
12-
13-
You can also follow [GraphQL Java on Twitter](https://twitter.com/graphql_java).

src/pages/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import Layout from '@theme/Layout';
44
import Link from '@docusaurus/Link';
55
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
66
import styles from './index.module.css';
7-
import HomepageFeatures from '../components/HomepageFeatures';
87

98
function HomepageHeader() {
109
const {siteConfig} = useDocusaurusContext();
@@ -32,9 +31,6 @@ export default function Home() {
3231
title={`Hello from ${siteConfig.title}`}
3332
description="GraphQL Java home page">
3433
<HomepageHeader />
35-
{/*<main>*/}
36-
{/*<HomepageFeatures />*/}
37-
{/*</main>*/}
3834
</Layout>
3935
);
4036
}

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

Lines changed: 45 additions & 57 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/) in 3 minutes. 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
109109

110110
For the project metadata, use:
111111

112-
- Group: `com.graphql-java.tutorial`
112+
- Group: `com.graphqljava.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.
@@ -124,8 +124,6 @@ Spring for GraphQL adds many useful features including loading schema files, ini
124124

125125
## Schema
126126

127-
Create a directory `src/main/resources/graphql`.
128-
129127
Add a new file `schema.graphqls` to `src/main/resources/graphql` with the following content:
130128

131129
```graphql
@@ -160,92 +158,81 @@ It is very important to understand that GraphQL doesn't dictate in any way where
160158
This is the power of GraphQL: it can come from a static in-memory list, from a database or an external service.
161159

162160
### Create the Book class
161+
163162
Add the following to `bookDetails/Book.java`
164163
```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-
164+
package com.graphqljava.tutorial.bookDetails;
165+
166+
import java.util.Arrays;
167+
import java.util.List;
168+
169+
record Book(String id, String name, int pageCount, String authorId) {
170+
179171
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")
172+
new Book("book-1", "Harry Potter and the Philosopher's Stone", 223, "author-1"),
173+
new Book("book-2", "Moby Dick", 635, "author-2"),
174+
new Book("book-3", "Interview with the vampire", 371, "author-3")
183175
);
184176

185177
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;
178+
return books.stream().filter(book -> book.id().equals(id)).findFirst().orElse(null);
191179
}
192180

193-
public String getAuthorId() {
194-
return authorId;
195-
}
196181
}
197182
```
198183

199184
### Create the Author class
200185
Add the following to `bookDetails/Author.java`
201186
```java
202-
public class Author {
187+
package com.graphqljava.tutorial.bookDetails;
203188

204-
private String id;
205-
private String firstName;
206-
private String lastName;
189+
import java.util.Arrays;
190+
import java.util.List;
207191

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

214194
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")
195+
new Author("author-1", "Joanne", "Rowling"),
196+
new Author("author-2", "Herman", "Melville"),
197+
new Author("author-3", "Anne", "Rice")
218198
);
219199

220200
public static Author getById(String id) {
221-
return authors.stream().filter(author -> author.getId().equals(id)).findFirst().orElse(null);
201+
return authors.stream().filter(author -> author.id().equals(id)).findFirst().orElse(null);
222202
}
223203

224-
public String getId() {
225-
return id;
226-
}
227204
}
228205
```
229206

230207
## 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.
208+
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.
232209

233210
Add the following to `bookDetails/BookController.java`
234211

235212
```java
213+
package com.graphqljava.tutorial.bookDetails;
214+
215+
import org.springframework.graphql.data.method.annotation.Argument;
216+
import org.springframework.graphql.data.method.annotation.QueryMapping;
217+
import org.springframework.graphql.data.method.annotation.SchemaMapping;
218+
import org.springframework.stereotype.Controller;
219+
236220
@Controller
237-
public class BookController {
221+
class BookController {
222+
238223
@QueryMapping
239224
public Book bookById(@Argument String id) {
240225
return Book.getById(id);
241226
}
242227

243228
@SchemaMapping
244229
public Author author(Book book) {
245-
return Author.getById(book.getAuthorId());
230+
return Author.getById(book.authorId());
246231
}
232+
247233
}
248234
```
235+
249236
The `@QueryMapping` annotation binds this method to a query, a field under the Query type.
250237
The query field is then determined from the method name, `bookById`. It could also be declared on the annotation.
251238
Spring for GraphQL uses `RuntimeWiring.Builder` to register the handler method as a `graphql.schema.DataFetcher` for the query field `bookById`.
@@ -259,7 +246,7 @@ The `@SchemaMapping` annotation maps a handler method to a field in the GraphQL
259246
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`.
260247
The type and field can be specified in the annotation.
261248

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).
249+
For more, see the [documentation for the Spring for GraphQL annotated controller feature](https://docs.spring.io/spring-graphql/reference/controllers.html).
263250

264251
That's all the code we need! Let's run our first query.
265252

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

271258
```
272259
spring.graphql.graphiql.enabled=true
273-
spring.graphql.graphiql.path=/graphiql
274260
```
275261

262+
This will enable GraphiQL at the path `/graphiql` by default. You can change this path by configuring `spring.graphql.graphiql.path`.
263+
276264
### Boot the application
277265
Start your Spring application.
278266

@@ -304,7 +292,7 @@ With the help of Spring for GraphQL features, we were able to achieve this with
304292

305293
## Further reading
306294
### Book
307-
If you want to learn more, we 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 and GraphQL Java.
295+
If you want to learn more, 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 and GraphQL Java.
308296

309297
Learn first-hand from the founder of GraphQL Java and co-author of Spring for GraphQL. The book is suitable for beginners and also includes advanced topics for intermediate readers. The book is available on [Leanpub](https://leanpub.com/graphql-java) and [Amazon](https://www.amazon.com/GraphQL-Java-Spring-Andreas-Marek-ebook/dp/B0C96ZYWPF/).
310298

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

317305
### 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.
306+
See the [Spring for GraphQL documentation for more samples](https://docs.spring.io/spring-graphql/reference/samples.html).
319307

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

static/img/docusaurus.png

-5.02 KB
Binary file not shown.

static/img/graphiQL.png

42.4 KB
Loading

0 commit comments

Comments
 (0)