An example of modern relay caching

I would like to enable caching in my native application. I am using GraphQL with Relay modern. I found out that caching is not enabled by default in relays, but they exposed RelayQueryResponseCache from relay-runtime , which we can add to the fetchQuery function in our API. I read a discussion here and here about this, but did not see any example to start. Can someone help me with this?

EDIT:

Ok, I came up with a solution. I think it misses a few things, but so far it satisfies our needs. I noticed that passing something to the QueryRenderer in cacheConfig results passing this value to the fetchQuery function inside my environment. So I created a Component that loads data in some relation and solves it into the correct json structure requested by the request. Then I return it to state. Then I expanded Component , which contains a QueryRenderer with the created "cache loader". Now when componentWillMount() is called, I request cached data. During this, I set this.state.loading = true , so I can handle the loading status. Reading from the database is asynchronous. I use this class in other components as well. Each processes its cache data. I just pass it a QueryRenderer .

However, I thought this added additional logic to each Component that is supported by this caching. Probably passing cache permission as cacheConfig and allowing cached data directly inside the environment would be much cleaner.

+5
source share

Source: https://habr.com/ru/post/1269927/


All Articles