Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/migration/src/commonMain/graphql/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ query RepositoryListQuery {
id
name
}
owner {
id
name
repositories {
id
stars
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/migration/src/commonMain/graphql/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Repository {
id: ID!
stars: Int!
starGazers: [User!]!
owner: User!
}

type Project {
Expand Down
61 changes: 43 additions & 18 deletions tests/migration/src/commonTest/kotlin/MigrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ private val REPOSITORY_LIST_RESPONSE = """
"id": "1",
"name": "Jane"
}
],
"owner": {
"__typename": "User",
"id": "2",
"name": "Owner",
"repositories": [
{
"__typename": "Repository",
"id": "0",
"stars": 10
}
]
}
}
]
}
Expand All @@ -69,9 +81,21 @@ private val REPOSITORY_LIST_DATA = RepositoryListQuery.Data(
RepositoryListQuery.StarGazer(id = "0", name = "John", __typename = "User"),
RepositoryListQuery.StarGazer(id = "1", name = "Jane", __typename = "User"),
),
__typename = "Repository"
)
)
__typename = "Repository",
owner = RepositoryListQuery.Owner(
id = "2",
name = "Owner",
__typename = "User",
repositories = listOf(
RepositoryListQuery.Repository1(
id = "0",
stars = 10,
__typename = "Repository",
),
),
),
),
),
)

class MigrationTest {
Expand Down Expand Up @@ -172,35 +196,36 @@ private suspend fun CacheManager.migrateFrom(legacyStore: LegacyApolloStore) {
}

private fun LegacyNormalizedCache.allRecordsSequence(): Sequence<LegacyRecord> {
suspend fun SequenceScope<LegacyRecord>.yieldRecordsRecursively(cache: LegacyNormalizedCache, cacheKeys: List<LegacyCacheKey>) {
for (cacheKeysChunk in cacheKeys.chunked(50)) {
val records = cache.loadRecords(cacheKeysChunk.map{it.key}, LegacyCacheHeaders.NONE)
val visited = mutableSetOf<String>()
suspend fun SequenceScope<LegacyRecord>.yieldRecordsRecursively(cacheKeys: Sequence<LegacyCacheKey>) {
val unvisitedCacheKeys = cacheKeys.filter { visited.add(it.key) }
for (cacheKeysChunk in unvisitedCacheKeys.chunked(50)) {
val records = loadRecords(cacheKeysChunk.map { it.key }, LegacyCacheHeaders.NONE)
yieldAll(records)
val references = records.flatMap{it.references()}
yieldRecordsRecursively(cache, references)
val references = records.asSequence().flatMap { it.references() }
yieldRecordsRecursively(references)
}
}
return sequence {
yieldRecordsRecursively(this@allRecordsSequence, listOf(LegacyCacheKey.rootKey()))
yieldRecordsRecursively(sequenceOf(LegacyCacheKey.rootKey()))
}
}

private fun LegacyRecord.references(): List<LegacyCacheKey> {
fun LegacyRecordValue.references(): List<LegacyCacheKey> {
return when (this) {
is LegacyCacheKey -> listOf(this)
is List<*> -> this.flatMap { it.references() }
is Map<*, *> -> this.values.flatMap { it.references() }
else -> emptyList()
private fun LegacyRecord.references(): Sequence<LegacyCacheKey> {
fun LegacyRecordValue.valueReferences(): Sequence<LegacyCacheKey> = sequence {
when (val value = this@valueReferences) {
is LegacyCacheKey -> yield(value)
is List<*> -> yieldAll(value.flatMap { it.valueReferences() })
is Map<*, *> -> yieldAll(value.values.flatMap { it.valueReferences() })
}
}
return fields.values.flatMap { it.references() }
return fields.values.asSequence().flatMap { it.valueReferences() }
}

private fun LegacyRecord.toRecord(): Record = Record(
key = CacheKey(key),
fields = fields.mapValues { (_, value) -> value.toRecordValue() },
mutationId = mutationId
mutationId = mutationId,
)

private fun LegacyRecordValue.toRecordValue(): RecordValue = when (this) {
Expand Down