-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathApp.tsx
More file actions
54 lines (45 loc) · 1.38 KB
/
App.tsx
File metadata and controls
54 lines (45 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { useQuery } from '@apollo/client';
import { DocumentType, FragmentType, graphql, isFragmentReady, useFragment } from './gql';
export const slowFieldFragment = graphql(/* GraphQL */ `
fragment SlowFieldFragment on Query {
slowField(waitFor: 5000)
}
`);
const alphabetQuery = graphql(/* GraphQL */ `
query SlowAndFastFieldWithDefer {
fastField
...SlowFieldFragment @defer
... @defer {
inlinedSlowField: slowField(waitFor: 5000)
}
}
`);
const SlowDataField = (props: { data: FragmentType<typeof slowFieldFragment> }) => {
const data = useFragment(slowFieldFragment, props.data);
return <p>{data.slowField}</p>;
};
const InlinedSlowDataField = (props: { data: DocumentType<typeof alphabetQuery> }) => {
try {
// @ts-expect-error - this field should be either undefined or a string
const _ = props.data.inlinedSlowField.toLowerCase();
} catch (e: any) {}
if (!props.data.inlinedSlowField) {
return null;
}
return <p>{props.data.inlinedSlowField} inlined fragment</p>;
};
function App() {
const { data } = useQuery(alphabetQuery);
return (
<div className="App">
{data && (
<>
<p>{data.fastField}</p>
{isFragmentReady(alphabetQuery, slowFieldFragment, data) && <SlowDataField data={data} />}
<InlinedSlowDataField data={data} />
</>
)}
</div>
);
}
export default App;