- Why GraphQL: Provides a unified data layer from multiple backend sources into a single API that can evolve with client apps.
- Introduction: Show how to create an effective app with GraphQL using Netflix DGS framework in Spring Boot. Netflix DGS is an annotation-based GraphQL Java library on Spring Boot, allowing code generation from schemas, simplifying unit tests and supporting websockets, file uploads, and GraphQL federation.
- Tech Stack: Java 17, Spring Boot 3.4.3, Netflix DGS GraphQL 10.0.4, JPA, Maven, Lombok, JUnit, H2 Database.
- Application Development and Analysis: Create Spring Boot app using Spring Initializr. Include required dependencies like spring-boot-starter-data-jpa, spring-boot-starter-validation, etc. in pom.xml. Schema files should be in /src/main/resources/schemas and are automatically detected by Netflix DGS.
- Grapql Schema: Defined types, queries (user, users), and mutations (createUser, updateUser, deleteUser) in schema.graphql. Can use DGS plugin to generate Java code or prefer Lombok annotations.
- Application Structure: Has domains (defined in schema), repositories (JPA for database communication), services (between fetchers and repositories), and fetchers (central part with @DgsComponent and @DgsData annotations for queries and mutations, using service methods and input arguments).
Implementation:
- Domain: Defines domain objects from schema.
- Repository: Defines JPA repositories for database interaction.
- Service: Intermediate layer between fetchers and repository.
- Fetcher: Defines queries and mutations based on schema, using @DgsComponent and @DgsData annotations and service methods.
- Database: Uses inmemory H2 database with configuration in application.properties and data.sql.
- Tests: In /graphql-spring-boot/src/test/java/rs/karajovic/milan/graphql_spring_boot/fetcher for queries and mutations.
- Run Application in Your Development Environment: Access H2 database at http://localhost:8080/h2-console and GraphiQL dashboard at http://localhost:8080/graphiql.
- Build and Run Application Using Docker: Install Docker, download source code, build with Maven (mvn clean install), create image with docker image build -t graphql-spring-boot., start application with docker-compose up, and access GraphiQL dashboard at http://localhost/graphiql.
- GraphiQL Dashboard: Shows all queries and mutations. Can test by showing all users, creating a user, showing updated users, and deleting a user.
- Contact and Support: Author is Milan Karajovic, with GitHub https://github.com/Milan-Karajovic/GraphQL-SpringBoot and LinkedIn https://www.linkedin.com/in/milan-karajovic-3a1243359/.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。