|
1 | | -import React, {useState} from 'react'; |
| 1 | +import {useQuery} from '@apollo/react-hooks'; |
2 | 2 | import gql from 'graphql-tag'; |
3 | | -import {Query} from 'react-apollo'; |
4 | | -import usePendingPromise from './usePendingPromise'; |
| 3 | +import React, {useState, useEffect} from 'react'; |
5 | 4 |
|
6 | 5 | type Props = { |
7 | 6 | isBroken?: boolean; |
@@ -29,69 +28,60 @@ interface Variables { |
29 | 28 |
|
30 | 29 | export default function DataFetcher({isBroken}: Props) { |
31 | 30 | const [skip, setSkip] = useState(true); |
32 | | - const [queryPromise, setQueryPromise] = usePendingPromise(); |
| 31 | + const {data, loading, error, refetch} = useQuery<Data, Variables>(query, { |
| 32 | + context: {useApolloNetworkStatus: true}, |
| 33 | + notifyOnNetworkStatusChange: true, |
| 34 | + fetchPolicy: 'network-only', |
| 35 | + skip, |
| 36 | + variables: isBroken ? undefined : {id: '1'} |
| 37 | + }); |
33 | 38 |
|
34 | | - function onFetch() { |
| 39 | + function onFetchClick() { |
35 | 40 | setSkip(false); |
36 | 41 | } |
37 | 42 |
|
38 | | - return ( |
39 | | - <p> |
40 | | - <Query<Data, Variables> |
41 | | - context={{useApolloNetworkStatus: true}} |
42 | | - fetchPolicy="network-only" |
43 | | - onError={error => { |
44 | | - // eslint-disable-next-line no-console |
45 | | - console.error(error); |
46 | | - }} |
47 | | - query={query} |
48 | | - skip={skip} |
49 | | - variables={isBroken ? undefined : {id: '1'}} |
50 | | - > |
51 | | - {({data, loading, error, refetch}) => { |
52 | | - function onRefetchClick() { |
53 | | - const result = refetch(); |
54 | | - setQueryPromise(result); |
55 | | - } |
| 43 | + function onRefetchClick() { |
| 44 | + refetch(); |
| 45 | + } |
| 46 | + |
| 47 | + function onRetryClick() { |
| 48 | + refetch(); |
| 49 | + } |
56 | 50 |
|
57 | | - function onRetryClick() { |
58 | | - refetch(); |
59 | | - } |
| 51 | + useEffect(() => { |
| 52 | + if (error) console.error(error); |
| 53 | + }, [error]); |
60 | 54 |
|
61 | | - if (skip) { |
62 | | - return ( |
63 | | - <span> |
64 | | - Idle{' '} |
65 | | - <button onClick={onFetch}> |
66 | | - {isBroken ? 'Broken fetch' : 'Fetch'} |
67 | | - </button> |
68 | | - </span> |
69 | | - ); |
70 | | - } |
| 55 | + let content; |
| 56 | + if (skip) { |
| 57 | + content = ( |
| 58 | + <> |
| 59 | + Idle{' '} |
| 60 | + <button onClick={onFetchClick}> |
| 61 | + {isBroken ? 'Broken fetch' : 'Fetch'} |
| 62 | + </button> |
| 63 | + </> |
| 64 | + ); |
| 65 | + } else if (data && data.user) { |
| 66 | + content = ( |
| 67 | + <> |
| 68 | + User: {data.user.name}{' '} |
| 69 | + <button disabled={loading} onClick={onRefetchClick}> |
| 70 | + Refetch |
| 71 | + </button> |
| 72 | + </> |
| 73 | + ); |
| 74 | + } else if (loading) { |
| 75 | + content = 'Loading …'; |
| 76 | + } else if (error) { |
| 77 | + content = ( |
| 78 | + <> |
| 79 | + Error <button onClick={onRetryClick}>Retry</button>{' '} |
| 80 | + </> |
| 81 | + ); |
| 82 | + } else { |
| 83 | + throw new Error('Unexpected state'); |
| 84 | + } |
71 | 85 |
|
72 | | - return ( |
73 | | - <span> |
74 | | - {data && data.user ? ( |
75 | | - <span> |
76 | | - User: {data.user.name}{' '} |
77 | | - <button |
78 | | - disabled={queryPromise != null} |
79 | | - onClick={onRefetchClick} |
80 | | - > |
81 | | - Refetch |
82 | | - </button> |
83 | | - </span> |
84 | | - ) : loading ? ( |
85 | | - 'Loading …' |
86 | | - ) : error ? ( |
87 | | - <> |
88 | | - Error <button onClick={onRetryClick}>Retry</button> |
89 | | - </> |
90 | | - ) : null} |
91 | | - </span> |
92 | | - ); |
93 | | - }} |
94 | | - </Query> |
95 | | - </p> |
96 | | - ); |
| 86 | + return <p>{content}</p>; |
97 | 87 | } |
0 commit comments