Reputation: 473
I've got a question about cacheRedirects
in Apollo, I've got an overview of items and a detailview. I'd like the detailview cache to be redirected to an item from the overview (like the book example in the docs).
The only thing is that the thing I'd like to redirect to can only be queried from inside a bigger parent query.
So expanding the books example it'd be like:
# for all books
library (id: ID) {
books (params) {
id
title
}
}
# for a single book
library (id: ID) {
book (id: ID) {
id
title
}
}
What would I redirect to if I'd like to fetch a single book?
I don't think I could do this, because I'm actually fetching a library in order to fetch a book:
cacheRedirects: {
Query: {
book: () => {
// return book id from cache
}
}
}
Is there a way to redirect a query for a book (through a library
) to the data for a book? Or am I thinking about this in the wrong way?
Upvotes: 5
Views: 1223
Reputation: 473
I didn't know you could also resolve types directly so in this case:
cacheRedirects: {
Library: {
book: (_, args) => toIdValue(dataIdFromObject({ id: args.id, __typename: 'Book' }))
}
}
Should work to redirect to a cached book from a library.
Upvotes: 3