dependencies {
implementation 'org.springframework.boot:spring-boot-starter-graphql'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework:spring-webflux'
testImplementation 'org.springframework.graphql:spring-graphql-test'
}
dependencies 설정에 'org.springframework.boot:spring-boot-starter-graphql'을 추가해주어야 사용 가능하다. 그 외 dependency들은 테스트를 하기 위한 설정들이다.
spring:
#DB설정부분 생략
graphql:
graphiql:
enabled: true #GraphQL 가능 여부
schema:
printer:
enabled: true
User정보를 바탕으로 GraphQL을 사용하는 테스트 코드를 작성하였습니다.
@Entity
@Getter
@Setter
public class User {
@Id
private String id;
private String phone;
private String email;
}
@GraphQlRepository
public interface UserRepository extends JpaRepository<User, String> {
}
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
public List<User> getUsers() {
return userRepository.findAll();
}
}
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class UserController {
private final UserService userService;
@QueryMapping
public List<User> getUsers() {
return userService.getUsers();
}
}
type Query {
getUsers:[User]
}
type User {
id:ID,
phone:String,
email:String
}
query {
getUsers {
id
}
}
front에서 직접적인 query를 입력함으로써 받는 데이터 용량을 더 줄일 수도 있군요.
좋은 글 감사합니다!