-
Notifications
You must be signed in to change notification settings - Fork 656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #2818 #4358
Merged
martinbonnin
merged 4 commits into
apollographql:release-2.x
from
eduardb:fix-overwrites-cache-entries-when-using-fragments
Aug 29, 2022
Merged
Fix #2818 #4358
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9c09156
Fix overwrites cache entries when using fragments
eduardb c5aad89
Add integration test for fragment overwrites fix
eduardb d173e6d
Avoid using method not available before API level 24 on Android
eduardb 0c04a41
Add assertion to check that the fragment is also correctly written
eduardb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...c/main/graphql/com/apollographql/apollo/integration/fragmentoverwrites/operations.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
query Home { | ||
home { | ||
...sectionFragment | ||
sectionA { | ||
name | ||
} | ||
} | ||
} | ||
|
||
fragment sectionFragment on Home { | ||
sectionA { | ||
id | ||
imageUrl | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ation/src/main/graphql/com/apollographql/apollo/integration/fragmentoverwrites/schema.sdl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
type Query { | ||
home: Home! | ||
} | ||
|
||
type Home { | ||
sectionA: SectionA | ||
} | ||
|
||
type SectionA { | ||
id: String! | ||
name: String! | ||
imageUrl: String | ||
} |
96 changes: 96 additions & 0 deletions
96
apollo-integration/src/test/java/com/apollographql/apollo/FragmentOverwritesTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.apollographql.apollo | ||
|
||
import com.apollographql.apollo.Utils.immediateExecutor | ||
import com.apollographql.apollo.Utils.immediateExecutorService | ||
import com.apollographql.apollo.Utils.mockResponse | ||
import com.apollographql.apollo.api.Operation | ||
import com.apollographql.apollo.api.ResponseField | ||
import com.apollographql.apollo.cache.normalized.CacheKey | ||
import com.apollographql.apollo.cache.normalized.CacheKeyResolver | ||
import com.apollographql.apollo.cache.normalized.lru.EvictionPolicy | ||
import com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory | ||
import com.apollographql.apollo.coroutines.await | ||
import com.apollographql.apollo.fetcher.ApolloResponseFetchers | ||
import com.apollographql.apollo.integration.fragmentoverwrites.HomeQuery | ||
import com.apollographql.apollo.integration.fragmentoverwrites.fragment.SectionFragment | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Dispatcher | ||
import okhttp3.OkHttpClient | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class FragmentOverwritesTest { | ||
|
||
private lateinit var apolloClient: ApolloClient | ||
|
||
@get:Rule | ||
val server = MockWebServer() | ||
|
||
private val cacheKeyResolver = object : CacheKeyResolver() { | ||
override fun fromFieldArguments(field: ResponseField, variables: Operation.Variables): CacheKey { | ||
return CacheKey.NO_KEY | ||
} | ||
|
||
override fun fromFieldRecordSet(field: ResponseField, recordSet: Map<String, Any>): CacheKey { | ||
return (recordSet["id"] as? String)?.let { CacheKey.from(it) } ?: CacheKey.NO_KEY | ||
} | ||
} | ||
|
||
@Before | ||
fun setup() { | ||
val okHttpClient = OkHttpClient.Builder() | ||
.dispatcher(Dispatcher(immediateExecutorService())) | ||
.build() | ||
|
||
apolloClient = ApolloClient.builder() | ||
.normalizedCache(LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION), cacheKeyResolver) | ||
.okHttpClient(okHttpClient) | ||
.dispatcher(immediateExecutor()) | ||
.serverUrl(server.url("/")) | ||
.build() | ||
} | ||
|
||
@Test | ||
fun `doesn't overwrite cache entries when using fragments`() { | ||
|
||
server.enqueue(mockResponse("FragmentOverwritesTestHomeQueryResponse.json")) | ||
|
||
runBlocking { | ||
val networkResponse = apolloClient.query(HomeQuery()).await() | ||
|
||
assertThat(networkResponse.data?.home?.sectionA?.name).isEqualTo("initialSectionName") | ||
assertThat(networkResponse.data?.home?.fragments?.sectionFragment?.sectionA?.imageUrl).isEqualTo("initialUrl") | ||
|
||
apolloClient.apolloStore.writeAndPublish( | ||
HomeQuery(), | ||
HomeQuery.Data( | ||
HomeQuery.Home( | ||
sectionA = HomeQuery.SectionA( | ||
name = "modifiedSectionName" | ||
), | ||
fragments = HomeQuery.Home.Fragments( | ||
sectionFragment = SectionFragment( | ||
sectionA = SectionFragment.SectionA( | ||
id = "section-id", | ||
imageUrl = "modifiedUrl", | ||
), | ||
) | ||
) | ||
) | ||
) | ||
).execute() | ||
|
||
val cacheResponse = apolloClient.query(HomeQuery()) | ||
.toBuilder() | ||
.responseFetcher(ApolloResponseFetchers.CACHE_ONLY) | ||
.build() | ||
.await() | ||
|
||
assertThat(cacheResponse.data?.home?.sectionA?.name).isEqualTo("modifiedSectionName") | ||
assertThat(cacheResponse.data?.home?.fragments?.sectionFragment?.sectionA?.imageUrl).isEqualTo("modifiedUrl") | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
apollo-integration/src/test/resources/FragmentOverwritesTestHomeQueryResponse.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"data": { | ||
"home": { | ||
"__typename": "Home", | ||
"sectionA": { | ||
"__typename": "SectionA", | ||
"name": "initialSectionName", | ||
"id": "section-id", | ||
"imageUrl": "initialUrl" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a check that the fragment is correctly written?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 0c04a41. Thanks!