Skip to content

Commit 8e34e01

Browse files
authored
feat(useQuery): change default value for the suspend option to false (#80)
BREAKING CHANGE: The default for the `suspend` option of `useQuery` is changed to `false`, and that hook no longer uses suspense by default. Suspense for data fetching is not recommended yet for production code. Please look at the [issue #69](#69) for details.
1 parent 56abacc commit 8e34e01

File tree

4 files changed

+168
-54
lines changed

4 files changed

+168
-54
lines changed

README.md

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,13 @@ const GET_DOGS = gql`
8989
`;
9090

9191
const Dogs = () => {
92-
const { data, error } = useQuery(GET_DOGS);
93-
if (error) return `Error! ${error.message}`;
92+
const { data, error, loading } = useQuery(GET_DOGS);
93+
if (loading) {
94+
return <div>Loading...</div>;
95+
};
96+
if (error) {
97+
return `Error! ${error.message}`;
98+
};
9499

95100
return (
96101
<ul>
@@ -100,35 +105,38 @@ const Dogs = () => {
100105
</ul>
101106
);
102107
};
108+
103109
```
104110

105-
To check if data is loaded use the
106-
[Suspense](https://reactjs.org/docs/code-splitting.html#suspense) component:
111+
### Usage with Suspense (experimental)
107112

108-
```javascript
109-
import React, { Suspense } from 'react';
113+
You can use `useQuery` with [React Suspense](https://www.youtube.com/watch?v=6g3g0Q_XVb4)
114+
with the `{ suspend: true }` option.
115+
Please note that it's not yet recommended to use it in production. Please look
116+
at the [issue #69](https://github.com/trojanowski/react-apollo-hooks/issues/69)
117+
for details.
110118

111-
const MyComponent = () => (
112-
<Suspense fallback={<div>Loading...</div>}>
113-
<Dogs />
114-
</Suspense>
115-
);
116-
```
117-
118-
Alternatively you can use the `useQuery` hook without suspense with the
119-
`{ suspend: false }` option. It's required if you want to use non-standard fetch
120-
policy. You have to manage loading state by yourself in that case:
119+
Example usage:
121120

122121
```javascript
123122
import gql from 'graphql-tag';
123+
import React, { Suspense } from 'react';
124124
import { useQuery } from 'react-apollo-hooks';
125125

126-
const GET_DOGS = gql`...`;
126+
const GET_DOGS = gql`
127+
{
128+
dogs {
129+
id
130+
breed
131+
}
132+
}
133+
`;
127134

128135
const Dogs = () => {
129-
const { data, error, loading } = useQuery(GET_DOGS, { suspend: false });
130-
if (loading) return <div>Loading...</div>;
131-
if (error) return `Error! ${error.message}`;
136+
const { data, error } = useQuery(GET_DOGS, { suspend: true });
137+
if (error) {
138+
return `Error! ${error.message}`;
139+
}
132140

133141
return (
134142
<ul>
@@ -138,6 +146,12 @@ const Dogs = () => {
138146
</ul>
139147
);
140148
};
149+
150+
const MyComponent = () => (
151+
<Suspense fallback={<div>Loading...</div>}>
152+
<Dogs />
153+
</Suspense>
154+
);
141155
```
142156

143157
## useMutation
@@ -264,15 +278,15 @@ component is not supported. You can use `unstable_SuspenseSSR` provided
264278
by this library instead:
265279

266280
```javascript
267-
import { unstable_SuspenseSSR } from 'react-apollo-hooks';
281+
import { unstable_SuspenseSSR as UnstableSuspenseSSR } from 'react-apollo-hooks';
268282

269283
function MyComponent() {
270284
return (
271-
<unstable_SuspenseSSR fallback={<Spinner />}>
285+
<UnstableSuspenseSSR fallback={<Spinner />}>
272286
<div>
273287
<ComponentWithGraphqlQuery />
274288
</div>
275-
</unstable_SuspenseSSR>
289+
</UnstableSuspenseSSR>
276290
);
277291
}
278292
```

src/__tests__/useMutation-test.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import gql from 'graphql-tag';
2-
import React, { Suspense } from 'react';
2+
import React from 'react';
33
import { cleanup, fireEvent, render } from 'react-testing-library';
44

55
import { ApolloProvider, useMutation, useQuery } from '..';
@@ -160,13 +160,17 @@ afterEach(cleanup);
160160

161161
it('should create a function to perform mutations', async () => {
162162
function TasksWithMutation() {
163-
const { data, error } = useQuery(TASKS_QUERY);
163+
const { data, error, loading } = useQuery(TASKS_QUERY);
164164
const toggleTask = useMutation(TOGGLE_TASK_MUTATION);
165165

166166
if (error) {
167167
throw error;
168168
}
169169

170+
if (loading) {
171+
return <>Loading</>;
172+
}
173+
170174
return (
171175
<TaskList
172176
onChange={task => toggleTask({ variables: { taskId: task.id } })}
@@ -178,9 +182,7 @@ it('should create a function to perform mutations', async () => {
178182
const client = createClient({ mocks: TASKS_MOCKS });
179183
const { container } = render(
180184
<ApolloProvider client={client}>
181-
<Suspense fallback={<div>Loading</div>}>
182-
<TasksWithMutation />
183-
</Suspense>
185+
<TasksWithMutation />
184186
</ApolloProvider>
185187
);
186188

@@ -199,7 +201,7 @@ it('should create a function to perform mutations', async () => {
199201

200202
it('should allow to pass options forwarded to the mutation', async () => {
201203
function TasksWithMutation() {
202-
const { data, error } = useQuery(TASKS_QUERY);
204+
const { data, error, loading } = useQuery(TASKS_QUERY);
203205
const addTask = useMutation<any, { input: Partial<TaskFragment> }>(
204206
ADD_TASK_MUTATION,
205207
{
@@ -222,6 +224,10 @@ it('should allow to pass options forwarded to the mutation', async () => {
222224
throw error;
223225
}
224226

227+
if (loading) {
228+
return <>Loading</>;
229+
}
230+
225231
return (
226232
<>
227233
<TaskList onChange={noop} tasks={data.tasks} />
@@ -235,9 +241,7 @@ it('should allow to pass options forwarded to the mutation', async () => {
235241
const client = createClient({ mocks: TASKS_MOCKS });
236242
const { container, getByTestId } = render(
237243
<ApolloProvider client={client}>
238-
<Suspense fallback={<div>Loading</div>}>
239-
<TasksWithMutation />
240-
</Suspense>
244+
<TasksWithMutation />
241245
</ApolloProvider>
242246
);
243247

0 commit comments

Comments
 (0)