aerogear / offix

GraphQL Offline Client and Server
https://offix.dev
Apache License 2.0
758 stars 45 forks source link

Used persisted cache on first-load #361

Closed amaury1093 closed 4 years ago

amaury1093 commented 4 years ago

Feature Request

Let's imagine a simple scenario, where I can of/off a toggle (where isOn is a boolean).

const MY_QUERY = gql`query getToggle() {
  getToggle() {
    _id
    isOn
  }
 }`;

const MY_MUT = gql`mutation setToggle($input: Bool!) {
  setToggle(input: $input) {
    _id
    isOn
  }
}`;

// In a React component
const { data } = useQuery(MY_QUERY);
const [setToggle] = useMutation(MY_MUT);

<Button onClick={() => setToggle(!data?.getToggle.isOn}}>Toggle</Button>
<p>isOn? {data?.getToggle.isOn}</p>

Repro:

Expected:

Actual:

Is your feature request related to a problem? Please describe.

I'd like to remove the small delay of fetching query at startup.

Describe the solution you'd like

Possibly persist all apollo client cache inside AsyncStorage?

Describe alternatives you've considered**

I'm using https://github.com/apollographql/apollo-cache-persist right now. It works, but I was wondering if there's a plan to directly integrate that inside offix?

wtrocki commented 4 years ago

Your code needs to await for the cache to restore data: https://github.com/aerogear/offix/blob/master/examples/react/src/index.js#L16

There is possibility to use synchronous api thanks to Apollo-Cache-Persist but we do not recommend or document this use case.