728x90
개발 환경 설정
- GraphQL SPQR 라이브러리 추가
implementation("io.leangen.graphql:graphql-spqr-spring-boot-starter:0.0.6")
implementation("com.graphql-java-kickstart:graphql-java-tools:11.0.0")
기본 설정 오류
{
"errors": [
{
"message": "Exception while fetching data (/getErrorUser) : 사용자 조회 실패",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"getErrorUser"
],
"extensions": {
"classification": "DataFetchingException"
}
}
],
"data": {
"getErrorUser": null
}
}
필요한 정보만 보여주기
- GraphQL Configuration 추가
@Configuration
class GraphQLConfiguration {
@Bean
fun graphQL(schema: GraphQLSchema?): GraphQL? {
return GraphQL.newGraphQL(schema)
.queryExecutionStrategy(AsyncExecutionStrategy(CustomDataFetcherExceptionHandler()))
.mutationExecutionStrategy(AsyncSerialExecutionStrategy(CustomDataFetcherExceptionHandler()))
.build()
}
}
class CustomDataFetcherExceptionHandler : DataFetcherExceptionHandler {
override fun onException(handlerParameters: DataFetcherExceptionHandlerParameters): DataFetcherExceptionHandlerResult {
handlerParameters.exception.printStackTrace()
val errors = when (val exception = handlerParameters.exception) {
is ConstraintViolationException -> {
exception.constraintViolations.map { CustomGraphQLError(code = it.propertyPath.toString(), errorMessage= it.message) }
}
else -> listOf(CustomGraphQLError(errorMessage = exception.message ?: "error"))
}
return DataFetcherExceptionHandlerResult.newResult()
.errors(errors)
.build()
}
}
class CustomGraphQLError(private val errorMessage: String, private val code: String = "", private val detailMessage: String = ""): GraphQLError {
override fun getMessage(): String {
return errorMessage
}
override fun getLocations(): MutableList<SourceLocation> {
return mutableListOf()
}
override fun getErrorType(): ErrorClassification? {
return null
}
override fun getExtensions(): Map<String, Any> {
return mapOf<String, Any>("code" to code, "detailMessage" to detailMessage)
}
}
- 오류 발생
- CustomGraphQLError 클래스를 수정하면 locations, extensions 등을 수정할 수 있다.
{
"errors": [
{
"message": "사용자 조회 실패",
"locations": [],
"extensions": {
"code": "",
"detailMessage": ""
}
}
],
"data": {
"getErrorUser": null
}
}
코드 참고
728x90
'GraphQL 스터디 > SPQR 라이브러리' 카테고리의 다른 글
GraphQL Custom Validation 추가 (0) | 2023.11.10 |
---|---|
GraphQL Validation 추가 (0) | 2023.11.10 |