A comprehensive demonstration of different approaches to implementing GraphQL with Spring Boot, showcasing three popular libraries and their unique features.
This repository contains three independent Spring Boot applications that demonstrate different approaches to implementing GraphQL APIs. Each application implements the same domain model (Organization β Department β Employee hierarchy) using different GraphQL libraries, allowing for direct comparison of approaches and features.
- How to simplify Spring Boot and GraphQL development with GraphQL Kickstart library. The article describes more advanced solution like filtering or joins with a database. The example is available in the branch master. A detailed guide may be found in the following article: An Advanced Guide to GraphQL with Spring Boot
- How to simplify Spring Boot and GraphQL development with Netflix DGS library. The example is available in the branch master. A detailed guide may be found in the following article: An Advanced GraphQL with Spring Boot and Netflix DGS.
- How to simplify Spring Boot and GraphQL development with Spring for Graph library. The example is available in the branch master. A detailed guide may be found in the following article: An Advanced GraphQL with Spring Boot.
graph TB
A[Organization] --> B[Department]
B --> C[Employee]
subgraph GraphQLImplementations ["GraphQL Implementations"]
D[GraphQL Kickstart]
E[Netflix DGS]
F[Spring for GraphQL]
end
subgraph DataLayer ["Data Layer"]
G[JPA/Hibernate]
H[H2 Database]
end
D --> G
E --> G
F --> G
G --> H
GraphQL Kickstart Library Implementation
- Uses GraphQL Java Kickstart library
- Resolver-based approach with separate Query and Mutation resolvers
- Built-in GraphiQL interface
- Schema-first development approach
Key Features:
- Schema definition through
.graphqls
files - Resolver classes for queries and mutations
- Built-in error handling and validation
- GraphiQL playground at
/graphiql
Netflix DGS Framework Implementation
- Uses Netflix DGS (Domain Graph Service) framework
- Annotation-based data fetchers
- Code-first approach with schema generation
- Advanced context and instrumentation support
Key Features:
@DgsQuery
and@DgsMutation
annotations- Built-in testing utilities
- Schema generation from code
- Custom context builder support
Spring for GraphQL Implementation
- Uses official Spring GraphQL support
- Controller-based approach similar to Spring MVC
- Integrated with Spring Boot ecosystem
- Schema-first development with annotation mapping
Key Features:
@QueryMapping
and@MutationMapping
annotations- Native Spring Boot integration
- WebMVC and WebFlux support
- Built-in security integration
The application models a simple organizational structure:
erDiagram
Organization ||--o{ Department : contains
Department ||--o{ Employee : contains
Organization ||--o{ Employee : employs
Organization {
int id
string name
}
Department {
int id
string name
int organization_id
}
Employee {
int id
string firstName
string lastName
string position
int salary
int age
int department_id
int organization_id
}
- Java 17 (Java 11 for spring-graphql module)
- Maven 3.6+
- IDE with GraphQL support (IntelliJ IDEA, VS Code with GraphQL extension)
git clone https://github.com/piomin/sample-spring-boot-graphql.git
cd sample-spring-boot-graphql
mvn clean install
cd sample-app-kickstart
mvn spring-boot:run
- GraphQL endpoint:
http://localhost:8080/graphql
- GraphiQL interface:
http://localhost:8080/graphiql
cd sample-app-netflix-dgs
mvn spring-boot:run
- GraphQL endpoint:
http://localhost:8080/graphql
- GraphiQL interface:
http://localhost:8080/graphiql
cd sample-app-spring-graphql
mvn spring-boot:run
- GraphQL endpoint:
http://localhost:8080/graphql
- GraphiQL interface:
http://localhost:8080/graphiql
All three applications implement the same GraphQL schema:
type Query {
# Organization queries
organizations: [Organization]
organization(id: ID!): Organization!
# Department queries
departments: [Department]
department(id: ID!): Department!
# Employee queries
employees: [Employee]
employee(id: ID!): Employee!
employeesWithFilter(filter: EmployeeFilter): [Employee]
}
type Mutation {
# Organization mutations
newOrganization(organization: OrganizationInput!): Organization
# Department mutations
newDepartment(department: DepartmentInput!): Department
# Employee mutations
newEmployee(employee: EmployeeInput!): Employee
}
type Organization {
id: ID!
name: String!
employees: [Employee]
departments: [Department]
}
type Department {
id: ID!
name: String!
organization: Organization
employees: [Employee]
}
type Employee {
id: ID!
firstName: String!
lastName: String!
position: String!
salary: Int
age: Int
department: Department
organization: Organization
}
input EmployeeFilter {
salary: FilterField
age: FilterField
position: FilterField
}
input FilterField {
operator: String! # eq, gt, lt, gte, lte, ne
value: String!
}
query {
organizations {
id
name
departments {
id
name
employees {
id
firstName
lastName
position
}
}
}
}
query {
employeesWithFilter(filter: {
salary: { operator: "gt", value: "15000" }
position: { operator: "eq", value: "Developer" }
}) {
id
firstName
lastName
position
salary
department {
name
}
}
}
mutation {
newEmployee(employee: {
firstName: "Jane"
lastName: "Doe"
position: "Senior Developer"
salary: 25000
age: 28
organizationId: 1
departmentId: 1
}) {
id
firstName
lastName
position
salary
}
}
mvn test
cd sample-app-kickstart
mvn test
mvn jacoco:report
Each module includes comprehensive test coverage for:
- GraphQL query resolvers
- Mutation resolvers
- Filtering functionality
- Error handling
Feature | GraphQL Kickstart | Netflix DGS | Spring GraphQL |
---|---|---|---|
Learning Curve | Moderate | Steep | Easy (Spring devs) |
Performance | Good | Excellent | Good |
Community | Active | Growing | Official Spring |
Documentation | Comprehensive | Good | Excellent |
Testing Support | Good | Excellent | Good |
Schema Generation | Schema-first | Code-first | Schema-first |
Spring Integration | Good | Excellent | Native |
- Framework: Spring Boot 3.x
- Language: Java 17
- Database: H2 (in-memory)
- ORM: JPA/Hibernate
- Build Tool: Maven
- Testing: JUnit 5, Spring Boot Test
- Code Coverage: JaCoCo
- CI/CD: CircleCI
- Code Quality: SonarCloud
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Make your changes
- Run tests (
mvn test
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Follow Java coding conventions
- Ensure all tests pass
- Maintain test coverage above 80%
- Update documentation for new features
This project is licensed under the MIT License - see the LICENSE file for details.
If you have any questions or need help with the project:
- Create an issue in this repository
- Follow @piotr_minkowski on Twitter
- Check out the related blog articles for detailed explanations
- GraphQL Java Kickstart team
- Netflix DGS team
- Spring GraphQL team
- All contributors to this project