Skip to content

Commit ba26cc4

Browse files
authored
Update README.md
1 parent 2c30765 commit ba26cc4

File tree

1 file changed

+80
-5
lines changed

1 file changed

+80
-5
lines changed

README.md

+80-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Features
5252
- TypeScript support
5353
- Zero dependencies (peer deps: react, react-dom)
5454
- GraphQL support (queries + mutations)
55+
- Provider to set default a `url` and `options`
5556

5657
### Examples
5758
- <a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-in-nextjs-nn9fm'>Example - Next.js</a>
@@ -178,6 +179,7 @@ const App = () => {
178179
)
179180
}
180181
```
182+
181183
#### GraphQL Mutation
182184
```jsx
183185
@@ -206,6 +208,78 @@ const App = () => {
206208
}
207209
```
208210
211+
#### `Provider` using the GraphQL `useMutation` and `useQuery`
212+
213+
The `Provider` allows us to set a default `url`, `options` (such as headers) and so on.
214+
215+
```jsx
216+
import { Provider, useQuery, useMutation } from 'use-http'
217+
218+
// Query for todos
219+
const QueryComponent = () => {
220+
const request = useQuery(`
221+
query Todos($userID string!) {
222+
todos(userID: $userID) {
223+
id
224+
title
225+
}
226+
}
227+
`)
228+
229+
const getTodosForUser = id => request.query({ userID: id })
230+
231+
return (
232+
<Fragment>
233+
<button onClick={() => getTodosForUser('theUsersID')}>Get User's Todos</button>
234+
{!request.loading ? 'Loading...' : <pre>{request.data}</pre>}
235+
</Fragment>
236+
)
237+
}
238+
239+
// Add a new todo
240+
const MutationComponent = () => {
241+
const [todoTitle, setTodoTitle] = useState('')
242+
243+
const [data, loading, error, mutate] = useMutation(`
244+
mutation CreateTodo($todoTitle string) {
245+
todo(title: $todoTitle) {
246+
id
247+
title
248+
}
249+
}
250+
`)
251+
252+
const createtodo = () => mutate({ todoTitle })
253+
254+
return (
255+
<Fragment>
256+
<input onChange={e => setTodoTitle(e.target.value)} />
257+
<button onClick={createTodo}>Create Todo</button>
258+
{!loading ? 'Loading...' : <pre>{data}</pre>}
259+
</Fragment>
260+
)
261+
}
262+
263+
// these are default options and URL used in every request
264+
// inside the <Provider />. They can be overwritten individually
265+
const App = () => {
266+
267+
const options = {
268+
headers: {
269+
Authorization: 'Bearer:asdfasdfasdfasdfasdafd'
270+
}
271+
}
272+
273+
return (
274+
<Provider url='http://example.com' options={options}>
275+
<QueryComponent />
276+
<MutationComponent />
277+
<Provider/>
278+
)
279+
}
280+
281+
```
282+
209283
#### The Goal With Suspense <sup><strong>(not implemented yet)</strong></sup>
210284
```jsx
211285
import React, { Suspense, unstable_ConcurrentMode as ConcurrentMode, useEffect } from 'react'
@@ -304,6 +378,11 @@ If you have feature requests, let's talk about them in [this issue](https://gith
304378
305379
Todos
306380
------
381+
- [x] port to typescript
382+
- [x] badges
383+
- [X] if no url is specified, and we're in the browser, use `window.location.origin`
384+
- [X] support for a global context config where you can set base url's (like Apollo's `client`) but better 😉
385+
- [X] add GraphQL `useQuery`, `useMutation`
307386
- [ ] Make work with React Suspense [current example WIP](https://codesandbox.io/s/7ww5950no0)
308387
- [ ] get it all working on a SSR codesandbox, this way we can have api to call locally
309388
- [ ] Allow option to fetch on server instead of just having `loading` state
@@ -313,20 +392,16 @@ Todos
313392
- [ ] if 2nd param of `post` or one of the methods is a `string` treat it as query params
314393
- [ ] error handling if no url is passed
315394
- [ ] tests
316-
- [x] port to typescript
317-
- [x] badges
318-
- [ ] if no url is specified, and we're in the browser, use `window.location.href`
319395
- [ ] github page/website
320-
- [ ] support for a global context config where you can set base url's (like Apollo's `client`) but better 😉
321396
- [ ] fix code so Maintainability is A
322397
- [ ] optimize badges [see awesome badge list](https://github.com/boennemann/badges)
323-
- [ ] add GraphQL `useQuery`, `useMutation`
324398
- [ ] make GraphQL work with React Suspense
325399
- [ ] make GraphQL examples
326400
#### Mutations with Suspense <sup>(Not Implemented Yet)</sup>
327401
```jsx
328402
const App = () => {
329403
const [todoTitle, setTodoTitle] = useState('')
404+
// if there's no <Provider /> used, useMutation works this way
330405
const mutation = useMutation('http://example.com', `
331406
mutation CreateTodo($todoTitle string) {
332407
todo(title: $todoTitle) {

0 commit comments

Comments
 (0)