1. Add Dependencies
If you’re using Maven, add the following to your pom.xml
:
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- GraphQL Starter -->
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>
<!-- GraphQL Tools (schema parsing) -->
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>11.0.0</version>
</dependency>
<!-- Optional: GraphiQL UI -->
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>11.1.0</version>
</dependency>
</dependencies>
(Or use the latest versions if you start a new project.)
2. Define a GraphQL Schema
Create a schema file under src/main/resources/graphql/schema.graphqls
:
type Book {
id: ID!
title: String!
author: String!
}
type Query {
getBookById(id: ID!): Book
getAllBooks: [Book]
}
This defines a Book
type and queries to fetch books.
3. Create a Model Class
package com.example.demo.model;
public class Book {
private String id;
private String title;
private String author;
public Book(String id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}
// getters & setters
public String getId() { return id; }
public String getTitle() { return title; }
public String getAuthor() { return author; }
}
4. Create a Service Layer
package com.example.demo.service;
import com.example.demo.model.Book;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
@Service
public class BookService {
private final List<Book> books = Arrays.asList(
new Book("1", "GraphQL for Beginners", "Alice"),
new Book("2", "Spring Boot in Action", "Bob")
);
public Book getBookById(String id) {
return books.stream().filter(b -> b.getId().equals(id)).findFirst().orElse(null);
}
public List<Book> getAllBooks() {
return books;
}
}
5. Create a GraphQL Resolver
package com.example.demo.resolver;
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import com.example.demo.model.Book;
import com.example.demo.service.BookService;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class BookQueryResolver implements GraphQLQueryResolver {
private final BookService bookService;
public BookQueryResolver(BookService bookService) {
this.bookService = bookService;
}
public Book getBookById(String id) {
return bookService.getBookById(id);
}
public List<Book> getAllBooks() {
return bookService.getAllBooks();
}
}
6. Run the Application
Run your Spring Boot app (@SpringBootApplication
).
GraphQL endpoint is available at:
- http://localhost:8080/graphql
- GraphiQL UI (if enabled): http://localhost:8080/graphiql
7. Example Queries
Query all books:
query {
getAllBooks {
id
title
author
}
}
Query a single book:
query {
getBookById(id: "1") {
id
title
author
}
}