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

updated to use latest libraries/versions #180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
82 changes: 6 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ Use [Apollo Client](https://github.com/apollographql/apollo-client) as React

Or if using [yarn](https://yarnpkg.com/en/)


`yarn add react-apollo-hooks`

# Example

<https://codesandbox.io/s/8819w85jn9> is a port of Pupstagram sample app to
react-apollo-hooks.

> NOTE: This example is outdated and may not work properly. Support for React Suspense is no longer supported.

# API

## ApolloProvider
Expand Down Expand Up @@ -97,48 +98,7 @@ const Dogs = () => {
const { data, error, loading } = useQuery(GET_DOGS);
if (loading) {
return <div>Loading...</div>;
};
if (error) {
return <div>Error! {error.message}</div>;
};

return (
<ul>
{data.dogs.map(dog => (
<li key={dog.id}>{dog.breed}</li>
))}
</ul>
);
};

```

### Usage with Suspense (experimental)

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.

Example usage:

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

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

const Dogs = () => {
const { data, error } = useQuery(GET_DOGS, { suspend: true });
if (error) {
return <div>Error! {error.message}</div>;
}
Expand All @@ -151,19 +111,8 @@ const Dogs = () => {
</ul>
);
};

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

There are known issues with suspense mode for `useQuery`:

* only the `cache-first` fetch policy is supported ([#13](https://github.com/trojanowski/react-apollo-hooks/issues/13))
* `networkStatus` returned by `useQuery` is undefined ([#68](https://github.com/trojanowski/react-apollo-hooks/pull/68))

## useMutation

```javascript
Expand Down Expand Up @@ -250,14 +199,14 @@ const NewMessagesIndicator = () => {

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

if (error) {
return <div>Error! {error.message}</div>;
};
}

return <div>{data.newMessagesCount} new messages</div>;
}
};
```

For more advanced use cases, e. g. when you'd like to show a notification
Expand All @@ -275,7 +224,7 @@ const { data, error, loading } = useSubscription(MY_SUBSCRIPTION, {
// data and the Apollo client. You can use methods of the client to update
// the Apollo cache:
// https://www.apollographql.com/docs/react/advanced/caching.html#direct
}
},
// ... rest options
});
```
Expand Down Expand Up @@ -335,22 +284,3 @@ app.get('/', async (req, res) => {
res.send(renderedHtml);
});
```

`getMarkupFromTree` supports `useQuery` hooks invoked in both suspense
and non-suspense mode, but the [React.Suspense](https://reactjs.org/docs/react-api.html#reactsuspense)
component is not supported. You can use `unstable_SuspenseSSR` provided
by this library instead:

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

function MyComponent() {
return (
<UnstableSuspenseSSR fallback={<Spinner />}>
<div>
<ComponentWithGraphqlQuery />
</div>
</UnstableSuspenseSSR>
);
}
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.1.0",
"@testing-library/react": "^8.0.1",
"@types/graphql": "^14.0.3",
"@types/jest": "^23.3.10",
"@types/lodash": "^4.14.119",
"@types/node": "^12.0.8",
"@types/react": "^16.8.2",
"@types/react-dom": "^16.8.0",
"apollo-cache-inmemory": "^1.3.11",
Expand All @@ -103,7 +105,6 @@
"prettier": "^1.15.2",
"react": "16.8.1",
"react-dom": "16.8.1",
"react-testing-library": "^5.5.3",
"rimraf": "^2.6.2",
"standard-version": "^4.4.0",
"tslint": "^5.12.0",
Expand Down
12 changes: 0 additions & 12 deletions src/SuspenseSSR.tsx

This file was deleted.

112 changes: 0 additions & 112 deletions src/__tests__/SuspenseSSR-test.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/__tests__/useApolloClient-test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cleanup, render } from '@testing-library/react';
import React from 'react';
import { cleanup, render } from 'react-testing-library';

import { ApolloProvider, useApolloClient } from '..';
import createClient from '../__testutils__/createClient';
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/useMutation-test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cleanup, fireEvent, render } from '@testing-library/react';
import gql from 'graphql-tag';
import React from 'react';
import { cleanup, fireEvent, render } from 'react-testing-library';

import { ApolloProvider, useMutation, useQuery } from '..';
import createClient from '../__testutils__/createClient';
Expand Down
Loading