Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useQuery): change default value for the suspend option to false #80

Merged
merged 1 commit into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 37 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ const GET_DOGS = gql`
`;

const Dogs = () => {
const { data, error } = useQuery(GET_DOGS);
if (error) return `Error! ${error.message}`;
const { data, error, loading } = useQuery(GET_DOGS);
if (loading) {
return <div>Loading...</div>;
};
if (error) {
return `Error! ${error.message}`;
};

return (
<ul>
Expand All @@ -100,35 +105,38 @@ const Dogs = () => {
</ul>
);
};

```

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

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

const MyComponent = () => (
<Suspense fallback={<div>Loading...</div>}>
<Dogs />
</Suspense>
);
```

Alternatively you can use the `useQuery` hook without suspense with the
`{ suspend: false }` option. It's required if you want to use non-standard fetch
policy. You have to manage loading state by yourself in that case:
Example usage:

```javascript
import gql from 'graphql-tag';
import React, { Suspense } from 'react';
import { useQuery } from 'react-apollo-hooks';

const GET_DOGS = gql`...`;
const GET_DOGS = gql`
{
dogs {
id
breed
}
}
`;

const Dogs = () => {
const { data, error, loading } = useQuery(GET_DOGS, { suspend: false });
if (loading) return <div>Loading...</div>;
if (error) return `Error! ${error.message}`;
const { data, error } = useQuery(GET_DOGS, { suspend: true });
if (error) {
return `Error! ${error.message}`;
}

return (
<ul>
Expand All @@ -138,6 +146,12 @@ const Dogs = () => {
</ul>
);
};

const MyComponent = () => (
<Suspense fallback={<div>Loading...</div>}>
<Dogs />
</Suspense>
);
```

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

```javascript
import { unstable_SuspenseSSR } from 'react-apollo-hooks';
import { unstable_SuspenseSSR as UnstableSuspenseSSR } from 'react-apollo-hooks';

function MyComponent() {
return (
<unstable_SuspenseSSR fallback={<Spinner />}>
<UnstableSuspenseSSR fallback={<Spinner />}>
<div>
<ComponentWithGraphqlQuery />
</div>
</unstable_SuspenseSSR>
</UnstableSuspenseSSR>
);
}
```
22 changes: 13 additions & 9 deletions src/__tests__/useMutation-test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gql from 'graphql-tag';
import React, { Suspense } from 'react';
import React from 'react';
import { cleanup, fireEvent, render } from 'react-testing-library';

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

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

if (error) {
throw error;
}

if (loading) {
return <>Loading</>;
}

return (
<TaskList
onChange={task => toggleTask({ variables: { taskId: task.id } })}
Expand All @@ -178,9 +182,7 @@ it('should create a function to perform mutations', async () => {
const client = createClient({ mocks: TASKS_MOCKS });
const { container } = render(
<ApolloProvider client={client}>
<Suspense fallback={<div>Loading</div>}>
<TasksWithMutation />
</Suspense>
<TasksWithMutation />
</ApolloProvider>
);

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

it('should allow to pass options forwarded to the mutation', async () => {
function TasksWithMutation() {
const { data, error } = useQuery(TASKS_QUERY);
const { data, error, loading } = useQuery(TASKS_QUERY);
const addTask = useMutation<any, { input: Partial<TaskFragment> }>(
ADD_TASK_MUTATION,
{
Expand All @@ -222,6 +224,10 @@ it('should allow to pass options forwarded to the mutation', async () => {
throw error;
}

if (loading) {
return <>Loading</>;
}

return (
<>
<TaskList onChange={noop} tasks={data.tasks} />
Expand All @@ -235,9 +241,7 @@ it('should allow to pass options forwarded to the mutation', async () => {
const client = createClient({ mocks: TASKS_MOCKS });
const { container, getByTestId } = render(
<ApolloProvider client={client}>
<Suspense fallback={<div>Loading</div>}>
<TasksWithMutation />
</Suspense>
<TasksWithMutation />
</ApolloProvider>
);

Expand Down
Loading