This repo contains the demo code for my talk "Getting Started with Spring for GraphQL". This project is built with Spring Boot 3.3.3 and the following dependencies:
- Spring Web
- Spring for GraphQL
- Spring Data JPA
- PostgreSQL
- Spring Boot Docker Compose
- Spring Boot Actuator
- More...
There are a few things you need to know about this application to get it up and running. First you will need to have Docker
desktop running to run the application as is. This is because the application uses the Docker Compose Module and will start
any services located in docker-compose.yml
automatically. If you don't want to use Docker you could remove that
module and then configure each of those services manually.
The application has a GraphiQL UI that you can use to interact with the GraphQL API. You can access it at http://localhost:8080/graphiql. Here you can run queries and mutations against the API. Here are a few examples:
query findProduct($id:ID) {
getProduct(id:$id) {
id
title
desc
}
}
query {
allProducts {
id
title
desc
}
}
query search {
search(text: "Waffle") {
__typename
... on Customer {
id
firstName
lastName
email
}
... on Product {
id
title
desc
}
}
}
mutation {
createProduct(product: {title:"New Product",desc:"New Product DESC"}) {
id
title
desc
}
}
There is a start-here
branch that you can use to follow along with the demo. Here is a rough outline of the steps I will
go through in the demo:
- http://start.spring.io
- Explore the Starting Code
- Walk through
src/main/resources/graphql/schema.graphqls
- Product Controller is empty to start
- Clear out GraphiQL UI
- Walk through
- Run the application schema mapping inspection report
- How much of the app are we going to build?
- ProductController
- allProducts
@SchemaMapping
&@QueryMapping
- method name doesn't match field ~ Cannot find GraphQL Schema
QueryMapping("products")
- getProduct
- @Argument
- Method Arguments
- createProduct
@MutationMapping
- Validation
- orders
@SchemaMapping
- Talk about difference between field and data fetcher / Registering Data Fetchers
- orders could come from a microservice
- allProducts
- Client App
- Testing
- Features (We won't get to cover all of these)
- Unions
- Interfaces
- Batch Mapping
- Defer
- Federation
- Virtual Threads
- All of this is happening sequentially on the same Tomcat thread
- Too many controller method invocations (n+1)
- ProductController
- Learn GraphQL
- Spring for GraphQL Project
- Spring for GraphQL Reference Documentation
- GraphQL Java
- Welcome, Spring for GraphQL by Rossen Stoyanchev @ Spring I/O 2022
- Observing Spring for GraphQL in Action by Brian Clozel & Rossen Stoyanchev @ Spring I/O 2023
- My GraphQL Playlist on YouTube
A huge thank you to the Spring team for all the hard work they have put into the Spring for GraphQL project. A special thanks to both Brian Clozel and Rossen Stoyanchev for all of their help and guidance on my GraphQL journey.