Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use relay cache data on react-native app while fresh data is being fetched

I have a React Native app integrated with Relay and I want to delivery an offline-first experience for users.

So, in the first app launch a placeholder should be shown while data is being loaded. After that, every time the app be launched I want to show the last cached data while a fresh data is loaded.

I found this issue from 2015 and based on eyston's answer I've tried to implement a CacheManager based on relay-cache-manager using the AsyncStorage. With the CacheManager I can save and load relay records from cache but when the network is disabled the app isn't able to show cached data.

Is there any way of use relay cached data while relay is fetching fresh data?

like image 390
Helielson Santos Avatar asked Mar 05 '17 03:03

Helielson Santos


People also ask

Does relay work with react native?

The Relay SDK for React Native is easy to use and only takes a few minute to setup and get running.

How does relay cache work?

Relay combines your React components with that's needed specifically for that component and renders it optimally to your frontend app. Now, the cache works by associating each piece of data with a unique ID.

How do I get cached data in React?

Approach: Follow these simple steps in order to store single data into cache in ReactJS. We have created our addDataIntoCache function which takes the user data and store into the browser cache. When we click on the button, the function is triggered and data gets stored into the cache, and we see an alert popup.


1 Answers

We have a production app which uses Relay and RealmDB for offline experience. We took a separate approach from CacheManager because CacheManager was not quite ready at that time. We used relay-local-schema for this.

We defined the entire schema required for mobile using relay-local-schema. This could be the same file as what your backend server would be using for defining graphql schema and change the resolve function to resolve the data from realm db. For this we also created schema in realmdb which had nearly same structure as the graphql schema for simplicity of writing data returned by backend server to realmdb. You can also automate generating of this schema by using the graphql introspection query. We defined a custom network layer where we made sure that all Relay queries always touch the local db. In the sendQueries function all queries are resolved with relay-local-schema which gets resolved very fast and react views shows the old data and at same time a network request is made for each request in sendQueries function. On receiving data from network request it is written in realmdb and Relay in-memory is store is also populated with the new data, this automatically refreshes all the react views whose data changed. To write data to Relay in-memory store we used the following undocumented method

Relay.Store.getStoreData().handleQueryPayload(query, response);

You can get query object from request that you receive in sendQueries function using request.getQuery().

Our current implementation is bit tied up with our business logic and hence it is difficult to open source this logic. I'll try to provide a demo app is possible.

like image 156
meteors Avatar answered Nov 15 '22 07:11

meteors