Skip to content

Commit 23ea1cf

Browse files
authored
Add graphql-hooks example
1 parent 5e26213 commit 23ea1cf

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/content/code/language-support/javascript/client/graphql-hooks.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,66 @@ url: https://github.com/nearform/graphql-hooks
55
github: nearform/graphql-hooks
66
npm: "graphql-hooks"
77
---
8+
9+
- 🥇 First-class hooks API
10+
- ⚖️ _Tiny_ bundle: only 7.6kB (2.8 gzipped)
11+
- 📄 Full SSR support: see [graphql-hooks-ssr](packages/graphql-hooks-ssr)
12+
- 🔌 Plugin Caching: see [graphql-hooks-memcache](packages/graphql-hooks-memcache)
13+
- 🔥 No more render props hell
14+
- ⏳ Handle loading and error states with ease
15+
16+
### Quickstart
17+
18+
```bash
19+
npm install graphql-hooks
20+
```
21+
22+
First you'll need to create a client and wrap your app with the provider:
23+
24+
```js
25+
import { GraphQLClient, ClientContext } from 'graphql-hooks'
26+
27+
const client = new GraphQLClient({
28+
url: '/graphql'
29+
})
30+
31+
function App() {
32+
return (
33+
<ClientContext.Provider value={client}>
34+
{/* children */}
35+
</ClientContext.Provider>
36+
)
37+
}
38+
```
39+
40+
Now in your child components you can make use of `useQuery`:
41+
42+
```js
43+
import { useQuery } from 'graphql-hooks'
44+
45+
const HOMEPAGE_QUERY = `query HomePage($limit: Int) {
46+
users(limit: $limit) {
47+
id
48+
name
49+
}
50+
}`
51+
52+
function MyComponent() {
53+
const { loading, error, data } = useQuery(HOMEPAGE_QUERY, {
54+
variables: {
55+
limit: 10
56+
}
57+
})
58+
59+
if (loading) return 'Loading...'
60+
if (error) return 'Something Bad Happened'
61+
62+
return (
63+
<ul>
64+
{data.users.map(({ id, name }) => (
65+
<li key={id}>{name}</li>
66+
))}
67+
</ul>
68+
)
69+
}
70+
```

0 commit comments

Comments
 (0)