TypePolicy on a request level
See original GitHub issueWith the deprecation of updateQuery
on a fetchMore
, there doesn’t seem to be a way to specify its replacement, a TypePolicy, in the same place. I understand that you can specify it on specific entities as seen in the docs and below:
const cache = new InMemoryCache({
typePolicies: {
Person: {
fields: {
...
But in my use-case, I have a helper method used by various entities with the list query passed in:
import {useQuery} from "@apollo/client";
import {useEffect, useState} from "react";
import {nameFromQuery} from "lib/graphql/helpers";
export default function useFetchAll(query) {
const [error, setError] = useState(null);
const [loading, setLoading] = useState(true);
const {data, fetchMore} = useQuery(query, {
onError: setError,
variables: {first: 100}
});
useEffect(() => {
if(!data) { return; }
const name = nameFromQuery(query);
const {endCursor, hasNextPage} = data[name].pageInfo;
if(hasNextPage) {
fetchMore({
updateQuery: (oldData, {fetchMoreResult: newData}) => ({
...newData,
[name]: {
...newData[name],
edges: [
...oldData[name].edges,
...newData[name].edges
]
}
}),
variables: {after: endCursor}
});
} else {
setLoading(false);
}
}, [data]);
return {data, error, loading};
}
I’m not sure how I can convert this method to using type policies instead. There are a couple of ways to avoid this issue:
- Type Policies for everything that uses this - but it would make the method unreliable
- Skipping the cache - but it’d then it’d making a lot of extra calls if used multiple times
It’d make more sense to me if useQuery
or fetchMore
accepted a typePolicy
, similar to how it accepts a fetchPolicy
. That may be a naive statement, but there just seems like there should be an equivalent way of handling the issue if updateQuery
is going away.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:9
- Comments:14 (4 by maintainers)
Top Results From Across the Web
Customizing the behavior of cached fields - Apollo GraphQL
You provide field policies to the constructor of InMemoryCache . Each field policy is defined inside whichever TypePolicy object corresponds to the field's ......
Read more >Type Policies in Apollo Client 3: An Introduction - YouTube
What are type policies in Apollo Client 3 and how can we leverage them in our applications? This video explores some of the...
Read more >Apollo Cache in a Nutshell - E.Y. - Medium
Inside the TypePolicies, you can supply field level policies for each ... Otherwise, the query with the same request data but different ...
Read more >Advanced Topics on Caching – Angular
Please note that if you call refetchQueries with an array of strings, ... const cache = new InMemoryCache({ typePolicies: { Query: { fields: ......
Read more >Customizing the behavior of cached fields - Client (React)
Each field policy is defined inside whatever TypePolicy object corresponds ... so that Apollo Client doesn't include it in requests to your GraphQL...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have a similar issue on migrating from v2 to v3 on cache updating. I used the same query while with different view types in my chat application.
Here is the sample query
The main part displays all messages in an infinite-scrolling method, where the following policy works great.
While the other part displays logs of messages in a pages, where only
$limit
number of records are shown at a time. I have to use the following policy to make this part work.As updateQuery is deprecated, is there a workaround for making both work now in v3?
@michel Yeah I solved my issue by: