Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Demo application illustrating advanced usage of GraphQL with Spring Boot like filtering or relationship fetching

NotificationsYou must be signed in to change notification settings

piomin/sample-spring-boot-graphql

Repository files navigation

CircleCISonarCloudBugsCoverageLines of Code

A comprehensive demonstration of different approaches to implementing GraphQL with Spring Boot, showcasing three popular libraries and their unique features.

🚀 Project Overview

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.

🔗 Related Articles

  1. 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
  2. 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.
  3. 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.

🏗️ Architecture

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
Loading

📦 Modules

1. sample-app-kickstart

GraphQL 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.graphqls files
  • Resolver classes for queries and mutations
  • Built-in error handling and validation
  • GraphiQL playground at/graphiql

2. sample-app-netflix-dgs

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:

  • @DgsQuery and@DgsMutation annotations
  • Built-in testing utilities
  • Schema generation from code
  • Custom context builder support

3. sample-app-spring-graphql

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:

  • @QueryMapping and@MutationMapping annotations
  • Native Spring Boot integration
  • WebMVC and WebFlux support
  • Built-in security integration

🗄️ Domain Model

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    }
Loading

🔧 Prerequisites

  • Java 17 (Java 11 for spring-graphql module)
  • Maven 3.6+
  • IDE with GraphQL support (IntelliJ IDEA, VS Code with GraphQL extension)

🚀 Quick Start

Clone the Repository

git clone https://github.com/piomin/sample-spring-boot-graphql.gitcd sample-spring-boot-graphql

Build All Modules

mvn clean install

Run Individual Applications

GraphQL Kickstart

cd sample-app-kickstartmvn spring-boot:run
  • GraphQL endpoint:http://localhost:8080/graphql
  • GraphiQL interface:http://localhost:8080/graphiql

Netflix DGS

cd sample-app-netflix-dgsmvn spring-boot:run
  • GraphQL endpoint:http://localhost:8080/graphql
  • GraphiQL interface:http://localhost:8080/graphiql

Spring for GraphQL

cd sample-app-spring-graphqlmvn spring-boot:run
  • GraphQL endpoint:http://localhost:8080/graphql
  • GraphiQL interface:http://localhost:8080/graphiql

📊 GraphQL Schema

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!}

🔍 Example Queries

Query All Organizations with Departments

query {organizations {idnamedepartments {idnameemployees {idfirstNamelastNameposition      }    }  }}

Query Employees with Filtering

query {employeesWithFilter(filter: {salary: {operator:"gt",value:"15000" }position: {operator:"eq",value:"Developer" }  }) {idfirstNamelastNamepositionsalarydepartment {name    }  }}

Create New Employee

mutation {newEmployee(employee: {firstName:"Jane"lastName:"Doe"position:"Senior Developer"salary:25000age:28organizationId:1departmentId:1  }) {idfirstNamelastNamepositionsalary  }}

🧪 Testing

Run All Tests

mvntest

Run Tests for Specific Module

cd sample-app-kickstartmvntest

Test Coverage

mvn jacoco:report

Each module includes comprehensive test coverage for:

  • GraphQL query resolvers
  • Mutation resolvers
  • Filtering functionality
  • Error handling

📈 Performance Comparison

FeatureGraphQL KickstartNetflix DGSSpring GraphQL
Learning CurveModerateSteepEasy (Spring devs)
PerformanceGoodExcellentGood
CommunityActiveGrowingOfficial Spring
DocumentationComprehensiveGoodExcellent
Testing SupportGoodExcellentGood
Schema GenerationSchema-firstCode-firstSchema-first
Spring IntegrationGoodExcellentNative

🛠️ Technology Stack

  • 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

🤝 Contributing

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.

Development Setup

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests (mvn test)
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

Code Style

  • Follow Java coding conventions
  • Ensure all tests pass
  • Maintain test coverage above 80%
  • Update documentation for new features

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙋‍♂️ Support

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

🌟 Acknowledgments

About

Demo application illustrating advanced usage of GraphQL with Spring Boot like filtering or relationship fetching

Topics

Resources

Stars

Watchers

Forks

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp