Skip to content

Commit

Permalink
#1 fix(IndexedDB for react-native), feat(reduced depenency from idb),…
Browse files Browse the repository at this point in the history
… feat(offline store customizable)
  • Loading branch information
morrys committed May 1, 2019
1 parent 15e4e3f commit ef87ab4
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 206 deletions.
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ yarn add react-relay relay-offline
```


## Example
## React Web Example

The [relay-offline-examples](https://github.com/morrys/relay-examples) repository is a fork of [relay-examples](https://github.com/relayjs/relay-examples) and contains an integration of react-relay-offline. To try it out:

Expand Down Expand Up @@ -46,15 +46,16 @@ Change the renderer
import {QueryRenderer} from 'react-relay-offline';
```

## React Native
## React Native Example

How to create the environment

```
import { Network } from 'relay-runtime';
import { OfflineStore, Store, Environment, RecordSource } from 'react-relay-offline';
const network = Network.create(fetchQuery);
const storeOffline = OfflineStore(network, {});
const storeOffline = OfflineStore(network);
const source = new RecordSource(storeOffline);
const store = new Store(storeOffline, source);
const modernEnvironment = new Environment({ network, store, dataFrom: "CACHE_FIRST" }, storeOffline);
Expand All @@ -68,6 +69,42 @@ import {QueryRenderer} from 'react-relay-offline';

In QueryRenderer you need to set the property LoadingComponent.

## IndexedDB

localStorage is used as the default react web persistence, while AsyncStorage is used for react-native.

To use persistence via IndexedDB:

```
import OfflineStore from 'react-relay-offline/lib/runtime/redux/OfflineStoreIDB'
```

## OfflineStore

It is possible to customize the offline store through these parameters:

* storage: any
* keyPrefix: string
* customWhitelist: string[]
* serialize: boolean
* customReducers: ReducersMapObject


## QueryRenderer

* Add "LoadingComponent" property
* Add "cached" property in render function

```
<QueryRenderer
environment={environment}
query={query}
variables={{}}
LoadingComponent={<Loading />}
render={({ props, error, retry, cached }) => {
```


## Requirement

* Version 3.0.0 or 4.0.0 of the react-relay library
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "react-relay-offline",
"version": "0.0.3",
"version": "0.1.0",
"keywords": [
"graphql",
"relay",
"offline"
"offline",
"react"
],
"main": "lib/index.js",
"license": "SEE LICENSE IN LICENSE",
Expand Down
13 changes: 10 additions & 3 deletions src/QueryRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type RenderProps = {
error: Error,
props: Object,
retry: () => void,
cached?: boolean
};

export interface Props {
Expand Down Expand Up @@ -300,6 +301,7 @@ function fetchQueryAndComputeStateFromProps(
}
}

var cached = false;
try {
const dataFrom = props.dataFrom || genericEnvironment.getDataFrom();
const storeSnapshot =
Expand All @@ -308,7 +310,9 @@ function fetchQueryAndComputeStateFromProps(
offline
? queryFetcher.lookupInStore(genericEnvironment, operation)
: null;
const cached = (offline || (dataFrom === CACHE_FIRST && storeSnapshot && !(storeSnapshot as any).expired));
cached = (
offline || //TODO to be evaluated ( offline && storeSnapshot)
(dataFrom === CACHE_FIRST && storeSnapshot && !(storeSnapshot as any).expired));
if (!cached) {
queryFetcher.resetCacheSelectionReference();
}
Expand All @@ -335,7 +339,7 @@ function fetchQueryAndComputeStateFromProps(
return {
error: null,
relayContext,
renderProps: getLoadingRenderProps(),
renderProps: {...getLoadingRenderProps(), cached},
snapshot: null,
requestCacheKey,
cached
Expand All @@ -350,7 +354,8 @@ function fetchQueryAndComputeStateFromProps(
null,
snapshot,
queryFetcher,
retryCallbacks
retryCallbacks,
cached
),
cached,
snapshot,
Expand Down Expand Up @@ -409,10 +414,12 @@ function getRenderProps(
snapshot: Snapshot,
queryFetcher: QueryFetcherOriginal,
retryCallbacks: RetryCallbacks,
cached: boolean = false,
): RenderProps {
return {
error: error ? error : null,
props: snapshot ? snapshot.data : null,
cached: cached,
retry: () => {
const syncSnapshot = queryFetcher.retry();
if (
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export {default as QueryRenderer} from "./QueryRendererOffline";
export {default as Environment} from './runtime/RelayModernEnvironment';
export {default as Store} from './runtime/Store';
export {default as RecordSource} from './runtime/RecordSource';
export {default as OfflineStore} from './runtime/redux/Store';
export {default as OfflineStore} from './runtime/redux/OfflineStore';
2 changes: 1 addition & 1 deletion src/runtime/RecordSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Store, Action } from 'redux';
import {Record} from 'relay-runtime/lib/RelayCombinedEnvironmentTypes';
import * as RelayRecordState from 'relay-runtime/lib/RelayRecordState';
import {MutableRecordSource} from 'relay-runtime/lib/RelayStoreTypes';
import { RelayCache, NORMALIZED_CACHE_KEY, writeThunk, WRITE_CACHE_ACTION } from "./redux/Store";
import { RelayCache, NORMALIZED_CACHE_KEY, writeThunk, WRITE_CACHE_ACTION } from "./redux/OfflineStore";

const {EXISTENT, NONEXISTENT, UNKNOWN} = RelayRecordState;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Store, RecordSource} from 'relay-runtime'

import { Store as StoreRedux, Action } from 'redux';
import { WRITE_ROOT_ACTION, NORMALIZED_ROOTS_KEY, RelayCache, writeThunk, } from './redux/Store';
import { WRITE_ROOT_ACTION, NORMALIZED_ROOTS_KEY, RelayCache, writeThunk, } from './redux/OfflineStore';
import {
MutableRecordSource,
Scheduler,
Expand Down
Loading

0 comments on commit ef87ab4

Please sign in to comment.