@@ -52,6 +52,7 @@ Features
52
52
- TypeScript support
53
53
- Zero dependencies (peer deps: react, react-dom)
54
54
- GraphQL support (queries + mutations)
55
+ - Provider to set default a ` url ` and ` options `
55
56
56
57
### Examples
57
58
- <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 = () => {
178
179
)
179
180
}
180
181
```
182
+
181
183
#### GraphQL Mutation
182
184
```jsx
183
185
@@ -206,6 +208,78 @@ const App = () => {
206
208
}
207
209
` ` `
208
210
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
+
209
283
#### The Goal With Suspense <sup><strong>(not implemented yet)</strong></sup>
210
284
` ` ` jsx
211
285
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
304
378
305
379
Todos
306
380
------
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`
307
386
- [ ] Make work with React Suspense [current example WIP](https://codesandbox.io/s/7ww5950no0)
308
387
- [ ] get it all working on a SSR codesandbox, this way we can have api to call locally
309
388
- [ ] Allow option to fetch on server instead of just having ` loading` state
@@ -313,20 +392,16 @@ Todos
313
392
- [ ] if 2nd param of ` post` or one of the methods is a ` string` treat it as query params
314
393
- [ ] error handling if no url is passed
315
394
- [ ] 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 `
319
395
- [ ] github page/website
320
- - [ ] support for a global context config where you can set base url's (like Apollo's ` client` ) but better 😉
321
396
- [ ] fix code so Maintainability is A
322
397
- [ ] optimize badges [see awesome badge list](https://github.com/boennemann/badges)
323
- - [ ] add GraphQL ` useQuery` , ` useMutation`
324
398
- [ ] make GraphQL work with React Suspense
325
399
- [ ] make GraphQL examples
326
400
#### Mutations with Suspense <sup>(Not Implemented Yet)</sup>
327
401
` ` ` jsx
328
402
const App = () => {
329
403
const [todoTitle , setTodoTitle ] = useState (' ' )
404
+ // if there's no <Provider /> used, useMutation works this way
330
405
const mutation = useMutation (' http://example.com' , `
331
406
mutation CreateTodo($todoTitle string) {
332
407
todo(title: $todoTitle) {
0 commit comments