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: src/pages/about.md
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,6 @@ hide_table_of_contents: true
6
6
7
7
# About
8
8
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).
10
10
11
11
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).
Copy file name to clipboardExpand all lines: src/pages/tutorials/getting-started-with-spring-boot.md
+45-57Lines changed: 45 additions & 57 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ id: tutorial-getting-started
5
5
---
6
6
# Getting started with Spring for GraphQL
7
7
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.
9
9
10
10
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/).
11
11
@@ -85,7 +85,7 @@ We've barely scratched the surface of what's possible with GraphQL. Further info
85
85
[GraphQL Java](https://www.graphql-java.com) is the Java (server) implementation for GraphQL.
86
86
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.
87
87
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.
89
89
90
90
The main steps of creating a GraphQL Java server are:
91
91
@@ -104,17 +104,17 @@ The easiest way to create a Spring Boot app is to use the [Spring Initializr](ht
104
104
Select:
105
105
106
106
- Gradle Project
107
-
- Java
108
-
- Spring Boot 2.7.x
107
+
- Spring Boot 3
108
+
- Java 17 or higher
109
109
110
110
For the project metadata, use:
111
111
112
-
- Group: `com.graphql-java.tutorial`
112
+
- Group: `com.graphqljava.tutorial`
113
113
- Artifact: `bookDetails`
114
114
115
-
For dependencies, use:
115
+
For dependencies, select:
116
116
117
-
- Spring Web
117
+
- Spring Web, and
118
118
- Spring for GraphQL
119
119
120
120
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
124
124
125
125
## Schema
126
126
127
-
Create a directory `src/main/resources/graphql`.
128
-
129
127
Add a new file `schema.graphqls` to `src/main/resources/graphql` with the following content:
130
128
131
129
```graphql
@@ -160,92 +158,81 @@ It is very important to understand that GraphQL doesn't dictate in any way where
160
158
This is the power of GraphQL: it can come from a static in-memory list, from a database or an external service.
161
159
162
160
### Create the Book class
161
+
163
162
Add the following to `bookDetails/Book.java`
164
163
```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(Stringid, 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
+
179
171
private static List<Book> books = Arrays.asList(
180
-
new Book("book-1", "Harry Potter and the Philosopher's Stone", 223, "author-1"),
181
-
newBook("book-2", "Moby Dick", 635, "author-2"),
182
-
newBook("book-3", "Interview with the vampire", 371, "author-3")
172
+
newBook("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")
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.
232
209
233
210
Add the following to `bookDetails/BookController.java`
The `@QueryMapping` annotation binds this method to a query, a field under the Query type.
250
237
The query field is then determined from the method name, `bookById`. It could also be declared on the annotation.
251
238
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
259
246
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`.
260
247
The type and field can be specified in the annotation.
261
248
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).
263
250
264
251
That's all the code we need! Let's run our first query.
265
252
@@ -270,9 +257,10 @@ GraphiQL is a useful visual interface for writing and executing queries, and muc
270
257
271
258
```
272
259
spring.graphql.graphiql.enabled=true
273
-
spring.graphql.graphiql.path=/graphiql
274
260
```
275
261
262
+
This will enable GraphiQL at the path `/graphiql` by default. You can change this path by configuring `spring.graphql.graphiql.path`.
263
+
276
264
### Boot the application
277
265
Start your Spring application.
278
266
@@ -304,7 +292,7 @@ With the help of Spring for GraphQL features, we were able to achieve this with
304
292
305
293
## Further reading
306
294
### 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.
308
296
309
297
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/).
310
298
@@ -315,7 +303,7 @@ The source code for this tutorial can be found on [GitHub](https://github.com/gr
315
303
Read the GraphQL Java [documentation](https://www.graphql-java.com/documentation/getting-started).
316
304
317
305
### 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).
319
307
320
308
### GitHub Discussions
321
309
We also use [GitHub Discussions](https://github.com/graphql-java/graphql-java/discussions) for any questions or problems.
0 commit comments