我使用阿波罗客户端获取图书图表,并使用中继样式分页。下面的NEW_BOOKS查询和ALL_BOOKS查询都可以独立工作。
目前,我在主页中使用NEW_BOOKS,在主页中使用弹出的ALL_BOOKS。当主页打开时,NEW_BOOKS会很好地加载。打开弹出并获取ALL_BOOKS时,newBooks变成undefined或ALL_BOOKS查询的结果。
为什么会发生这种情况?
const { loading, data: newBooks, fetchMore, networkStatus } = useQuery(NEW_BOOKS, {
variables: {
first: PAGE_SIZE,
after: endCursor
},
notifyOnNetworkStatusChange: true
});
const NEW_BOOKS = gql`query GetNewBooks($first:Int!, $after:String){
books(
first: $first, after: $after,
filters: [
{
path: "isNew",
value: true
}
]
) {
totalCount
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
edges {
node {
id
name
author {
id
name
}
}
}
}
}`;-All图书查询可按名称进行过滤
const { loading, data: filteredBooks, fetchMore, networkStatus } = useQuery(ALL_BOOKS, {
variables: {
first: PAGE_SIZE,
after: endCursor,
name: nameFilter
},
notifyOnNetworkStatusChange: true
});
const ALL_BOOKS = gql`query GetAllBooks($first:Int!, $after:String, $name:String){
books(
first: $first, after: $after,
filters: [
{
path: "name",
value: $name,
type: "contains"
}
]
) {
totalCount
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
edges {
node {
id
name
copiesSold
author {
id
name
}
}
}
}
}`;正在使用的缓存如下,
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
books: relayStylePagination(),
},
}
},
});发布于 2021-12-28 08:44:48
当使用keyArgs或类似的分页时,我们必须明确地传递relayStylePagination。
keyArgs: ["type"]字段策略配置意味着类型是缓存在访问该字段的值时应该考虑的惟一参数(除了字段名和封闭对象的标识)。keyArgs: false配置禁用了通过参数来区分字段值的整个系统,因此该字段的值将仅由字段的名称(在某些StoreObject中)标识,而不追加任何序列化参数。
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
books: relayStylePagination(["name"]),
},
}
},
});https://stackoverflow.com/questions/70213613
复制相似问题