Spring for GraphQL simplifies the development of GraphQL APIs by integrating with Spring Boot. This guide demonstrates how to create a GraphQL API using Java and Kotlin, with examples for schema definition, query handling, and dependency injection.
🌟 Why Use GraphQL?
GraphQL allows clients to request specific data, reducing over-fetching and under-fetching compared to REST. It also supports strong typing and facilitates efficient data fetching, making it a popular choice for modern APIs.
🌟 Prerequisites
📋 Ensure you have the following:
- ☕ Java Development Kit (JDK) 17+
- 📦 Maven or Gradle installed
- 🔤 A Java IDE (e.g., IntelliJ IDEA, Eclipse)
🛠️ Step 1: Add Dependencies
Include the following dependencies in your project to enable Spring for GraphQL.
- Maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-graphql</artifactId> </dependency>
- Gradle:
implementation 'org.springframework.boot:spring-boot-starter-graphql'
📋 Step 2: Define the GraphQL Schema
Create a schema file named schema.graphqls
under the src/main/resources/graphql
directory.
type Query { getUser(id: ID!): User getUsers: [User] } type User { id: ID! name: String! email: String! }
📖 Step 3: Implement the Data Model and Services
Define the data model and service layer for handling queries.
Entity
package com.example.demo.model; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class User { private String id; private String name; private String email; }
Service
package com.example.demo.service; import com.example.demo.model.User; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; @Service public class UserService { private final Map<String, User> userData = Stream.of( new User("1", "Alice", "alice@example.com"), new User("2", "Bob", "bob@example.com") ).collect(Collectors.toMap(User::getId, user -> user)); public User getUser(String id) { return userData.get(id); } public List<User> getUsers() { return List.copyOf(userData.values()); } }
Entity
package com.example.demo.model data class User( val id: String, val name: String, val email: String )
Service
package com.example.demo.service import com.example.demo.model.User import org.springframework.stereotype.Service @Service class UserService { private val userData = mapOf( "1" to User("1", "Alice", "alice@example.com"), "2" to User("2", "Bob", "bob@example.com") ) fun getUser(id: String): User? = userData[id] fun getUsers(): List<User> = userData.values.toList() }
📘 Step 4: Implement GraphQL Controllers
Controllers handle GraphQL queries and mutations. Use the @Controller
annotation in Spring for GraphQL.
package com.example.demo.controller; import com.example.demo.model.User; import com.example.demo.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.graphql.data.method.annotation.QueryMapping; import org.springframework.stereotype.Controller; import java.util.List; @Controller @RequiredArgsConstructor public class UserController { private final UserService userService; @QueryMapping public User getUser(String id) { return userService.getUser(id); } @QueryMapping public List<User> getUsers() { return userService.getUsers(); } }
package com.example.demo.controller import com.example.demo.model.User import com.example.demo.service.UserService import org.springframework.graphql.data.method.annotation.QueryMapping import org.springframework.stereotype.Controller @Controller class UserController( private val userService: UserService ) { @QueryMapping fun getUser(id: String): User? = userService.getUser(id) @QueryMapping fun getUsers(): List<User> = userService.getUsers() }
▶️ Running the Application
Run the application using the following commands:
-
Spring Boot (Java/Kotlin):
./mvnw spring-boot:run
Access the GraphQL Playground at http://localhost:8080/graphiql
to test your API.
🧪 Testing the GraphQL API
Here are some example queries to test your API:
- Fetch a user by ID:
query { getUser(id: "1") { id name email } }
- Fetch all users:
query { getUsers { id name email } }
This guide demonstrates how to build powerful and flexible GraphQL APIs using Spring for GraphQL with Java and Kotlin. Leverage GraphQL’s advantages to make your project more efficient and user-friendly.