Skip to content

Commit 7d9ca76

Browse files
authored
Merge pull request #57 from morrys/render-as-you-fetch
useLazyLoadQuery & render-as-you-fetch pattern in offline mode
2 parents f0b66e9 + b3b10d2 commit 7d9ca76

18 files changed

+436
-142
lines changed

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"trailingComma": "all",
55
"arrowParens": "always",
66
"printWidth": 140,
7-
"parser": "typescript"
7+
"parser": "typescript",
8+
"endOfLine": "auto"
89
}

README.md

Lines changed: 98 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -77,39 +77,20 @@ You then need to link the native parts of the library for the platforms you are
7777

7878
<a href="https://memorangapp.com" target="_blank"><img height=40px src="https://github.com/morrys/react-relay-offline/raw/master/docs/assets/memorang-logo.png" alt="Memorang">
7979

80-
## React Web Example
80+
## react-relay-offline examples
8181

82-
The [react-relay-offline-examples](https://github.com/morrys/react-relay-offline-example) repository contains an integration of react-relay-offline. To try it out:
82+
The [offline-examples](https://github.com/morrys/offline-examples) repository contains example projects on how to use react-relay-offline:
8383

84-
```
85-
git clone https://github.com/morrys/react-relay-offline-example.git
86-
cd react-relay-offline-example/todo
87-
yarn
88-
yarn build
89-
yarn start
90-
```
91-
92-
Then, just point your browser at `http://localhost:3000`.
93-
94-
or
95-
96-
```
97-
git clone https://github.com/morrys/react-relay-offline-example.git
98-
cd react-relay-offline-example/todo-updater
99-
yarn
100-
yarn build
101-
yarn start
102-
```
84+
* `nextjs-ssr-preload`: using the render-as-you-fetch pattern with loadQuery in SSR contexts
85+
* `nextjs`: using the QueryRenderer in SSR contexts
86+
* `react-native/todo-updater`: using QueryRender in an RN application
87+
* `todo-updater`: using the QueryRender
88+
* `suspense/cra`: using useLazyLoadQuery in a CRA
89+
* `suspense/nextjs-ssr-preload`: using the render-as-you-fetch pattern with loadLazyQuery in react concurrent + SSR contexts
90+
* `suspense/nextjs-ssr`: using useLazyLoadQuery in SSR contexts
10391

104-
Then, just point your browser at `http://localhost:3000`.
92+
To try it out!
10593

106-
## React NextJS Offline SSR Example
107-
108-
The [React NextJS Offline SSR Example](https://github.com/morrys/offline-examples/tree/master/relay/nextjs)
109-
110-
## React Native Example
111-
112-
The [react native offline example](https://github.com/morrys/offline-examples#react-native)
11394

11495
## Environment
11596

@@ -259,10 +240,35 @@ import { QueryRenderer } from 'react-relay-offline';
259240
render={({ props, error, retry, cached }) => {
260241
```
261242
243+
## useQuery
244+
245+
```ts
246+
import { useQuery } from "react-relay-offline";
247+
const hooksProps = useQuery(query, variables, {
248+
networkCacheConfig: cacheConfig,
249+
fetchPolicy,
250+
ttl
251+
});
252+
```
253+
254+
## useLazyLoadQuery
255+
256+
```ts
257+
import { useQuery } from "react-relay-offline";
258+
const hooksProps = useLazyLoadQuery(query, variables, {
259+
networkCacheConfig: cacheConfig,
260+
fetchPolicy,
261+
ttl
262+
});
263+
```
264+
262265
## useRestore & loading
263266
264-
the **useRestore** hook allows you to manage the restore of data persisted in the storage.
265-
**To be used if relay components are used outside of the QueryRenderer** or **for web applications without SSR & react-native** (
267+
the **useRestore** hook allows you to manage the hydratation of persistent data in memory and to initialize the environment.
268+
269+
**It must always be used before using environement in web applications without SSR & react legacy & react-native.**
270+
271+
**Otherwise, for SSR and react concurrent applications the restore is natively managed by QueryRenderer & useQueryLazyLoad & useQuery.**
266272
267273
```
268274
const isRehydrated = useRestore(environment);
@@ -271,9 +277,6 @@ const isRehydrated = useRestore(environment);
271277
}
272278
```
273279
274-
**For SSR web applications there is a native management in the QueryRenderer to correctly manage the DOM returned by the server and restore the environment**
275-
276-
277280
## fetchQuery
278281
279282
```ts
@@ -294,24 +297,76 @@ import { useNetInfo } from "react-relay-offline";
294297
import { NetInfo } from "react-relay-offline";
295298
```
296299
297-
## Hooks & useQuery
300+
## Supports Hooks from relay-hooks
298301
299302
Now you can use hooks (useFragment, usePagination, useRefetch) from [relay-hooks](https://github.com/relay-tools/relay-hooks)
300303
301-
while it is necessary to use `useQuery` of react-relay-offline to manage the offline.
304+
## render-as-you-fetch & usePreloadedQuery
305+
306+
### loadQuery
307+
308+
* input parameters
309+
310+
same as useQuery + environment
311+
312+
* output parameters
313+
*
314+
`next: <TOperationType extends OperationType>(
315+
environment: Environment,
316+
gqlQuery: GraphQLTaggedNode,
317+
variables?: TOperationType['variables'],
318+
options?: QueryOptions,
319+
) => Promise<void>`: fetches data. A promise returns to allow the await in case of SSR
320+
* `dispose: () => void`: cancel the subscription and dispose of the fetch
321+
* `subscribe: (callback: (value: any) => any) => () => void`: used by the usePreloadedQuery
322+
* `getValue <TOperationType>(environment?: Environment,) => OfflineRenderProps<TOperationType> | Promise<any>`: used by the usePreloadedQuery
302323
303324
```ts
304-
import { useQuery } from "react-relay-offline";
305-
const hooksProps = useQuery(query, variables, {
306-
networkCacheConfig: cacheConfig,
307-
fetchPolicy,
308-
ttl
309-
});
325+
import {graphql, loadQuery} from 'react-relay-offline';
326+
import {environment} from ''./environment';
327+
328+
const query = graphql`
329+
query AppQuery($id: ID!) {
330+
user(id: $id) {
331+
name
332+
}
333+
}
334+
`;
335+
336+
const prefetch = loadQuery();
337+
prefetch.next(
338+
environment,
339+
query,
340+
{id: '4'},
341+
{fetchPolicy: 'store-or-network'},
342+
);
343+
// pass prefetch to usePreloadedQuery()
344+
```
345+
346+
### loadLazyQuery
347+
348+
**is the same as loadQuery but must be used with suspense**
349+
350+
### render-as-you-fetch in SSR
351+
352+
In SSR contexts, **not using the useRestore hook** it is necessary to manually invoke the hydrate but without using the await.
353+
354+
This will allow the usePreloadedQuery hook to correctly retrieve the data from the store and once the hydration is done it will be react-relay-offline
355+
356+
to notify any updated data in the store.
357+
358+
```ts
359+
if (!environment.isRehydrated() && ssr) {
360+
environment.hydrate().then(() => {}).catch((error) => {});
361+
}
362+
prefetch.next(environment, QUERY_APP, variables, {
363+
fetchPolicy: NETWORK_ONLY,
364+
});
310365
```
311366
312367
## Requirement
313368
314-
- Version >=6.0.0 of the react-relay library
369+
- Version >=8.0.0 of the react-relay library
315370
- When a new node is created by mutation the id must be generated in the browser to use it in the optimistic response
316371
317372
## License

0 commit comments

Comments
 (0)