Skip to content

Commit d36ca8b

Browse files
Use Kotlin object keyword for apollo client
1 parent a730e97 commit d36ca8b

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

app/src/main/java/guide/graphql/toc/data/Apollo.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import com.apollographql.apollo.ApolloClient
44
import com.apollographql.apollo.cache.normalized.lru.EvictionPolicy
55
import com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory
66

7-
val cacheFactory =
8-
LruNormalizedCacheFactory(EvictionPolicy.builder().maxSizeBytes(10 * 1024 * 1024).build())
7+
object Apollo {
98

10-
val apolloClient: ApolloClient = ApolloClient.builder()
11-
.serverUrl("https://api.graphql.guide/graphql")
12-
.normalizedCache(cacheFactory)
13-
.build()
9+
val client: ApolloClient by lazy {
10+
val cacheFactory =
11+
LruNormalizedCacheFactory(EvictionPolicy.builder().maxSizeBytes(10 * 1024 * 1024).build())
12+
13+
ApolloClient.builder()
14+
.serverUrl("https://api.graphql.guide/graphql")
15+
.normalizedCache(cacheFactory)
16+
.build()
17+
}
18+
}

app/src/main/java/guide/graphql/toc/ui/chapters/ChaptersFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import com.apollographql.apollo.exception.ApolloException
1515
import com.google.android.material.transition.MaterialSharedAxis
1616
import guide.graphql.toc.ChaptersQuery
1717
import guide.graphql.toc.R
18-
import guide.graphql.toc.data.apolloClient
18+
import guide.graphql.toc.data.Apollo
1919
import guide.graphql.toc.databinding.ChaptersFragmentBinding
2020

2121
class ChaptersFragment : Fragment() {
@@ -73,7 +73,7 @@ class ChaptersFragment : Fragment() {
7373

7474
lifecycleScope.launchWhenStarted {
7575
try {
76-
val response = apolloClient.query(
76+
val response = Apollo.client.query(
7777
ChaptersQuery()
7878
).toDeferred().await()
7979
if (response.hasErrors()) {

app/src/main/java/guide/graphql/toc/ui/sections/SectionsFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import com.apollographql.apollo.coroutines.toDeferred
1313
import com.apollographql.apollo.exception.ApolloException
1414
import com.google.android.material.transition.MaterialSharedAxis
1515
import guide.graphql.toc.SectionsQuery
16-
import guide.graphql.toc.data.apolloClient
16+
import guide.graphql.toc.data.Apollo
1717
import guide.graphql.toc.databinding.SectionsFragmentBinding
1818

1919
class SectionsFragment : Fragment() {
@@ -66,7 +66,7 @@ class SectionsFragment : Fragment() {
6666
binding.spinner.visibility = View.VISIBLE
6767
binding.error.visibility = View.GONE
6868
try {
69-
val response = apolloClient.query(
69+
val response = Apollo.client.query(
7070
SectionsQuery(id = args.chapterId)
7171
).toDeferred().await()
7272

0 commit comments

Comments
 (0)