Skip to content

Commit

Permalink
[3.x] Cleanup ApolloStore API (#2880)
Browse files Browse the repository at this point in the history
* cosmetics

* remove ReadableStore.readOperationInternal

* remove RealApolloStore.doWriteFragment

* remove ApolloStoreOperation

* fix tests

* actually remove ApolloStoreOperation
  • Loading branch information
martinbonnin committed Jan 19, 2021
1 parent d7fbd96 commit 6636d59
Show file tree
Hide file tree
Showing 25 changed files with 350 additions and 722 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.apollographql.apollo.integration.normalizer.EpisodeHeroNameWithIdQuer
import com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery
import com.apollographql.apollo.integration.normalizer.StarshipByIdQuery
import com.apollographql.apollo.integration.normalizer.type.Episode
import com.apollographql.apollo.internal.RealApolloStore
import com.google.common.truth.Truth
import com.google.common.truth.Truth.assertThat
import junit.framework.Assert
Expand Down Expand Up @@ -66,7 +67,7 @@ class ApolloWatcherTest {
@Test
fun testQueryWatcherUpdated_SameQuery_DifferentResults() {
val heroNameList: MutableList<String> = ArrayList()
val query: EpisodeHeroNameQuery = EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE))
val query = EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE))
server.enqueue(Utils.mockResponse("EpisodeHeroNameResponseWithId.json"))
val watcher = apolloClient.query(query).watcher()
watcher.enqueueAndWatch(
Expand Down Expand Up @@ -112,14 +113,13 @@ class ApolloWatcherTest {
assertThat(channel.receiveOrTimeout()?.hero?.name).isEqualTo("R2-D2")

// Someone writes to the store directly
val changedKeys: Set<String> = apolloClient.apolloStore.writeTransaction(object : Transaction<WriteableStore, Set<String>> {
override fun execute(cache: WriteableStore): Set<String> {
val changedKeys: Set<String> = (apolloClient.apolloStore as RealApolloStore).writeTransaction {
val record: Record = Record.builder("2001")
.addField("name", "Artoo")
.build()
return cache.merge(listOf(record), CacheHeaders.NONE)
}
})
it.merge(listOf(record), CacheHeaders.NONE)
}

apolloClient.apolloStore.publish(changedKeys)
assertThat(channel.receiveOrTimeout()?.hero?.name).isEqualTo("Artoo")

Expand Down Expand Up @@ -321,7 +321,7 @@ class ApolloWatcherTest {
Assert.fail(e.message)
}
})
Truth.assertThat(watchedHeroes).hasSize(1)
assertThat(watchedHeroes).hasSize(1)
assertThat(watchedHeroes[0]?.name).isEqualTo("R2-D2")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class CoroutinesApolloTest {
.toFlow()
.first() // Cancels the flow after first response

apolloClient.clearNormalizedCache()
apolloClient.apolloStore.clearAll()
apolloClient.clearHttpCache()

server.enqueue(mockResponse("HeroNameResponse.json"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.apollographql.apollo.cache.CacheHeaders.Companion.builder
import com.apollographql.apollo.cache.http.ApolloHttpCache
import com.apollographql.apollo.cache.http.DiskLruHttpCacheStore
import com.apollographql.apollo.cache.http.internal.FileSystem
import com.apollographql.apollo.coroutines.await
import com.apollographql.apollo.exception.ApolloException
import com.apollographql.apollo.exception.ApolloHttpException
import com.apollographql.apollo.integration.httpcache.AllFilmsQuery
Expand All @@ -21,6 +22,7 @@ import com.apollographql.apollo.integration.httpcache.DroidDetailsQuery
import com.apollographql.apollo.integration.httpcache.type.CustomScalars
import com.apollographql.apollo.rx2.Rx2Apollo
import com.google.common.truth.Truth
import kotlinx.coroutines.runBlocking
import okhttp3.Dispatcher
import okhttp3.Interceptor
import okhttp3.OkHttpClient
Expand All @@ -39,6 +41,7 @@ import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.concurrent.TimeUnit
import kotlin.test.assertTrue

class HttpCacheTest {
private lateinit var apolloClient: ApolloClient
Expand Down Expand Up @@ -472,12 +475,12 @@ class HttpCacheTest {

@Test
@Throws(Exception::class)
fun networkFirst() {
fun networkFirst() = runBlocking {
enqueueResponse("/HttpCacheTestAllPlanets.json")
Rx2Apollo.from(apolloClient
.query(AllPlanetsQuery()))
.test()
.assertValue { response -> !response.hasErrors() }

val response = apolloClient.query(AllPlanetsQuery()).await()
Truth.assertThat(response.hasErrors()).isFalse()

Truth.assertThat(server.requestCount).isEqualTo(1)
Truth.assertThat(lastHttResponse!!.networkResponse()).isNotNull()
Truth.assertThat(lastHttResponse!!.cacheResponse()).isNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class NormalizedCacheTestCase {
val heroWithFriendsFragment = apolloClient.apolloStore.readFragment(
HeroWithFriendsFragmentImpl(),
from("2001"),
).execute()
)!!
assertThat(heroWithFriendsFragment.id).isEqualTo("2001")
assertThat(heroWithFriendsFragment.name).isEqualTo("R2-D2")
assertThat(heroWithFriendsFragment.friends).hasSize(3)
Expand All @@ -379,22 +379,22 @@ class NormalizedCacheTestCase {
var fragment: HumanWithIdFragment = apolloClient.apolloStore.readFragment(
HumanWithIdFragmentImpl(),
from("1000"),
).execute()
)!!

assertThat(fragment.id).isEqualTo("1000")
assertThat(fragment.name).isEqualTo("Luke Skywalker")

fragment = apolloClient.apolloStore.readFragment(
HumanWithIdFragmentImpl(),
from("1002"),
).execute()
)!!
assertThat(fragment.id).isEqualTo("1002")
assertThat(fragment.name).isEqualTo("Han Solo")

fragment = apolloClient.apolloStore.readFragment(
HumanWithIdFragmentImpl(),
from("1003"),
).execute()
)!!
assertThat(fragment.id).isEqualTo("1003")
assertThat(fragment.name).isEqualTo("Leia Organa")
}
Expand Down Expand Up @@ -450,7 +450,7 @@ class NormalizedCacheTestCase {
}

// test remove root query object
Truth.assertThat(apolloClient.apolloStore.remove(from("2001")).execute()).isTrue()
Truth.assertThat(apolloClient.apolloStore.remove(from("2001"))).isTrue()
assertResponse(
apolloClient.query(HeroAndFriendsNamesWithIDsQuery(fromNullable(Episode.NEWHOPE)))
.responseFetcher(ApolloResponseFetchers.CACHE_ONLY), Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>> { response ->
Expand All @@ -474,7 +474,7 @@ class NormalizedCacheTestCase {
}

// test remove object from the list
Truth.assertThat(apolloClient.apolloStore.remove(from("1002")).execute()).isTrue()
Truth.assertThat(apolloClient.apolloStore.remove(from("1002"))).isTrue()
assertResponse(
apolloClient.query(HeroAndFriendsNamesWithIDsQuery(fromNullable(Episode.NEWHOPE)))
.responseFetcher(ApolloResponseFetchers.CACHE_ONLY), Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>> { response ->
Expand Down Expand Up @@ -535,8 +535,7 @@ class NormalizedCacheTestCase {
assertThat(response.data!!.character!!.name).isEqualTo("Leia Organa")
true
}
Truth.assertThat(apolloClient.apolloStore.remove(Arrays.asList(from("1002"), from("1000")))
.execute()).isEqualTo(2)
Truth.assertThat(apolloClient.apolloStore.remove(Arrays.asList(from("1002"), from("1000")))).isEqualTo(2)
assertResponse(
apolloClient.query(CharacterNameByIdQuery("1000")).responseFetcher(ApolloResponseFetchers.CACHE_ONLY),
Predicate<Response<CharacterNameByIdQuery.Data>> { response ->
Expand Down Expand Up @@ -734,7 +733,7 @@ LruNormalizedCache {
}

// test remove root query object
assertThat(apolloClient.apolloStore.remove(from("2001"), true).execute()).isTrue()
assertThat(apolloClient.apolloStore.remove(from("2001"), true)).isTrue()
assertResponse(
apolloClient.query(HeroAndFriendsNamesWithIDsQuery(fromNullable(Episode.NEWHOPE)))
.responseFetcher(ApolloResponseFetchers.CACHE_ONLY), Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>> { response ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class OptimisticCacheTestCase {
)
)
))
apolloClient!!.apolloStore.writeOptimisticUpdatesAndPublish(query, data, mutationId).execute()
apolloClient!!.apolloStore.writeOptimisticUpdates(query, data, mutationId, true)
assertResponse(
apolloClient!!.query(query).responseFetcher(ApolloResponseFetchers.CACHE_ONLY)
) { (_, data1) ->
Expand All @@ -84,7 +84,7 @@ class OptimisticCacheTestCase {
assertThat(data1.hero?.friends?.get(1)?.name).isEqualTo("Batman")
true
}
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId).execute()
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId, false)
assertResponse(
apolloClient!!.query(query).responseFetcher(ApolloResponseFetchers.CACHE_ONLY)
) { (_, data1) ->
Expand All @@ -102,14 +102,15 @@ class OptimisticCacheTestCase {
fun two_optimistic_two_rollback() {
val query1 = HeroAndFriendsNamesWithIDsQuery(fromNullable(Episode.JEDI))
val mutationId1 = UUID.randomUUID()
val query2 = HeroNameWithIdQuery()
val mutationId2 = UUID.randomUUID()

// execute query1 from the network
enqueueAndAssertResponse(
server,
"HeroAndFriendsNameWithIdsResponse.json",
apolloClient!!.query(query1),
Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>> { response -> !response.hasErrors() }
)
apolloClient!!.query(query1)
) { response -> !response.hasErrors() }

// now write some optimistic updates for query1
val data1 = HeroAndFriendsNamesWithIDsQuery.Data(
HeroAndFriendsNamesWithIDsQuery.Data.Hero(
"2001",
Expand All @@ -126,7 +127,7 @@ class OptimisticCacheTestCase {
)
)
)
apolloClient!!.apolloStore.writeOptimisticUpdatesAndPublish(query1, data1, mutationId1).execute()
apolloClient!!.apolloStore.writeOptimisticUpdates(query1, data1, mutationId1, true)

// check if query1 see optimistic updates
assertResponse(
Expand All @@ -141,19 +142,26 @@ class OptimisticCacheTestCase {
assertThat(data.hero?.friends?.get(1)?.name).isEqualTo("Batman")
true
}

// execute query2
val query2 = HeroNameWithIdQuery()
val mutationId2 = UUID.randomUUID()
enqueueAndAssertResponse(
server,
"HeroNameWithIdResponse.json",
apolloClient!!.query(query2),
Predicate<Response<HeroNameWithIdQuery.Data>> { response -> !response.hasErrors() }
)
apolloClient!!.query(query2)
) {
response -> !response.hasErrors()
}

// write optimistic data2
val data2 = HeroNameWithIdQuery.Data(HeroNameWithIdQuery.Data.Hero(
"1000",
"Beast"
))
apolloClient!!.apolloStore.writeOptimisticUpdatesAndPublish(query2, data2, mutationId2).execute()
apolloClient!!.apolloStore.writeOptimisticUpdates(query2, data2, mutationId2, true)

// check if query1 see the latest optimistic updates
// check if query1 sees data2
assertResponse(
apolloClient!!.query(query1).responseFetcher(ApolloResponseFetchers.CACHE_ONLY)
) { (_, data) ->
Expand All @@ -167,7 +175,7 @@ class OptimisticCacheTestCase {
true
}

// check if query2 see the latest optimistic updates
// check if query2 sees data2
assertResponse(
apolloClient!!.query(query2).responseFetcher(ApolloResponseFetchers.CACHE_ONLY)
) { (_, data) ->
Expand All @@ -176,10 +184,10 @@ class OptimisticCacheTestCase {
true
}

// rollback query1 optimistic updates
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId1).execute()
// rollback data1
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId1, false)

// check if query1 see the latest optimistic updates
// check if query2 sees the rollback
assertResponse(
apolloClient!!.query(query1).responseFetcher(ApolloResponseFetchers.CACHE_ONLY)
) { (_, data) ->
Expand All @@ -205,7 +213,7 @@ class OptimisticCacheTestCase {
}

// rollback query2 optimistic updates
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId2).execute()
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId2, false)

// check if query2 see the latest optimistic updates
assertResponse(
Expand All @@ -230,8 +238,9 @@ class OptimisticCacheTestCase {
apolloClient!!.apolloStore.writeOptimisticUpdates(
HeroNameQuery(),
HeroNameQuery.Data(HeroNameQuery.Data.Hero("R22-D22")),
mutationId
).execute()
mutationId,
false
)
assertResponse(
apolloClient!!.query(HeroNameWithEnumsQuery()).responseFetcher(ApolloResponseFetchers.CACHE_ONLY)
) { (_, data) ->
Expand All @@ -240,7 +249,7 @@ class OptimisticCacheTestCase {
assertThat(data.hero?.appearsIn).isEqualTo(Arrays.asList(Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI))
true
}
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId).execute()
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId, false)
assertResponse(
apolloClient!!.query(HeroNameWithEnumsQuery()).responseFetcher(ApolloResponseFetchers.CACHE_ONLY)
) { (_, data) ->
Expand Down Expand Up @@ -269,7 +278,7 @@ class OptimisticCacheTestCase {
val updateReviewMutation = UpdateReviewMutation(
"empireReview2",
ReviewInput(
commentary = Input.fromNullable("Great"),
commentary = fromNullable("Great"),
stars = 5,
favoriteColor = ColorInput()
)
Expand All @@ -281,7 +290,7 @@ class OptimisticCacheTestCase {
override fun onFailure(e: ApolloException) {}
}
)
Truth.assertThat(watcherData).hasSize(3)
assertThat(watcherData).hasSize(3)

// before mutation and optimistic updates
assertThat(watcherData[0].reviews).hasSize(3)
Expand Down Expand Up @@ -355,12 +364,12 @@ class OptimisticCacheTestCase {
)
)
)
apolloClient!!.apolloStore.writeOptimisticUpdatesAndPublish(query1, data1, mutationId1).execute()
apolloClient!!.apolloStore.writeOptimisticUpdates(query1, data1, mutationId1, true)
val data2 = HeroNameWithIdQuery.Data(HeroNameWithIdQuery.Data.Hero(
"1000",
"Spiderman"
))
apolloClient!!.apolloStore.writeOptimisticUpdatesAndPublish(query2, data2, mutationId2).execute()
apolloClient!!.apolloStore.writeOptimisticUpdates(query2, data2, mutationId2, true)

// check if query1 see optimistic updates
assertResponse(
Expand All @@ -386,7 +395,7 @@ class OptimisticCacheTestCase {
}

// rollback query2 optimistic updates
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId2).execute()
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId2, false)

// check if query1 see the latest optimistic updates
assertResponse(
Expand All @@ -412,7 +421,7 @@ class OptimisticCacheTestCase {
}

// rollback query1 optimistic updates
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId1).execute()
apolloClient!!.apolloStore.rollbackOptimisticUpdates(mutationId1, false)

// check if query1 see the latest non-optimistic updates
assertResponse(
Expand Down

0 comments on commit 6636d59

Please sign in to comment.