Below is a complete guide to structuring a React-TypeScript-Apollo-Codegen app with Ant Design and a Hasura/Postgres backend. We’ll focus on one entity (Customer) end-to-end—once you see the pattern, you can repeat for employee, product, productstatus, producttype, sale, etc.
1) Project structure
code Copy code
my-app/
├─ codegen.yml
├─ package.json
├─ tsconfig.json
├─ .env
└─ src/
├─ App.tsx
├─ index.tsx
├─ graphql/ # Your raw .graphql documents
│ └─ customer/
│ ├─ GetCustomers.graphql
│ ├─ CreateCustomer.graphql
│ ├─ UpdateCustomer.graphql
│ └─ DeleteCustomer.graphql
├─ generated/ # Code-gen output
│ └─ graphql.tsx
├─ pages/
│ └─ CustomersPage.tsx
├─ components/
│ └─ Customer/
│ ├─ CustomerList.tsx
│ └─ CustomerForm.tsx
└─ apollo/
└─ client.ts # Apollo Client setup
2) Setup GraphQL Codegen
codegen.yml
yaml Copy code
schema:
- ${HASURA_GRAPHQL_URL} # e.g. https://my-hasura.herokuapp.com/v1/graphql
documents: "src/graphql/**/*.graphql"
generates:
src/generated/graphql.tsx:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
config:
withHooks: true
reactApolloVersion: 3
skipTypename: false
Add to package.json scripts:
json Copy code
{
"scripts": {
"codegen": "graphql-codegen --config codegen.yml"
}
}
Make sure .env has:
code Copy code
HASURA_GRAPHQL_URL=https://your-hasura-instance/v1/graphql
Run:
code Copy code
npm run codegen
3) Apollo Client
src/apollo/client.ts
ts Copy code
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';
const httpLink = new HttpLink({
uri: process.env.REACT_APP_HASURA_GRAPHQL_URL,
headers: {
// authorization: `Bearer ${token}` // if you have auth
},
});
export const apolloClient = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
});
Wrap your app in index.tsx:
tsx Copy code
// src/index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import { ApolloProvider } from '@apollo/client';
import { apolloClient } from './apollo/client';
import App from './App';
ReactDOM.render(
<ApolloProvider client={apolloClient}>
<App />
</ApolloProvider>,
document.getElementById('root')
);
4) GraphQL documents for Customer
Place these in src/graphql/customer/
GetCustomers.graphql
graphql Copy code
query GetCustomers {
customer {
id
name
email
phone
}
}
CreateCustomer.graphql
graphql Copy code
mutation CreateCustomer($name: String!, $email: String!, $phone: String!) {
insert_customer_one(object: { name: $name, email: $email, phone: $phone }) {
id
name
email
phone
}
}
UpdateCustomer.graphql
graphql Copy code
mutation UpdateCustomer($id: Int!, $name: String!, $email: String!, $phone: String!) {
update_customer_by_pk(pk_columns: { id: $id }, _set: { name: $name, email: $email, phone: $phone }) {
id
name
email
phone
}
}
DeleteCustomer.graphql
graphql Copy code
mutation DeleteCustomer($id: Int!) {
delete_customer_by_pk(id: $id) {
id
}
}
After npm run codegen, you’ll have typed hooks in src/generated/graphql.tsx:
useGetCustomersQuery
useCreateCustomerMutation
useUpdateCustomerMutation
useDeleteCustomerMutation
5) UI Components
5.1 CustomerList.tsx
tsx Copy code
// src/components/Customer/CustomerList.tsx
import React, { useState } from 'react';
import { Table, Button, Popconfirm, Spin } from 'antd';
import { ColumnsType } from 'antd/lib/table';
import {
GetCustomersQuery,
useGetCustomersQuery,
useDeleteCustomerMutation,
} from '../../generated/graphql';
import CustomerForm from './CustomerForm';
type Customer = NonNullable<GetCustomersQuery['customer']>[0];
const CustomerList: React.FC = () => {
const { data, loading, refetch } = useGetCustomersQuery();
const [deleteCustomer] = useDeleteCustomerMutation();
const [editingCustomer, setEditingCustomer] = useState<Customer | null>(null);
const [isModalVisible, setModalVisible] = useState(false);
if (loading) return <Spin />;
const handleDelete = async (id: number) => {
await deleteCustomer({ variables: { id } });
refetch();
};
const columns: ColumnsType<Customer> = [
{ title: 'Name', dataIndex: 'name' },
{ title: 'Email', dataIndex: 'email' },
{ title: 'Phone', dataIndex: 'phone' },
{
title: 'Actions',
render: (_, record) => (
<>
<Button type="link" onClick={() => { setEditingCustomer(record); setModalVisible(true); }}>
Edit
</Button>
<Popconfirm
title="Sure to delete?"
onConfirm={() => handleDelete(record.id)}
>
<Button type="link" danger>Delete</Button>
</Popconfirm>
</>
),
},
];
return (
<>
<Button
type="primary"
style={{ marginBottom: 16 }}
onClick={() => { setEditingCustomer(null); setModalVisible(true); }}
>
New Customer
</Button>
<Table<Customer>
rowKey="id"
dataSource={data?.customer || []}
columns={columns}
/>
<CustomerForm
visible={isModalVisible}
customer={editingCustomer}
onClose={() => setModalVisible(false)}
onSuccess={() => { refetch(); setModalVisible(false); }}
/>
</>
);
};
export default CustomerList;
5.2 CustomerForm.tsx
tsx Copy code
// src/components/Customer/CustomerForm.tsx
import React, { useEffect } from 'react';
import { Modal, Form, Input } from 'antd';
import {
Customer,
useCreateCustomerMutation,
useUpdateCustomerMutation,
} from '../../generated/graphql';
interface Props {
visible: boolean;
customer: Customer | null;
onClose: () => void;
onSuccess: () => void;
}
const CustomerForm: React.FC<Props> = ({
visible, customer, onClose, onSuccess,
}) => {
const [form] = Form.useForm();
const [createCustomer, { loading: creating }] = useCreateCustomerMutation();
const [updateCustomer, { loading: updating }] = useUpdateCustomerMutation();
useEffect(() => {
if (customer) {
form.setFieldsValue(customer);
} else {
form.resetFields();
}
}, [customer, form]);
const handleOk = async () => {
const values = await form.validateFields();
if (customer) {
await updateCustomer({
variables: { id: customer.id, ...values },
});
} else {
await createCustomer({ variables: values });
}
onSuccess();
};
return (
<Modal
title={customer ? 'Edit Customer' : 'New Customer'}
visible={visible}
onOk={handleOk}
onCancel={onClose}
confirmLoading={creating || updating}
>
<Form form={form} layout="vertical">
<Form.Item name="name" label="Name"
rules={[{ required: true, message: 'Please input name' }]}
>
<Input />
</Form.Item>
<Form.Item name="email" label="Email"
rules={[
{ required: true, message: 'Please input email' },
{ type: 'email', message: 'Invalid email' },
]}
>
<Input />
</Form.Item>
<Form.Item name="phone" label="Phone"
rules={[{ required: true, message: 'Please input phone' }]}
>
<Input />
</Form.Item>
</Form>
</Modal>
);
};
export default CustomerForm;
6) Page Layer
tsx Copy code
// src/pages/CustomersPage.tsx
import React from 'react';
import { Layout, Typography } from 'antd';
import CustomerList from '../components/Customer/CustomerList';
const { Header, Content } = Layout;
const { Title } = Typography;
const CustomersPage: React.FC = () => (
<Layout style={{ height: '100vh' }}>
<Header style={{ color: 'white' }}><Title style={{ color: 'white', margin: 0 }} level={3}>Customers</Title></Header>
<Content style={{ padding: 24 }}><CustomerList /></Content>
</Layout>
);
export default CustomersPage;
And in App.tsx :
tsx Copy code
// src/App.tsx
import React from 'react';
import CustomersPage from './pages/CustomersPage';
const App: React.FC = () => {
// You can add React-Router here for multiple pages
return <CustomersPage />;
};
export default App;
7) Repeat for Other Entities
For employee, product, productstatus, producttype, sale:
Create .graphql files under src/graphql/<entity>/…
Add queries & mutations: GetAll, GetByPK (if needed), Create, Update, Delete.
Run npm run codegen.
Copy the CustomerList + CustomerForm pattern → EmployeeList & EmployeeForm, etc.
Wire up the new pages in your router or App.tsx.
8) Tips & Best Practices
Single Source of Truth : Everything GraphQL lives in src/graphql/.
Auto-Generated Types : Don’t hand-write your schemas in TS—let codegen produce them.
Component Split :
List components handle loading, fetching, table.
Form components handle create/update logic.
Error & Loading States : Enhance with Ant Design notifications or spinners.
Pagination & Filtering : Expand your queries with limit/offset or Hasura’s where clauses.
Cache Updates : Use refetch() or update(cache, …) for real-time feedback.
Routing : Use React-Router for multiple pages (/customers, /products, …).
—
With this structure you’ll get a scalable , type-safe , well-organized CRUD front-end backed by Hasura’s GraphQL engine. Enjoy building! 🚀