How eh3rrera/graphql-java-spring-boot-example works
Project type and purpose
• This is a backend‐only Spring Boot application that exposes a GraphQL API.
• Purpose: serve a GraphQL endpoint for querying and mutating Book and Author data, backed by an in-memory H2 database.
File structure
• Top level: pom.xml, Maven wrapper files, application entry point (DemoGraphQlApplication.java).
• src/main/java/com/example/DemoGraphQL:
• model/ – domain entities (Book, Author)
• repository/ – Spring Data JPA interfaces (BookRepository, AuthorRepository)
• resolver/ – GraphQL resolvers (Query, Mutation, BookResolver)
• exception/ – custom exceptions and error adapter for GraphQL
• MyProperties.java – custom configuration properties
• src/main/resources:
• application.yml – Spring Boot config (H2 console, datasource)
• graphql/*.graphqls – schema definitions for Query/Mutation types
• src/test: unit tests for application startup and Query resolver
Key points from README
• Uses Java 11/17, Spring Boot, graphql-java and graphql-spring-boot-starter.
• Runs on embedded Tomcat at localhost:8080
• Exposes GraphiQL, Altair, Playground, Voyager UIs.
• H2 in-memory DB accessible via Spring’s H2 console.
Main components and interactions (
a. Client layer:
• Browser (GraphiQL/Altair/Playground/Voyager UIs)
• Third-party GraphQL clients or other services consuming the API
b. Web/API layer:
• Spring Boot embedded web server (Tomcat)
• HTTP endpoint “/graphql” handling POST GraphQL requests
c. GraphQL engine:
• graphql-java Tools (schema parser, runtime wiring)
• Schema files loaded from classpath (author.graphqls, book.graphqls)
d. Resolver layer:
• Query resolver (fetch list of books, single book by ID)
• Mutation resolver (newBook)
• Field resolver (BookResolver to fetch nested Author for a Book)
• GraphQLErrorAdapter (to map exceptions to GraphQL errors)
e. Persistence layer:
• Spring Data JPA repositories
• JPA Entities (Book, Author)
f. Database:
• H2 in-memory database (jdbc:h2:mem:testdb)
g. Configuration:
• application.yml for datasource and GraphQL servlet settings
• MyProperties for any custom config
Relationships and data flow
1. Client (via browser UI or external tool) sends HTTP POST to /graphql
2. Spring Boot dispatches to GraphQL servlet
3. graphql-java parses query against schema
4. Query/Mutation resolver methods invoked
5. Resolvers call Spring Data JPA repositories
6. Repositories interact with H2 database
7. Data returned up the chain, serialized to JSON GraphQL response
8. Any exceptions thrown by resolvers are adapted by GraphQLErrorAdapter
Architectural patterns / design principles
• Separation of concerns:
• Controllers (GraphQL servlet) vs. business logic (resolver) vs. persistence (repository)
• Dependency injection: Spring Boot wiring of resolvers and repositories
• Schema-first GraphQL: GraphQL SDL files define contract
• Exception handling adapter to unify error responses
Technologies / frameworks / libraries
• Java 11/17
• Spring Boot (web, data-jpa, configuration properties)
• graphql-java, graphql-spring-boot-starter, graphql-java-tools
• H2 Database
• Maven build (pom.xml)
Final checklist
• [ ] All components labeled (Client UIs, HTTP Endpoint, GraphQL Engine, Resolvers, Repositories, DB)
• [ ] Callouts for key configuration (application.yml, MyProperties) and SDL files
• [ ] Notes on exception handling (GraphQLErrorAdapter)