Skip to content

Instantly share code, notes, and snippets.

@tvler
Created February 8, 2021 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvler/0a158e651dc88ec43094584b72789b70 to your computer and use it in GitHub Desktop.
Save tvler/0a158e651dc88ec43094584b72789b70 to your computer and use it in GitHub Desktop.
WriteFragment: A declarative way to write fragments to Apollo cache using React components
import { useApolloClient, Cache } from "@apollo/client";
import { useEffect } from "react";
/*
* WriteFragment: A declarative way to write fragments to Apollo cache
* using React components. When the fragment props change, the cache is
* updated. When then component unmounts, the fragment is deleted.
*
* This is essentially a reactive version of the cache.writeFragment
* method, using the same exact arguments and types.
*
* writeFragment API: apollographql.com/docs/react/api/cache/InMemoryCache/#writefragment
*
* Ex:
* const fragment = gql`fragment UserFragment on User { id name }`;
* const data = {
* __typename: "User",
* id: 123,
* name: "Tyler Deitz",
* };
*
* // Then, somewhere in your JSX
*
* <WriteFragment fragment={fragment} data={data} />
*/
export const WriteFragment: React.FC<Cache.WriteFragmentOptions<any, any>> = ({
fragment,
data,
broadcast,
id,
fragmentName,
variables,
}) => {
const { cache } = useApolloClient();
useEffect(() => {
cache.writeFragment({
fragment,
data,
broadcast,
id,
fragmentName,
variables,
});
return () => {
const normalizedIdentity = cache.identify(data);
if (normalizedIdentity !== undefined) {
cache.evict({ id: normalizedIdentity });
cache.gc();
}
};
}, [broadcast, cache, data, fragment, fragmentName, id, variables]);
return null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment