Apollo 中的嵌套查询缓存重定向
Nested query cache redirects in Apollo
我有一个关于 Apollo 中 cacheRedirects
的问题,我有一个项目概览和一个详细视图。我希望将详细视图缓存重定向到概述中的项目(如文档中的 the book example)。
唯一的问题是,我想重定向到的内容只能从更大的父查询中查询。
所以扩展书籍示例就像:
# for all books
library (id: ID) {
books (params) {
id
title
}
}
# for a single book
library (id: ID) {
book (id: ID) {
id
title
}
}
如果我想取一本书,我会重定向到什么?
我认为我做不到,因为我实际上是为了取书而去图书馆:
cacheRedirects: {
Query: {
book: () => {
// return book id from cache
}
}
}
有没有办法将图书查询(通过 library
)重定向到图书数据?还是我的想法不对?
我不知道您还可以直接解析类型,所以在这种情况下:
cacheRedirects: {
Library: {
book: (_, args) => toIdValue(dataIdFromObject({ id: args.id, __typename: 'Book' }))
}
}
应该可以从图书馆重定向到缓存的图书。
我有一个关于 Apollo 中 cacheRedirects
的问题,我有一个项目概览和一个详细视图。我希望将详细视图缓存重定向到概述中的项目(如文档中的 the book example)。
唯一的问题是,我想重定向到的内容只能从更大的父查询中查询。
所以扩展书籍示例就像:
# for all books
library (id: ID) {
books (params) {
id
title
}
}
# for a single book
library (id: ID) {
book (id: ID) {
id
title
}
}
如果我想取一本书,我会重定向到什么?
我认为我做不到,因为我实际上是为了取书而去图书馆:
cacheRedirects: {
Query: {
book: () => {
// return book id from cache
}
}
}
有没有办法将图书查询(通过 library
)重定向到图书数据?还是我的想法不对?
我不知道您还可以直接解析类型,所以在这种情况下:
cacheRedirects: {
Library: {
book: (_, args) => toIdValue(dataIdFromObject({ id: args.id, __typename: 'Book' }))
}
}
应该可以从图书馆重定向到缓存的图书。