storybook/docs/snippets/react/document-screen-with-graphql.ts.mdx
Fardeen Panjwani 018eca8f69
docs(snippets/document-screen): fix typo
Replace `useQueryuseQuery<Data>(...)` with `useQuery<Data>(...)`
2023-06-29 21:04:18 -04:00

82 lines
1.4 KiB
Plaintext

```ts
// YourPage.ts|tsx
import { useQuery, gql } from '@apollo/client';
import { PageLayout } from './PageLayout';
import { DocumentHeader } from './DocumentHeader';
import { DocumentList } from './DocumentList';
const AllInfoQuery = gql`
query AllInfo {
user {
userID
name
}
document {
id
userID
title
brief
status
}
subdocuments {
id
userID
title
content
status
}
}
`;
interface Data {
AllInfo: {
user: {
userID: number;
name: string;
opening_crawl: boolean;
};
document: {
id: number;
userID: number;
title: string;
brief: string;
status: string;
};
subdocuments: {
id: number;
userID: number;
title: string;
content: string;
status: string;
};
};
}
function useFetchInfo() {
const { loading, error, data } = useQuery<Data>(AllInfoQuery);
return { loading, error, data };
}
export function DocumentScreen() {
const { loading, error, data } = useFetchInfo();
if (loading) {
return <p>Loading...</p>;
}
if (error) {
return <p>There was an error fetching the data!</p>;
}
return (
<PageLayout user={data.user}>
<DocumentHeader document={data.document} />
<DocumentList documents={data.subdocuments} />
</PageLayout>
);
}
```