I'm missing the cache with queries where arguments...
# apollo-kotlin
s
I'm missing the cache with queries where arguments are provided in nested fields, not in the top level query. 🧵
Object 'UserCardsPayload:{field=CreatedAt, order=Desc}' not found
Copy code
type Query {
    user: User
}

type User {
    cards(afterId: UUID, language: String, limit: Int, search: String, sortBy: CardSortingInput): UserCardsPayload!
    decks(afterId: UUID, limit: Int, sortBy: DeckSortingInput, tags: [String!], title: String): UserDecksPayload!
}

type UserCardsPayload {
    cards: [Card!]
    errors: [AccessError!]!
    hasNext: Boolean!
}

type UserDecksPayload {
    decks: [Deck!]
    errors: [AccessError!]!
    hasNext: Boolean!
}
Copy code
query Collection {
    user {
        decks {
            decks {
                ...DeckItem
            }
            hasNext
        }
        cards(sortBy: {field: CreatedAt, order: Desc}) {
            cards {
                ...CardItem
            }
            hasNext
        }
    }
}
Copy code
extend type User @fieldPolicy(forField: "decks", keyArgs: "afterId sortBy tags title")
extend type User @fieldPolicy(forField: "cards", keyArgs: "afterId sortBy search language")

extend type Deck @typePolicy(keyFields: "id")
extend type Card @typePolicy(keyFields: "id")
extend type User @typePolicy(keyFields: "id")
so,
user.decks
works and would get retrieved from the cache but
user.cards(sortBy: {...})
cannot be found with the log on top
image.png
I'm also a bit confused by
Object 'UserCardsPayload:{field=CreatedAt, order=Desc}' not found
since it's supposed to be a fieldPolicy to
user.cards
, is it trying to find the payload class as if there was a typePolicy set or is this just what the logs look like?
b
You need a typePolicy on
UserCardsPayload
that matches the fieldPolicy for user.cards (of type
UserCardsPayload
) - otherwise the generated cache ids will be different when writing to vs when reading from the cache, which results in the cache miss. But in your case, you can't really do that, since the arguments have no equivalents in the return's type fields. You should probably remove these fieldPolicies and the default cache id (uses the path) will be used.
s
yeah that's what mainly confused me. Omitting the policies seems to indeed work, thanks
👍 1
btw what's the use behind
paginationArgs
for fieldPolicies? it says they're omitted for computing the cache key?
b
yes exactly - This is an experimental feature. The implementation used to live on the main repo but now its development happens on the incubating cache repo. More info here - if you want to experiment with it, feedback is welcome!
s
Looks great! And seems to also work in my case with
Copy code
extend type User @fieldPolicy(forField: "cards", paginationArgs: "afterId")
b
Cool 🙂 Warning though, if you only do this, each new page will override the previous one in the cache, which is not really what you want. You'll probably need to implement the whole thing (either manually with
ApolloStore
or with the incubating lib)
s
yes yes, I just wanted to test if I can even add a fieldpolicy due to the above issue
👍 1
however, is it supposed to just always do a network call when including paging arguments and cache first without? currently it seems to always retrieve from cache, no matter the afterId arg
b
yeah that's expected, afterId is ignored, so the list is found in the cache
s
so I'm supposed to set a NetworkFirst fetch policy for paging queries?
b
If you want the app to refresh the list, yes you can do that
👍🏻 1