DomainScoreCursor Rules
GitHub

1/23/2025

Best practices and guidelines for using GraphQL with Apollo Client in a React application.



# GraphQL (Apollo Client)

# GraphQL (Apollo Client) Best Practices

## Project Background
This project utilizes GraphQL with Apollo Client for state management and data fetching in a React application. The goal is to ensure efficient data handling, maintainable code, and a robust development workflow.

## Coding Standards
- **Functional Components**: Prefer functional components with hooks over class components.
- **Type Safety**: Use TypeScript for type safety with GraphQL operations.
- **Naming Conventions**: Follow consistent naming conventions for queries, mutations, and fragments.

## Preferred Libraries
- **Apollo Client**: For state management and data fetching.
- **TypeScript**: For type safety and better developer experience.

## File Structure
```
src/
  components/
  graphql/
    queries/
    mutations/
    fragments/
  hooks/
  pages/
  utils/
```

## Performance Optimization
- **Caching**: Utilize Apollo Client's caching capabilities to minimize redundant data fetching.
- **Lazy Loading**: Implement lazy loading for routing components to improve initial load performance.
- **Error Boundaries**: Implement proper error boundaries to handle GraphQL errors gracefully.

## Testing Requirements
- **Unit Tests**: Write unit tests using Jest and React Testing Library.
- **Test Coverage**: Ensure test coverage reaches at least 80%.
- **Snapshot Testing**: Use snapshot testing for UI components to detect unintended changes.

## Documentation
- **JSDoc**: Write comments for functions and components in JSDoc format.
- **README**: Each main directory must contain a README.md file with both English and Chinese versions.

## Error Handling
- **Try/Catch**: Use try/catch blocks to handle asynchronous operations.
- **Global Error Boundary**: Implement a global error boundary component to catch runtime errors.

## Additional Instructions
1. **Apollo Provider**: Use Apollo Provider at the root of your app.
2. **Custom Hooks**: Implement custom hooks for Apollo operations to encapsulate logic.
3. **DevTools**: Use Apollo Client DevTools for debugging and monitoring.
4. **Fragments**: Utilize fragments for reusable query parts to keep queries DRY.
5. **Mutations**: Implement mutations for data modifications and ensure proper error handling.
6. **Loading States**: Implement proper loading states to enhance user experience.
7. **Error Handling**: Ensure proper error handling for GraphQL operations to provide meaningful feedback to users.