|
| 1 | +import { Suspense, useTransition } from "react"; |
| 2 | +import { |
| 3 | + gql, |
| 4 | + TypedDocumentNode, |
| 5 | + useBackgroundQuery, |
| 6 | + useSuspenseQuery, |
| 7 | + QueryReference, |
| 8 | + useReadQuery, |
| 9 | +} from "@apollo/client"; |
| 10 | + |
| 11 | +interface Data { |
| 12 | + dog: { |
| 13 | + id: string; |
| 14 | + name: string; |
| 15 | + breed: string; |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +interface BreedData { |
| 20 | + breeds: { id: string; name: string; characteristics: string[] }[]; |
| 21 | +} |
| 22 | + |
| 23 | +interface Variables { |
| 24 | + id: string; |
| 25 | +} |
| 26 | + |
| 27 | +interface DogProps { |
| 28 | + id: string; |
| 29 | + isPending: boolean; |
| 30 | + queryRef: QueryReference<BreedData>; |
| 31 | + refetchHandler: () => void; |
| 32 | +} |
| 33 | + |
| 34 | +interface BreedsProps { |
| 35 | + isPending: boolean; |
| 36 | + queryRef: QueryReference<BreedData>; |
| 37 | +} |
| 38 | + |
| 39 | +export const GET_DOG_QUERY: TypedDocumentNode<Data, Variables> = gql` |
| 40 | + query GetDog($id: String) { |
| 41 | + dog(id: $id) { |
| 42 | + # By default, an object's cache key is a combination of its |
| 43 | + # __typename and id fields, so we should always make sure the |
| 44 | + # id is in the response so our data can be normalized and cached properly. |
| 45 | + id |
| 46 | + name |
| 47 | + breed |
| 48 | + } |
| 49 | + } |
| 50 | +`; |
| 51 | + |
| 52 | +export const GET_BREEDS_QUERY: TypedDocumentNode<BreedData> = gql` |
| 53 | + query GetBreeds { |
| 54 | + breeds { |
| 55 | + id |
| 56 | + name |
| 57 | + characteristics |
| 58 | + } |
| 59 | + } |
| 60 | +`; |
| 61 | + |
| 62 | +function App() { |
| 63 | + const [isPending, startTransition] = useTransition(); |
| 64 | + const [queryRef, { refetch }] = useBackgroundQuery(GET_BREEDS_QUERY); |
| 65 | + |
| 66 | + const refetchHandler = () => { |
| 67 | + startTransition(() => { |
| 68 | + refetch(); |
| 69 | + }); |
| 70 | + }; |
| 71 | + |
| 72 | + return ( |
| 73 | + <Suspense fallback={<div>Loading...</div>}> |
| 74 | + <Dog |
| 75 | + id="3" |
| 76 | + queryRef={queryRef} |
| 77 | + isPending={isPending} |
| 78 | + refetchHandler={refetchHandler} |
| 79 | + /> |
| 80 | + </Suspense> |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +function Dog({ id, queryRef, isPending, refetchHandler }: DogProps) { |
| 85 | + const { data } = useSuspenseQuery(GET_DOG_QUERY, { |
| 86 | + variables: { id }, |
| 87 | + }); |
| 88 | + |
| 89 | + return ( |
| 90 | + <> |
| 91 | + Name: {data.dog.name} |
| 92 | + <Suspense fallback={<div>Loading breeds...</div>}> |
| 93 | + <Breeds isPending={isPending} queryRef={queryRef} /> |
| 94 | + </Suspense> |
| 95 | + <button onClick={refetchHandler}>Refetch!</button> |
| 96 | + </> |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function Breeds({ queryRef, isPending }: BreedsProps) { |
| 101 | + const { data } = useReadQuery(queryRef); |
| 102 | + return data.breeds.map(({ characteristics }) => |
| 103 | + characteristics.map((characteristic) => ( |
| 104 | + <div style={{ opacity: `${isPending ? 0.5 : 1}` }} key={characteristic}> |
| 105 | + {characteristic} |
| 106 | + </div> |
| 107 | + )) |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +export default App; |
0 commit comments