RunQuery Pattern
We recommend wrapping queries with runQuery
to enable offline first and optimistic creation / mutation functionality.
Depending on the validity of the cache, runQuery
can return different values:
Cache is not available:
Source | Data | Loading |
---|---|---|
Cache | undefined | true |
Server | server data | false |
Cache is available but expired:
Source | Data | Loading |
---|---|---|
Cache | cache data | true |
Server | server data | false |
Cache is available and valid:
Source | Data | Loading |
---|---|---|
cache | cache data | false |
In case of error:
Source | Data | Error |
---|---|---|
server | undefined | Error Object |
Based on data from the above tables. There are some cases that need to be considered.
If you don't need the expired cache data:
if (loading) return
For cases when there is an error from the server
if(!data) return
For cases you have already subscribed
if (subscribed) return
Add one or all of these inside the callback for `runQuery` based on your use case
Getter
You can set the period of validity of the cache manually using the third parameter. Example: to set the cached duration to an hour
Flow Diagram
Mutator
RunQuery will do an optimistic mutation (creation, update, delete operation) to the cache before making a request to the server.
Pagination
The runQuery
for an query that has pagination acts a little differently. Instead of accepting a callback that passes just the object of interest, it will also pass a pages
object with the key prevPage
and nextPage
referring to the Amity.Page
object that can be used to query the previous page and the next page, respectively. Example:
Last updated