GraphQL을 사용하기 편리하게 만들어주는 라이브러리입니다.
GraphQL을 사용해보기 위해 테스트 서버로 위 주소를 사용하겠습니다.
brew install apollo-cli
MacOS의 경우 brew를 통해 apollo Client를 설치해주면 편리합니다.
혹은 npm을 통해 설치도 가능합니다.
classpath 추가
buildscript {
...
dependencies {
...
classpath "com.apollographql.apollo:apollo-gradle-plugin:2.5.2"
}
}
plugin 추가
plugins {
...
id 'com.apollographql.apollo'
}
generateKotlinModels 활성화
android {
...
apollo {
generateKotlinModels = true
}
}
라이브러리 추가
dependencies {
...
implementation "com.apollographql.apollo:apollo-runtime:2.5.2"
implementation "com.apollographql.apollo:apollo-android-support:2.5.2"
}
안드로이드 스튜디오에서 위 이미지와 같이 java 패키지와 비슷하게 main에 GraphQL 패키지를 만들어주면 됩니다.
apollo schema:download --endpoint=https://rickandmortyapi.com/graphql schema.json
안드로이드 스튜디오 내부 터미널에서 위 명령어를 입력해주면 방금 만든 GraphQL 패키지에 schema.json 파일이 저장됩니다.
apollo schema:download --endpoint=http://localhost:8080/graphql --header="Authorization: Bearer <token>"
서버에서 token을 요청할 경우 위와 같이 header 넣어주시면 됩니다.
result.graphql
query Result {
characters {
results {
name
species
gender
image
}
}
}
파일을 생성하고 쿼리를 입력해줍니다.
그리고 Rebuild Project를 꼭 해줍시다.
val apolloClient = ApolloClient.builder()
.serverUrl("https://rickandmortyapi.com/graphql/")
.build()
apolloClient.query(ResultQuery())
.enqueue(object : ApolloCall.Callback<ResultQuery.Data>() {
override fun onResponse(response: Response<ResultQuery.Data>) {
...
}
override fun onFailure(e: ApolloException) {
...
}
})
RestAPI 라이브러리인 Retrofit2에 비해 초기 설정이 복잡하지만, RxJava나 코루틴을 같이 사용가능하며, Request 요청하는 부분이 간단하고, 필요한 데이터만 받을 수 있어 효율적으로 느껴졌습니다.
전체 코드는 Github를 참고해주세요.