apollo-cache-inmemory#defaultDataIdFromObject JavaScript Examples
The following examples show how to use
apollo-cache-inmemory#defaultDataIdFromObject.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AppCache.js From trashpanda-fe with MIT License | 5 votes |
AppCache = props => {
const [client, setClient] = useState(undefined);
useEffect(() => {
const link = new HttpLink({ uri: "https://trashpanda-be.herokuapp.com" });
const cache = new InMemoryCache({
dataIdFromObject: object => {
switch (object.__typename) {
case "Material":
return `Material: ${object.material_id}`;
case "Family":
return `Family: ${object.family_id}`;
case "GPS":
return `GPS`;
case "PostalCode":
return `PostalCode: ${object.postal_code}`;
case "Location":
return `Location: ${object.address}`;
case "Permission":
return `Permission`;
default:
return defaultDataIdFromObject(object);
}
}
});
// TODO: Monkey-patching in a fix for an open issue suggesting that
// `readQuery` should return null or undefined if the query is not yet in the
// cache: https://github.com/apollographql/apollo-feature-requests/issues/1
cache.originalReadQuery = cache.readQuery;
cache.readQuery = (...args) => {
try {
return cache.originalReadQuery(...args);
} catch (err) {
return undefined;
}
};
const client = new ApolloClient({
link,
cache,
typeDefs,
resolvers
});
// set client immediately (replaces cache persist)
setClient(client);
return () => {};
}, []);
if (client === undefined) return <Spinner />;
return <ApolloProvider client={client}>{props.children}</ApolloProvider>;
}