적용 이유
친구와 사이드 프로젝트를 시작했다. 자바로 백엔드를 만들기로 했으며, Rest API 대신 Graphql을 통해 구현해 볼 것을 제안했다. 이유는 Graphql이 Rest API에 비해 언더패치와 오버패치를 방지할 수 있다는 장점이 있기 때문이었다.
Spring 적용
[ build.gradle 파일에 추가 ]
implementation 'org.springframework.boot:spring-boot-starter-graphql'
[ application.yml 파일에 추가 ]
graphql:
servlet:
enabled: true
mapping: /graphql
corsEnabled: true
cors:
allowed-origins: http://localhost:8080
allowed-methods: GET, HEAD, POST, PATCH
exception-handlers-enabled: true
context-setting: PER_REQUEST_WITH_INSTRUMENTATION
async-mode-enabled: true
tools:
schema-location-pattern: "**/*.graphqls"
[ resource/schema.graphqls 파일 추가 ]
type Query {
userById(id: Int!): User
}
type User {
id : Int!
email : String
name : String
}
[ UserController 추가 ]
@QueryMapping
public User userById(@Argument Long id) {
return userService.getUser(id);
}
결과
Java Spring에 Graphql을 적용한 이후 Curl에 명령어를 통해 결과를 확인했다. 정상적으로 요청한 데이터가 내려오는 것을 확인했다.
참고 링크
'개발일지 > TIL' 카테고리의 다른 글
[ 230619 ] Chained Exception ? (0) | 2023.06.19 |
---|---|
[ 230618 ] 사이드 프로젝트 Graphiql 적용하기 (0) | 2023.06.18 |
[ 230617 ] Java Spring WireMock 이슈 및 적용 (0) | 2023.06.17 |
[ 230616 ] 내가 느낀 페어 프로그래밍 관하여 (0) | 2023.06.16 |
[ 230615 ] Java 계산기 메서드 작성하기 (0) | 2023.06.15 |