jotaijs / jotai-urql

Jotai integration library for URQL
MIT License
29 stars 5 forks source link

double queries and refetch does not work #14

Closed barthuijgen closed 1 year ago

barthuijgen commented 1 year ago

I have suspense atoms set to false using the useHydrateAtoms hook

and a simple query like

import { graphql } from "../gql";
import { atomWithQuery } from "jotai-urql";

const accountsGql = graphql(`
  query getAccounts {
    accounts {
      id
      username
    }
  }
`);

export const accountsAtom = atomWithQuery({
  query: accountsGql,
});

Simply using it like

const [accounts, refetchAccounts] = useAtom(accountsAtom);

Returns in two http requests in the network tab. If set suspenseAtom to true, it only uses one http request.

Aditionally testing refetches resulted in no new http requests, regardless if suspenseAtom was true or false.

  const [accounts, refetchAccounts] = useAtom(accountsAtom);

  useEffect(() => {
    const int = setInterval(() => {
      console.log("refetchAccounts");
      refetchAccounts();
    }, 2000);
    return () => clearInterval(int);
  }, []);
RIP21 commented 1 year ago

Regarding 2 requests - it may be fixed with dedupe exchange of urql, if they're instant, then it may actually able to do twice, but if it take longer (the request), I think it should be deduped on urql client side. TBH, non suspended version was rather an afterthought and hacky. Either way, as a workaround you can use loadable and not rehydrate suspense atom to be false.

Regarding refetch is not refetching. It's not refetch, it's reexecute of the operation. And, same as in original urql react bindings, context matters, namely a networkPolicy. It should be cache-and-network (recommended) or network-only so reexecute will trigger a refetch, if it's cache-only, or cache-first nothing will happen as it will simply read the data from the cache and that is it.

RIP21 commented 1 year ago

Closing this as not much actionable. Feel free to reopen.

Highly recommend using loadable util instead of disabling suspense. Haven't tested it, but it should work fine.