Skip to content

Don't call MetadataGenerator on error fields #107

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 1 commit into from
Mar 14, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ internal class Normalizer(
path = base.append(fieldKey),
embeddedFields = embeddedFieldsProvider.getEmbeddedFields(EmbeddedFieldsContext(parentType)),
)
val metadata = metadataGenerator.metadataForObject(entry.value, MetadataGeneratorContext(field = mergedField, variables))
val metadata = if (entry.value is Error) {
emptyMap()
} else {
metadataGenerator.metadataForObject(entry.value, MetadataGeneratorContext(field = mergedField, variables))
}
fieldKey to FieldInfo(value, metadata)
}.toMap()

Expand Down Expand Up @@ -164,7 +168,7 @@ internal class Normalizer(
*
* This function builds the list of records as a side effect
*
* @param value a json value from the response. Can be any type supported by [com.apollographql.apollo.api.json.JsonWriter]
* @param value a json value from the response. Can be [com.apollographql.apollo.api.json.ApolloJsonElement] or [Error]
* @param field the field currently being normalized
* @param type_ the type currently being normalized. It can be different from `field.type` for lists.
* @param embeddedFields the embedded fields of the parent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type Query {
users(first: Int = 10, after: String = null, last: Int = null, before: String = null): UserConnection!
users(first: Int = 10, after: String = null, last: Int = null, before: String = null): UserConnection
}

type UserConnection {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pagination

import com.apollographql.apollo.api.Error
import com.apollographql.apollo.api.Optional
import com.apollographql.apollo.testing.internal.runTest
import com.apollographql.cache.normalized.ApolloStore
Expand Down Expand Up @@ -344,5 +345,42 @@ class ConnectionPaginationTest {
assertEquals(data5, dataFromStore)
assertChainedCachesAreEqual(apolloStore)
}
}


@Test
fun errorMemoryCache() {
errorTest(MemoryCacheFactory())
}

@Test
fun errorSqlCache() {
errorTest(SqlNormalizedCacheFactory())
}

@Test
fun errorChainedCache() {
errorTest(MemoryCacheFactory().chain(SqlNormalizedCacheFactory()))
}

private fun errorTest(cacheFactory: NormalizedCacheFactory) = runTest {
val apolloStore = ApolloStore(
normalizedCacheFactory = cacheFactory,
cacheKeyGenerator = TypePolicyCacheKeyGenerator,
metadataGenerator = ConnectionMetadataGenerator(Pagination.connectionTypes),
cacheResolver = FieldPolicyCacheResolver,
recordMerger = ConnectionRecordMerger
)
apolloStore.clearAll()
val query = UsersQuery(first = Optional.Present(2))
apolloStore.writeOperation(
operation = query,
data = UsersQuery.Data { users = null },
errors = listOf(Error.Builder("An error occurred.").path(listOf("users")).build())
)
val responseFromStore = apolloStore.readOperation(query)
assertEquals(UsersQuery.Data { users = null }, responseFromStore.data)
assertEquals(1, responseFromStore.errors?.size)
assertEquals("An error occurred.", responseFromStore.errors?.firstOrNull()?.message)
assertEquals(listOf("users"), responseFromStore.errors?.firstOrNull()?.path)
}
}