Skip to content

Commit

Permalink
make NormalizationIR internal
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Jan 12, 2021
1 parent 5654c19 commit 10338a5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
Expand Up @@ -6,7 +6,7 @@ import com.apollographql.apollo.api.ResponseField
/**
* A hierarchy of Json object with their fields attached used for normalization
*/
object NormalizationIR {
internal object NormalizationIR {
sealed class Element {
class Object(val fields: kotlin.collections.List<Field>) : Element() {
data class Field(val fieldKey: String, val field: ResponseField, val element: Element)
Expand All @@ -17,7 +17,7 @@ object NormalizationIR {
}
}

fun NormalizationIR.Element.mergeWith(other: NormalizationIR.Element): NormalizationIR.Element {
internal fun NormalizationIR.Element.mergeWith(other: NormalizationIR.Element): NormalizationIR.Element {
return when {
this is NormalizationIR.Element.Scalar && other is NormalizationIR.Element.Scalar -> {
check(this.value == other.value)
Expand Down Expand Up @@ -50,4 +50,4 @@ fun NormalizationIR.Element.mergeWith(other: NormalizationIR.Element): Normaliza
}
}

fun NormalizationIR.Element.Object.Field.mergeWith(other: NormalizationIR.Element.Object.Field) = copy(element = element.mergeWith(other.element))
internal fun NormalizationIR.Element.Object.Field.mergeWith(other: NormalizationIR.Element.Object.Field) = copy(element = element.mergeWith(other.element))
Expand Up @@ -11,7 +11,7 @@ import com.apollographql.apollo.api.internal.Utils.shouldSkip
/**
*
*/
class NormalizationIRResponseWriter(
internal class NormalizationIRResponseWriter(
private val operationVariables: Operation.Variables,
private val customScalarAdapters: CustomScalarAdapters,
) : ResponseWriter, ResponseWriter.ListItemWriter {
Expand Down
Expand Up @@ -9,12 +9,12 @@ import com.apollographql.apollo.cache.normalized.Record
/**
* Takes a AJObject and returns a list of [Record]
*/
class Normalizer(private val cacheKeyResolver: CacheKeyResolver) {
internal class Normalizer(private val cacheKeyResolver: CacheKeyResolver) {

private val records = mutableMapOf<String, Record>()

fun normalize(`object`: NormalizationIR.Element.Object, rootKey: String?): Map<String, Record> {
`object`.normalize(rootKey, null)
fun normalize(obj: NormalizationIR.Element.Object, rootKey: String?): Map<String, Record> {
obj.normalize(rootKey, null)
return records
}

Expand Down
@@ -1,6 +1,7 @@
package com.apollographql.apollo.cache.normalized.internal

import com.apollographql.apollo.api.CustomScalarAdapters
import com.apollographql.apollo.api.Fragment
import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.cache.normalized.Record
import com.apollographql.apollo.cache.normalized.CacheKeyResolver
Expand All @@ -15,6 +16,17 @@ fun <D: Operation.Data> Operation<D>.normalize(
return Normalizer(cacheKeyResolver).normalize(writer.root, null).values.toSet()
}

fun <D: Fragment.Data> Fragment<D>.normalize(
data: D,
customScalarAdapters: CustomScalarAdapters,
cacheKeyResolver: CacheKeyResolver,
rootKey: String
): Set<Record> {
val writer = NormalizationIRResponseWriter(variables(), customScalarAdapters)
adapter().toResponse(writer, data)
return Normalizer(cacheKeyResolver).normalize(writer.root, rootKey).values.toSet()
}

fun Set<Record>?.dependentKeys(): Set<String> {
return this?.flatMap {
it.keys() + it.key
Expand Down
Expand Up @@ -8,6 +8,7 @@ import com.apollographql.apollo.api.Response.Companion.builder
import com.apollographql.apollo.api.internal.ApolloLogger
import com.apollographql.apollo.api.internal.MapResponseReader
import com.apollographql.apollo.api.internal.ResponseAdapter
import com.apollographql.apollo.api.parseData
import com.apollographql.apollo.cache.CacheHeaders
import com.apollographql.apollo.cache.normalized.ApolloStore
import com.apollographql.apollo.cache.normalized.ApolloStore.RecordChangeSubscriber
Expand All @@ -23,9 +24,6 @@ import com.apollographql.apollo.cache.normalized.internal.ReadableStore
import com.apollographql.apollo.cache.normalized.internal.RealCacheKeyBuilder
import com.apollographql.apollo.cache.normalized.internal.Transaction
import com.apollographql.apollo.cache.normalized.internal.WriteableStore
import com.apollographql.apollo.api.parseData
import com.apollographql.apollo.cache.normalized.internal.Normalizer
import com.apollographql.apollo.cache.normalized.internal.NormalizationIRResponseWriter
import com.apollographql.apollo.cache.normalized.internal.dependentKeys
import com.apollographql.apollo.cache.normalized.internal.normalize
import java.util.ArrayList
Expand Down Expand Up @@ -192,7 +190,7 @@ class RealApolloStore(normalizedCache: NormalizedCache,
): ApolloStoreOperation<Set<String>> {
return object : ApolloStoreOperation<Set<String>>(dispatcher) {
override fun perform(): Set<String> {
val changedKeys = doWrite(operation, operationData, false, null)
val changedKeys = doWriteOperation(operation, operationData, false, null)
if (publish) {
publish(changedKeys)
}
Expand All @@ -213,7 +211,7 @@ class RealApolloStore(normalizedCache: NormalizedCache,
return object : ApolloStoreOperation<Set<String>>(dispatcher) {
override fun perform(): Set<String> {
return writeTransaction {
val changedKeys = doWrite(fragment.adapter(), cacheKey, fragment.variables(), fragmentData)
val changedKeys = doWriteFragment(fragment, cacheKey, fragmentData)
if (publish) {
publish(changedKeys)
}
Expand All @@ -227,7 +225,7 @@ class RealApolloStore(normalizedCache: NormalizedCache,
mutationId: UUID): ApolloStoreOperation<Set<String>> {
return object : ApolloStoreOperation<Set<String>>(dispatcher) {
override fun perform(): Set<String> {
return doWrite(operation, operationData, true, mutationId)
return doWriteOperation(operation, operationData, true, mutationId)
}
}
}
Expand All @@ -236,7 +234,7 @@ class RealApolloStore(normalizedCache: NormalizedCache,
mutationId: UUID): ApolloStoreOperation<Boolean> {
return object : ApolloStoreOperation<Boolean>(dispatcher) {
override fun perform(): Boolean {
val changedKeys = doWrite(operation, operationData, true, mutationId)
val changedKeys = doWriteOperation(operation, operationData, true, mutationId)
publish(changedKeys)
return java.lang.Boolean.TRUE
}
Expand Down Expand Up @@ -307,7 +305,7 @@ class RealApolloStore(normalizedCache: NormalizedCache,
})
}

fun <D : Operation.Data> doWrite(
fun <D : Operation.Data> doWriteOperation(
operation: Operation<D>,
operationData: D,
optimistic: Boolean,
Expand All @@ -325,10 +323,8 @@ class RealApolloStore(normalizedCache: NormalizedCache,

}

fun <D> doWrite(adapter: ResponseAdapter<D>, cacheKey: CacheKey, variables: Operation.Variables, value: D): Set<String> = writeTransaction {
val writer = NormalizationIRResponseWriter(variables, customScalarAdapters)
adapter.toResponse(writer, value)
val records = Normalizer(cacheKeyResolver).normalize(writer.root, cacheKey.key).values.toSet()
fun <D: Fragment.Data> doWriteFragment(fragment: Fragment<D>, cacheKey: CacheKey, data: D): Set<String> = writeTransaction {
val records = fragment.normalize(data, customScalarAdapters, cacheKeyResolver, cacheKey.key)
merge(records, CacheHeaders.NONE)
}
}

0 comments on commit 10338a5

Please sign in to comment.