- Notifications
You must be signed in to change notification settings - Fork47
Demo application illustrating advanced usage of GraphQL with Spring Boot like filtering or relationship fetching
piomin/sample-spring-boot-graphql
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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 branchmaster. 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 branchmaster. 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 branchmaster. 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 --> HGraphQL Kickstart Library Implementation
- UsesGraphQL 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
.graphqlsfiles - Resolver classes for queries and mutations
- Built-in error handling and validation
- GraphiQL playground at
/graphiql
Netflix DGS Framework Implementation
- UsesNetflix DGS (Domain Graph Service) framework
- Annotation-based data fetchers
- Code-first approach with schema generation
- Advanced context and instrumentation support
Key Features:
@DgsQueryand@DgsMutationannotations- Built-in testing utilities
- Schema generation from code
- Custom context builder support
Spring for GraphQL Implementation
- Uses officialSpring GraphQL support
- Controller-based approach similar to Spring MVC
- Integrated with Spring Boot ecosystem
- Schema-first development with annotation mapping
Key Features:
@QueryMappingand@MutationMappingannotations- 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.gitcd sample-spring-boot-graphqlmvn clean install
cd sample-app-kickstartmvn spring-boot:run- GraphQL endpoint:
http://localhost:8080/graphql - GraphiQL interface:
http://localhost:8080/graphiql
cd sample-app-netflix-dgsmvn spring-boot:run- GraphQL endpoint:
http://localhost:8080/graphql - GraphiQL interface:
http://localhost:8080/graphiql
cd sample-app-spring-graphqlmvn spring-boot:run- GraphQL endpoint:
http://localhost:8080/graphql - GraphiQL interface:
http://localhost:8080/graphiql
All three applications implement the same GraphQL schema:
typeQuery { # Organization queriesorganizations: [Organization]organization(id:ID!):Organization! # Department queriesdepartments: [Department]department(id:ID!):Department! # Employee queriesemployees: [Employee]employee(id:ID!):Employee!employeesWithFilter(filter:EmployeeFilter): [Employee]}typeMutation { # Organization mutationsnewOrganization(organization:OrganizationInput!):Organization # Department mutationsnewDepartment(department:DepartmentInput!):Department # Employee mutationsnewEmployee(employee:EmployeeInput!):Employee}typeOrganization {id:ID!name:String!employees: [Employee]departments: [Department]}typeDepartment {id:ID!name:String!organization:Organizationemployees: [Employee]}typeEmployee {id:ID!firstName:String!lastName:String!position:String!salary:Intage:Intdepartment:Departmentorganization:Organization}inputEmployeeFilter {salary:FilterFieldage:FilterFieldposition:FilterField}inputFilterField {operator:String! # eq, gt, lt, gte, lte, nevalue:String!}
query {organizations {idnamedepartments {idnameemployees {idfirstNamelastNameposition } } }}
query {employeesWithFilter(filter: {salary: {operator:"gt",value:"15000" }position: {operator:"eq",value:"Developer" } }) {idfirstNamelastNamepositionsalarydepartment {name } }}
mutation {newEmployee(employee: {firstName:"Jane"lastName:"Doe"position:"Senior Developer"salary:25000age:28organizationId:1departmentId:1 }) {idfirstNamelastNamepositionsalary }}
mvntestcd sample-app-kickstartmvntest
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
About
Demo application illustrating advanced usage of GraphQL with Spring Boot like filtering or relationship fetching
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.