I am trying to make some reusable Apollo hooks and simplify the APIs with some encapsulation. So my idea is to have the useQuery
hook inside of a custom hook. In my case I need actually the useLazyQuery
hook:
export const useGetSomething = () => {
const [something, setSomething] = useState<Something>();
const [getSomething, { data, refetch }] =
useLazyQuery<GetSomethingHook>(GET_SOMETHING_QUERY, {
fetchPolicy: 'no-cache',
});
useEffect(() => {
if (data?.something) {
setSomething(data.something);
}
}, [setSomething, data]);
const loadSomething = useCallback(
(id: string, date: Date) => {
getSomething({
variables: {
args: {
id,
date,
},
},
});
},
[getSomething],
);
const refetchSomething = useCallback(
(id: string, date: Date) => {
refetch({
variables: {
args: {
id,
date,
},
},
}).then((result) => setSomething(result.data.something))
},
[setSomething, refetch],
);
return useMemo(() => {
return {
loadSomething,
refetchSomething,
something,
};
}, [something, loadSomething, refetchSomething]);
};
So there is a range of issues:
- if I use
refetch
with the sameid
and a newdate
, theuseLazyQuery
calls for thesomething
with the old date that was the initial one (why? no idea...) - if I replace
refetch
withfetchMore
inrefetchSomething
, the call is actually done properly AT FIRST, but then there is a weird another call without ANY actual source in that hook which calls forsomething
with olddate
- if I use
getSomething
in bothloadSomething
andrefetchSomething
it works likefetchMore
where even tho the function was called only once, theNetwork
tab shows two calls - one with old and second with new data.
It just looks like Apollo
is just broken.