I have a react typescript Apollo graphql ant design app with a hasura backend. Give me a boilerplate for the required hooks to read, write, modify and delete operations, simple and more complex ones. I have the following tables in my postures database: customer, employee, product, product category, product status, product name, sale, payout, data transaction.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Of course! This is an excellent stack for building robust applications. Here is a comprehensive boilerplate and guide for creating React hooks for your Hasura-backed GraphQL API, integrated with TypeScript and Ant Design.
1. Prerequisites & Setup
First, let's ensure your project is set up correctly.
a. Apollo Client Setup
Your App.tsx or main.tsx should have the Apollo Client configured and wrapping your application.
typescript
// src/lib/apolloClient.ts
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';
const httpLink = new HttpLink({
uri: 'YOUR_HASURA_GRAPHQL_ENDPOINT', // e.g., https://your-project.hasura.app/v1/graphql
headers: {
'x-hasura-admin-secret': 'YOUR_HASURA_ADMIN_SECRET', // Or handle auth with JWTs
},
});
export const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
});
// src/main.tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import { ApolloProvider } from '@apollo/client';
import App from './App';
import { client } from './lib/apolloClient';
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<ApolloProvider client={client}>
<App />
</ApolloProvider>
</React.StrictMode>,
);
b. GraphQL Code Generator (Highly Recommended)
This is the most critical step for a good TypeScript experience. It will generate types automatically from your GraphQL schema and operations, giving you full type safety.
// src/graphql/hooks/useProducts.ts (add this to the existing file)
import { useMutation } from '@apollo/client';
import { CREATE_PRODUCT } from '../mutations/productMutations';
import { GET_PRODUCTS_PAGINATED } from '../queries/productQueries'; // To refetch
import { CreateProductMutation, CreateProductMutationVariables } from '../generated/graphql';
export const useCreateProduct = () => {
const [createProduct, { loading, error }] = useMutation<
CreateProductMutation,
CreateProductMutationVariables
>(CREATE_PRODUCT, {
// This tells Apollo to re-run this query after the mutation succeeds
// to update the UI with the new product.
refetchQueries: [{ query: GET_PRODUCTS_PAGINATED }],
awaitRefetchQueries: true,
});
return { createProduct, loading, error };
};
3. Use in a Component (Ant Design Form and Modal):
// src/graphql/hooks/useEmployees.ts
import { useMutation } from '@apollo/client';
import { UPDATE_EMPLOYEE } from '../mutations/employeeMutations';
import { UpdateEmployeeMutation, UpdateEmployeeMutationVariables } from '../generated/graphql';
export const useUpdateEmployee = () => {
const [updateEmployee, { loading, error }] = useMutation<
UpdateEmployeeMutation,
UpdateEmployeeMutationVariables
>(UPDATE_EMPLOYEE);
// No refetch here, as Apollo can often update the cache automatically
// if the mutation returns the object with its `id` and `__typename`.
return { updateEmployee, loading, error };
};
C. Delete Operation: Delete a Sale
1. Define the Mutation:
typescript
// src/graphql/mutations/saleMutations.ts
import { gql } from '../generated';
export const DELETE_SALE = gql(`
mutation DeleteSale($id: uuid!) {
delete_sale_by_pk(id: $id) {
id # Returning the ID is good practice
}
}
`);
2. Create the Custom Hook:
typescript
// src/graphql/hooks/useSales.ts
import { useMutation } from '@apollo/client';
import { DELETE_SALE } from '../mutations/saleMutations';
import { DeleteSaleMutation, DeleteSaleMutationVariables } from '../generated/graphql';
export const useDeleteSale = () => {
const [deleteSale, { loading, error }] = useMutation<
DeleteSaleMutation,
DeleteSaleMutationVariables
>(DELETE_SALE, {
// You MUST update the cache after a delete. Refetching is the easiest way.
// You can also use the `update` function for better performance.
refetchQueries: ['GetAllSales'], // Use query name string or the full query object
});
return { deleteSale, loading, error };
};
3. Use in a Component (with Ant Design Popconfirm):
This boilerplate provides a solid, scalable, and type-safe foundation for your React/Apollo/Hasura application. The key is to leverage GraphQL Code Generator and create reusable custom hooks for each primary data operation.