RunQuery Pattern

Every action inside of ts-sdk can be wrapped by runQuery to enable offline first functionality.


For a getter, runQuery will first try to get the data from cache. If the data already exists in the cache, it will not fetch from the server. Otherwise, it will fetch the data and store the cache accordingly.

You can set the period of validity of the cache manually using the third parameter. With this example, the cache will be considered valid for an hour.

Flow Diagram


For a mutator, runQuery will do an optimistic mutation (creation, update, delete operation) to the cache before making a request to the server.


The runQuery for an action that requires 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. A sample code is provided below:

Last updated