S.
12/10/2023, 10:57 PMObject 'DeckOrError:*UUID*' not found
.
I have a typePolicy on Deck
with it's id and I can see that those are being stored in the cache, however it's fields are never getting retrieved from the cache.
I also set up a fieldPolicy extend type Deck @fieldPolicy(forField: "cards", keyArgs: "afterId limit sortBy")
but same story - it's being cached but never retrieved and resulting in the above not-found message.
It only works for the top-level getUser -> userDecks query, which says Object 'QUERY_ROOT' has no field named 'getUser'
only the first time, subsequent calls are fetched from the cache.
What can be done, or what am I missing to make the cache work properly? it's not even possible to apply extensions on the union type itselfmbonnin
12/11/2023, 10:08 AMquery GetDeck {
deck(id: "42") {
id
}
}
?
For these kind of things, you'll need to make a field policy for deck:
extend type Query @fieldPolicy(forField: "deck", keyArgs: "id")
mbonnin
12/11/2023, 10:08 AMmbonnin
12/11/2023, 10:09 AMS.
12/11/2023, 8:04 PMmbonnin
12/11/2023, 8:20 PMschema.graphqls
and extra.graphqls
?S.
12/11/2023, 8:29 PMunion UserOrError = User | Error
# same for the others
type Card {
id: UUID!
}
type CardList {
hasNext: Boolean!
list: [Card!]!
}
type Deck {
cards(afterId: UUID, limit: Int, sortBy: CardSortingInput): CardListOrError!
id: UUID!
}
type DeckList {
hasNext: Boolean!
list: [Deck!]!
}
type Query {
getDeck(id: UUID!): DeckOrError!
getUser: UserOrError!
}
type User {
id: UUID!
userDecks(): DeckListOrError!
}
extend type User @typePolicy(keyFields: "id")
extend type Card @typePolicy(keyFields: "id")
extend type Deck @typePolicy(keyFields: "id")
extend type Query @fieldPolicy(forField: "getCard", keyArgs: "id")
extend type Query @fieldPolicy(forField: "getDeck", keyArgs: "id")
extend type Deck @fieldPolicy(forField: "cards", keyArgs: "afterId limit sortBy")
S.
12/11/2023, 8:32 PMmbonnin
12/11/2023, 8:33 PMS.
12/11/2023, 8:35 PMObject 'DeckOrError:b090cd01-1107-4b19-8d03-de513591e4fb' not found
Object 'DeckOrError:1269fecc-adf1-4e12-84bb-466d3d3dfa97' not found
every time I try to query it againS.
12/11/2023, 8:37 PMquery UserDecks() {
getUser {
... on User {
userDecks() {
... on DeckList {
list {
id
}
hasNext
}
... on NotFound {
msg
}
}
}
... on Error {
msg
}
}
}
query Deck($id: UUID!) {
getDeck(id: $id) {
... on Error {
msg
}
... on Deck {
id
cards {
...CardItem
}
}
}
}
query DeckCards($id: UUID!, $afterId: UUID, $limit: Int, $sortBy: CardSortingInput) {
getDeck(id: $id) {
... on Error {
msg
}
... on Deck {
id
cards(afterId: $afterId, limit: $limit, sortBy: $sortBy) {
...CardItem
}
}
}
}
mbonnin
12/11/2023, 8:42 PMI'm guessingCopy codeObject 'DeckOrError:b090cd01-1107-4b19-8d03-de513591e4fb' not found
DeckOrError
is an union as well, right? If that's the case, looks like this is the issue, the default FieldPolicyCacheResolver doesn't work with abstract types (interfaces and unions)S.
12/11/2023, 8:42 PMmbonnin
12/11/2023, 8:43 PMmbonnin
12/11/2023, 8:44 PMS.
12/11/2023, 8:46 PMS.
12/11/2023, 8:50 PMmbonnin
12/11/2023, 8:51 PMmbonnin
12/11/2023, 8:53 PM__typename
from the cache key:
object FieldPolicyCacheResolver : CacheResolver {
override fun resolveField(
field: CompiledField,
variables: Executable.Variables,
parent: Map<String, @JvmSuppressWildcards Any?>,
parentId: String,
): Any? {
val keyArgsValues = field.argumentValues(variables) {it.isKey }.values.map { it.toString() }
if (keyArgsValues.isNotEmpty()) {
// Remove the typename here
return CacheKey(keyArgsValues)
}
return DefaultCacheResolver.resolveField(field, variables, parent, parentId)
}
}
mbonnin
12/11/2023, 8:54 PMTypePolicyCacheKeyGenerator
:
object TypePolicyCacheKeyGenerator : CacheKeyGenerator {
override fun cacheKeyForObject(obj: Map<String, Any?>, context: CacheKeyGeneratorContext): CacheKey? {
val keyFields = context.field.type.rawType().keyFields()
return if (keyFields.isNotEmpty()) {
CacheKey(keyFields.map { obj[it].toString() }.joinToString(""))
} else {
null
}
}
}
S.
12/11/2023, 8:55 PMmbonnin
12/11/2023, 8:55 PMmbonnin
12/11/2023, 8:55 PMextend type Deck @fieldPolicy(forField: "cards", keyArgs: "afterId limit sortBy")
mbonnin
12/11/2023, 8:56 PMS.
12/11/2023, 8:59 PMS.
12/12/2023, 2:03 PMmbonnin
12/12/2023, 2:05 PMadd the newly fetched values?In the cache yes, in your responses, "it depends the query"
mbonnin
12/12/2023, 2:06 PMDeck
has an id of courseS.
12/12/2023, 2:15 PMS.
02/20/2024, 6:32 PMQUERY_ROOT.getUser
if I'm fetching new fields but not for any other objects
CACHE MISS: Object '6b0bc4f1-4856-4eee-ac91-05f06721f0f4' has no field named 'public'
CACHE MISS: Object '6b0bc4f1-4856-4eee-ac91-05f06721f0f4' has no field named 'public'
CACHE MISS: Object '6b0bc4f1-4856-4eee-ac91-05f06721f0f4' has no field named 'public'
while CACHE MISS: Object 'getUser' has no field named 'userDecks()'
does only appear once and is then retrieved from the cache successfullyS.
02/20/2024, 6:36 PMS.
02/20/2024, 6:45 PMextend type Query @fieldPolicy(forField: "getDeck", keyArgs: "id")
however, not sure whyS.
02/21/2024, 10:13 AMquery Deck($id: UUID!, $limit: Int) {
getDeck(id: $id) {
... on Error {
msg
}
... on Deck {
id
title
public
cards(limit: $limit) {
...CardItem
}
mutation updateDeck($deckId: UUID!, $title: String, $public: Boolean) {
updateDeck(id: $deckId, title: $title, public: $public) {
... on Error {
msg
}
... on Deck {
id
title
public
}
}
}
mbonnin
02/21/2024, 10:16 AMS.
02/21/2024, 10:23 AMS.
02/21/2024, 10:24 AMS.
02/21/2024, 10:25 AMgetUser.userDecks
mbonnin
02/21/2024, 10:29 AMS.
02/21/2024, 10:30 AMmbonnin
02/21/2024, 10:31 AM"getDeck(id: ...)"
keymbonnin
02/21/2024, 10:32 AMmbonnin
02/21/2024, 10:33 AMcacheKeyForObject()
returned null
mbonnin
02/21/2024, 10:34 AMMutation.getDeck
is an union, right?S.
02/21/2024, 10:34 AMTypePolicyCacheKeyGenerator
? I have it like above in the threadmbonnin
02/21/2024, 10:35 AMS.
02/21/2024, 10:35 AMgetDeck(id: UUID!): DeckOrFetchingError!
union DeckOrFetchingError = Deck | NotFound | Unauthorized
those are all union types, yesS.
02/21/2024, 10:36 AMupdateDeck(id: UUID!, public: Boolean, title: String): DeckOrFetchingError!
mbonnin
02/21/2024, 10:36 AMmbonnin
02/21/2024, 10:37 AMextend type DeckOrFetchingError @typePolicy(keyFields: "id")
Which is weird because unions do not have fields...mbonnin
02/21/2024, 10:38 AMCacheKeyGenerator
to handle that case. I need to finish something but will send sample code a bit latermbonnin
02/21/2024, 10:38 AMS.
02/21/2024, 10:39 AMmbonnin
02/21/2024, 10:39 AMS.
02/21/2024, 10:40 AMmbonnin
02/21/2024, 10:42 AMmbonnin
02/21/2024, 10:43 AMmbonnin
02/21/2024, 10:44 AMobject MyCacheKeyGenerator: CacheKeyGenerator {
override fun cacheKeyForObject(obj: Map<String, Any?>, context: CacheKeyGeneratorContext): CacheKey? {
if (obj["__typename"] == "Deck") {
return CacheKey(obj["__typename"].toString(), obj["id"].toString())
}
// Other types here
return null
}
}
S.
02/21/2024, 10:50 AMmbonnin
02/21/2024, 10:51 AMmbonnin
02/21/2024, 10:51 AMmbonnin
02/21/2024, 10:52 AMmbonnin
02/21/2024, 10:54 AMmbonnin
02/21/2024, 10:55 AMS.
02/21/2024, 12:30 PMmbonnin
02/21/2024, 12:42 PMS.
02/21/2024, 12:43 PMDeck:UUID
mbonnin
02/21/2024, 12:43 PMmbonnin
02/21/2024, 12:43 PMS.
02/21/2024, 12:44 PMmbonnin
02/21/2024, 12:44 PMmbonnin
02/21/2024, 12:45 PMS.
02/21/2024, 12:49 PMmbonnin
02/21/2024, 12:50 PM