Skip to content

Fix normalization of variable default values #23

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

Merged
merged 2 commits into from
Jul 22, 2024
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
3 changes: 2 additions & 1 deletion .github/workflows/build-pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ jobs:
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 #v4.1.7
- run: |
./gradlew build
./gradlew build
./gradlew -p tests build
5 changes: 1 addition & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ androidx-sqlite = "2.3.1"
[libraries]
librarian = "com.gradleup.librarian:core:0.0.3"
kgp = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin-plugin" }
android-plugin = { group = "com.android.tools.build", name = "gradle", version.ref = "android-plugin" }
apollo-api = { group = "com.apollographql.apollo", name = "apollo-api", version.ref = "apollo" }
apollo-gradle-plugin = { group = "com.apollographql.apollo", name = "apollo-gradle-plugin", version.ref = "apollo" }
apollo-mpp-utils = { group = "com.apollographql.apollo", name = "apollo-mpp-utils", version.ref = "apollo" }
apollo-testing-support = { group = "com.apollographql.apollo", name = "apollo-testing-support", version.ref = "apollo" }
apollo-runtime = { group = "com.apollographql.apollo", name = "apollo-runtime", version.ref = "apollo" }
apollo-mockserver = "com.apollographql.mockserver:apollo-mockserver:0.0.1"
atomicfu-library = { group = "org.jetbrains.kotlinx", name = "atomicfu", version.ref = "atomicfu" }
atomicfu-plugin = { group = "org.jetbrains.kotlinx", name = "atomicfu-gradle-plugin", version.ref = "atomicfu" }
coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1"
kotlin-test = { group = "org.jetbrains.kotlin", name = "kotlin-test" } # the Kotlin plugin resolves the version
kotlin-test-junit = { group = "org.jetbrains.kotlin", name = "kotlin-test-junit" } # the Kotlin plugin resolves the version
Expand All @@ -29,7 +26,6 @@ uuid = "com.benasher44:uuid:0.8.2"
sqldelight-android = { group = "app.cash.sqldelight", name = "android-driver", version.ref = "sqldelight" }
sqldelight-jvm = { group = "app.cash.sqldelight", name = "sqlite-driver", version.ref = "sqldelight" }
sqldelight-native = { group = "app.cash.sqldelight", name = "native-driver", version.ref = "sqldelight" }
sqldelight-plugin = { group = "app.cash.sqldelight", name = "gradle-plugin", version.ref = "sqldelight" }
sqldelight-runtime = { group = "app.cash.sqldelight", name = "runtime", version.ref = "sqldelight" }
sqldelight-sqlite = { group = "app.cash.sqldelight", name = "sqlite-dialect", version.ref = "sqldelight" }
sqlite-jdbc = "org.xerial:sqlite-jdbc:3.43.2.0"
Expand All @@ -45,3 +41,4 @@ android = { id = "com.android.library", version.ref = "android-plugin" }
librarian = { id = "com.gradleup.librarian", version = "0.0.4" }
atomicfu = { id = "org.jetbrains.kotlin.plugin.atomicfu", version.ref = "kotlin-plugin" }
sqldelight = { id = "app.cash.sqldelight", version.ref = "sqldelight" }
apollo = { id = "com.apollographql.apollo", version.ref = "apollo" }
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.apollographql.cache.normalized.api

import com.apollographql.apollo.annotations.ApolloInternal
import com.apollographql.apollo.api.Adapter
import com.apollographql.apollo.api.CustomScalarAdapters
import com.apollographql.apollo.api.Executable
import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.api.*
import com.apollographql.apollo.api.json.MapJsonReader
import com.apollographql.apollo.api.json.MapJsonWriter
import com.apollographql.apollo.api.variables
import com.apollographql.cache.normalized.api.internal.CacheBatchReader
import com.apollographql.cache.normalized.api.internal.Normalizer
import kotlin.jvm.JvmOverloads
Expand All @@ -33,7 +29,7 @@ fun <D : Executable.Data> Executable<D>.normalize(
): Map<String, Record> {
val writer = MapJsonWriter()
adapter().toJson(writer, customScalarAdapters, data)
val variables = variables(customScalarAdapters)
val variables = variables(customScalarAdapters, withDefaultValues = true)
@Suppress("UNCHECKED_CAST")
return Normalizer(variables, rootKey, cacheKeyGenerator, metadataGenerator, fieldKeyGenerator, embeddedFieldsProvider)
.normalize(writer.root() as Map<String, Any?>, rootField().selections, rootField().type.rawType())
Expand Down
8 changes: 5 additions & 3 deletions tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ buildscript {
mavenCentral()
google()
}
dependencies {
classpath("build-logic:build-logic")
}
}

plugins {
alias(libs.plugins.kotlin).apply(false)
alias(libs.plugins.apollo).apply(false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ query GetUser {
name
email
}
}
}

query RepositoryListQuery($first: Int = 15, $after: String) {
repositories(first: $first, after: $after) {
id
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
type Query {
user: User
repositories(first: Int, after: String): [Repository!]!
}

type User {
name: String!
email: String!
admin: Boolean
}
}

type Repository {
id: ID!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package test

import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.testing.QueueTestNetworkTransport
import com.apollographql.apollo.testing.enqueueTestResponse
import com.apollographql.apollo.testing.internal.runTest
import com.apollographql.cache.normalized.FetchPolicy
import com.apollographql.cache.normalized.api.MemoryCacheFactory
import com.apollographql.cache.normalized.fetchPolicy
import com.apollographql.cache.normalized.normalizedCache
import sqlite.RepositoryListQuery
import kotlin.test.Test
import kotlin.test.assertEquals

class NormalizationTest {
@Test
fun variableDefaultValuesTest() = runTest {
val apolloClient = ApolloClient.Builder()
.networkTransport(QueueTestNetworkTransport())
.normalizedCache(MemoryCacheFactory())
.build()
val query = RepositoryListQuery()
apolloClient.enqueueTestResponse(
query,
RepositoryListQuery.Data(
listOf(RepositoryListQuery.Repository("42"))
)
)
apolloClient.query(query).execute()
val response = apolloClient
.query(query)
.fetchPolicy(FetchPolicy.CacheOnly)
.execute()
assertEquals("42", response.data!!.repositories.first().id)
}
}