Skip to content
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

[Kotlin runtime] [WIP] normalized cache #2452

Closed
wants to merge 14 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.apollographql.apollo.api.internal

import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.api.ResponseField

class NoOpResolveDelegate<T>: ResolveDelegate<T> {
override fun willResolveRootQuery(operation: Operation<*, *, *>) {
}

override fun willResolve(field: ResponseField, variables: Operation.Variables, value: Any?) {
}

override fun didResolve(field: ResponseField, variables: Operation.Variables) {
}

override fun didResolveScalar(value: Any?) {
}

override fun willResolveObject(objectField: ResponseField, objectSource: T?) {
}

override fun didResolveObject(objectField: ResponseField, objectSource: T?) {
}

override fun didResolveList(array: List<*>) {
}

override fun willResolveElement(atIndex: Int) {
}

override fun didResolveElement(atIndex: Int) {
}

override fun didResolveNull() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.apollographql.apollo.api.ScalarTypeAdapters
import com.apollographql.apollo.api.internal.FieldValueResolver
import com.apollographql.apollo.api.internal.ResolveDelegate
import com.apollographql.apollo.api.internal.ResponseReader
import java.util.Collections

class RealResponseReader<R>(
val operationVariables: Operation.Variables,
Expand Down Expand Up @@ -144,7 +143,7 @@ class RealResponseReader<R>(
}.also { resolveDelegate.didResolveList(values) }
}
didResolve(field)
return if (result != null) Collections.unmodifiableList(result) else null
return result
}

override fun <T : Any> readCustomType(field: ResponseField.CustomTypeField): T? {
Expand Down Expand Up @@ -288,7 +287,7 @@ class RealResponseReader<R>(
}.also { resolveDelegate.didResolveElement(index) }
}
resolveDelegate.didResolveList(values)
return Collections.unmodifiableList(result)
return result
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.apollographql.apollo.internal.response

import com.apollographql.apollo.api.CustomTypeAdapter
import com.apollographql.apollo.api.BigDecimal
import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.api.ResponseField
import com.apollographql.apollo.api.ScalarType
import com.apollographql.apollo.api.ScalarTypeAdapters
import com.apollographql.apollo.api.internal.ResolveDelegate
import com.apollographql.apollo.api.internal.ResponseFieldMarshaller
import com.apollographql.apollo.api.internal.ResponseWriter
import java.math.BigDecimal
import java.util.ArrayList
import java.util.LinkedHashMap

class RealResponseWriter(private val operationVariables: Operation.Variables, private val scalarTypeAdapters: ScalarTypeAdapters) : ResponseWriter {
val buffer: MutableMap<String, FieldDescriptor> = LinkedHashMap()
Expand All @@ -19,15 +16,15 @@ class RealResponseWriter(private val operationVariables: Operation.Variables, pr
}

override fun writeInt(field: ResponseField, value: Int?) {
writeScalarFieldValue(field, if (value != null) BigDecimal.valueOf(value.toLong()) else null)
writeScalarFieldValue(field, if (value != null) BigDecimal(value.toLong()) else null)
}

override fun writeLong(field: ResponseField, value: Long?) {
writeScalarFieldValue(field, if (value != null) BigDecimal.valueOf(value) else null)
writeScalarFieldValue(field, if (value != null) BigDecimal(value) else null)
}

override fun writeDouble(field: ResponseField, value: Double?) {
writeScalarFieldValue(field, if (value != null) BigDecimal.valueOf(value) else null)
writeScalarFieldValue(field, if (value != null) BigDecimal(value) else null)
}

override fun writeBoolean(field: ResponseField, value: Boolean?) {
Expand Down Expand Up @@ -177,15 +174,15 @@ class RealResponseWriter(private val operationVariables: Operation.Variables, pr
}

override fun writeInt(value: Int?) {
accumulator.add(if (value != null) BigDecimal.valueOf(value.toLong()) else null)
accumulator.add(if (value != null) BigDecimal(value.toLong()) else null)
}

override fun writeLong(value: Long?) {
accumulator.add(if (value != null) BigDecimal.valueOf(value) else null)
accumulator.add(if (value != null) BigDecimal(value) else null)
}

override fun writeDouble(value: Double?) {
accumulator.add(if (value != null) BigDecimal.valueOf(value) else null)
accumulator.add(if (value != null) BigDecimal(value) else null)
}

override fun writeBoolean(value: Boolean?) {
Expand Down Expand Up @@ -220,10 +217,8 @@ class RealResponseWriter(private val operationVariables: Operation.Variables, pr
companion object {
private fun checkFieldValue(field: ResponseField, value: Any?) {
if (!field.optional && value == null) {
throw NullPointerException(String.format("Mandatory response field `%s` resolved with null value",
field.responseName))
throw NullPointerException("Mandatory response field `${field.responseName}` resolved with null value")
}
}
}

}
55 changes: 47 additions & 8 deletions apollo-normalized-cache/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
plugins {
`java-library`
kotlin("jvm")
kotlin("multiplatform")
}

dependencies {
api(project(":apollo-api"))
api(project(":apollo-normalized-cache-api"))
implementation(groovy.util.Eval.x(project, "x.dep.cache"))
implementation(groovy.util.Eval.x(project, "x.dep.kotlin.stdLib"))
kotlin {
@Suppress("ClassName")
data class iOSTarget(val name: String, val preset: String, val id: String)

testImplementation(groovy.util.Eval.x(project, "x.dep.junit"))
testImplementation(groovy.util.Eval.x(project, "x.dep.truth"))
val iosTargets = listOf(
iOSTarget("ios", "iosArm64", "ios-arm64"),
iOSTarget("iosSim", "iosX64", "ios-x64")
)

for ((targetName, presetName, id) in iosTargets) {
targetFromPreset(presets.getByName(presetName), targetName) {
mavenPublication {
artifactId = "${project.name}-$id"
}
}
}

jvm {
withJava()
}

sourceSets {
val commonMain by getting {
dependencies {
api(project(":apollo-api"))
api(project(":apollo-normalized-cache-api"))
implementation(kotlin("stdlib-common"))
}
}

val jvmMain by getting {
dependsOn(commonMain)
dependencies {
implementation(kotlin("stdlib"))
implementation(groovy.util.Eval.x(project, "x.dep.cache"))
}
}

val jvmTest by getting {
dependsOn(jvmMain)
dependencies {
implementation(groovy.util.Eval.x(project, "x.dep.junit"))
implementation(groovy.util.Eval.x(project, "x.dep.truth"))
}
}

}
}

tasks.withType<Javadoc> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.apollographql.apollo.api.ResponseField.Companion.isArgumentValueVaria
import com.apollographql.apollo.api.internal.json.JsonWriter
import com.apollographql.apollo.api.internal.json.Utils
import okio.Buffer
import java.io.IOException
import okio.IOException

class RealCacheKeyBuilder : CacheKeyBuilder {

Expand All @@ -22,7 +22,7 @@ class RealCacheKeyBuilder : CacheKeyBuilder {
jsonWriter.serializeNulls = true
Utils.writeToJson(resolvedArguments, jsonWriter)
jsonWriter.close()
String.format("%s(%s)", field.fieldName, buffer.readUtf8())
"${field.fieldName}(${buffer.readUtf8()})"
} catch (e: IOException) {
throw RuntimeException(e)
}
Expand All @@ -41,7 +41,9 @@ class RealCacheKeyBuilder : CacheKeyBuilder {
} else {
value
}
}.toSortedMap()
}.toList()
.sortedBy { it.first }
.toMap()
}

@Suppress("UNCHECKED_CAST")
Expand All @@ -52,7 +54,7 @@ class RealCacheKeyBuilder : CacheKeyBuilder {
null -> null
is Map<*, *> -> resolveArguments(resolvedVariable as Map<String, Any?>, variables)
is InputType -> {
val inputFieldMapWriter = SortedInputFieldMapWriter(Comparator { o1, o2 -> o1.compareTo(o2) })
val inputFieldMapWriter = SortedInputFieldMapWriter()
resolvedVariable.marshaller().marshal(inputFieldMapWriter)
resolveArguments(inputFieldMapWriter.map(), variables)
}
Expand Down
Loading