mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 14:52:55 +08:00
Add comprehensive MDC files for various technologies and frameworks
Generate a large set of Markdown Configuration (MDC) files covering best practices across multiple domains, including: - AI/ML libraries (PyTorch, TensorFlow, scikit-learn) - Backend frameworks (Django, FastAPI, Express, Spring Boot) - Frontend frameworks (React, Vue, Angular) - Cloud platforms (AWS, Azure, GCP) - Development tools (Docker, Kubernetes, Terraform) - State management libraries - UI component libraries - Web technologies and APIs Enhance project documentation with detailed, standardized best practice guidelines for different technologies and frameworks
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
---
|
||||
description: This rule provides comprehensive best practices for using Apollo Client in your projects, covering code organization, performance, security, testing, and common pitfalls. It aims to guide developers in building robust and maintainable GraphQL-powered applications.
|
||||
globs: **/*.{js,jsx,ts,tsx,gql,graphql}
|
||||
---
|
||||
- **Code Organization and Structure:**
|
||||
- **Directory Structure:**
|
||||
- Organize GraphQL queries and mutations in a dedicated directory (e.g., `graphql` or `queries`).
|
||||
- Group related queries and mutations into subdirectories based on features or modules.
|
||||
- Consider a separate directory for GraphQL types/schemas if you're managing them locally.
|
||||
- Example:
|
||||
|
||||
src/
|
||||
graphql/
|
||||
user/
|
||||
getUser.gql
|
||||
createUser.gql
|
||||
product/
|
||||
getProduct.gql
|
||||
updateProduct.gql
|
||||
|
||||
- **File Naming Conventions:**
|
||||
- Use descriptive names for GraphQL query and mutation files (e.g., `getUser.gql`, `updateProduct.gql`).
|
||||
- Employ a consistent naming scheme (e.g., `[operationName].[operationType].gql`).
|
||||
- **Module Organization:**
|
||||
- Encapsulate Apollo Client logic (e.g., initialization, hooks) within reusable modules.
|
||||
- Create custom hooks for common data fetching patterns (e.g., `useUser`, `useProduct`).
|
||||
- **Component Architecture:**
|
||||
- Favor a component-based architecture for UI development.
|
||||
- Decouple data fetching logic from UI components using hooks or render props.
|
||||
- Utilize higher-order components (HOCs) or render props for cross-cutting concerns (e.g., authentication, error handling).
|
||||
- **Code Splitting:**
|
||||
- Use dynamic imports to lazy-load components and queries, improving initial load time.
|
||||
- Split large queries into smaller fragments to reduce bundle size.
|
||||
|
||||
- **Common Patterns and Anti-patterns:**
|
||||
- **Design Patterns:**
|
||||
- **Repository Pattern:** Abstract data access logic behind a repository interface.
|
||||
- **Facade Pattern:** Provide a simplified interface to a complex subsystem (e.g., Apollo Client).
|
||||
- **Hook Composition:** Combine multiple custom hooks to create more complex data fetching logic.
|
||||
- **Recommended Approaches:**
|
||||
- Use the `useQuery` and `useMutation` hooks for data fetching and manipulation.
|
||||
- Leverage Apollo Client's cache for improved performance and offline capabilities.
|
||||
- Implement optimistic updates to provide a better user experience.
|
||||
- Use GraphQL fragments to reuse common data structures across multiple queries and mutations.
|
||||
- **Anti-patterns:**
|
||||
- Avoid directly manipulating the Apollo Client cache outside of mutations.
|
||||
- Don't fetch the entire schema unnecessarily; only fetch what you need.
|
||||
- Avoid deeply nested components with complex data fetching logic.
|
||||
- **State Management:**
|
||||
- Use Apollo Client's cache as the primary source of truth for application state.
|
||||
- Integrate with external state management libraries (e.g., Redux, Zustand) when necessary for complex state requirements.
|
||||
- Consider using Apollo Link State for managing local client-side state.
|
||||
- **Error Handling:**
|
||||
- Implement global error handling with Apollo Link.
|
||||
- Display user-friendly error messages to the user.
|
||||
- Retry failed queries or mutations automatically.
|
||||
|
||||
- **Performance Considerations:**
|
||||
- **Optimization Techniques:**
|
||||
- Use the `fetchPolicy` option in `useQuery` to control cache behavior.
|
||||
- Implement pagination for large datasets.
|
||||
- Use batching and caching at the server-side to reduce network requests.
|
||||
- **Memory Management:**
|
||||
- Unsubscribe from subscriptions when components unmount to prevent memory leaks.
|
||||
- Avoid storing large amounts of data in the Apollo Client cache.
|
||||
- **Rendering Optimization:**
|
||||
- Use React's `memo` or `useMemo` to prevent unnecessary re-renders.
|
||||
- Implement virtualization for rendering large lists.
|
||||
- **Bundle Size Optimization:**
|
||||
- Use code splitting to reduce the initial bundle size.
|
||||
- Remove unused dependencies.
|
||||
- Minify and compress your code.
|
||||
- **Lazy Loading:**
|
||||
- Lazy-load components and queries using dynamic imports.
|
||||
|
||||
- **Security Best Practices:**
|
||||
- **Common Vulnerabilities:**
|
||||
- **GraphQL Injection:** Prevent injection attacks by validating user input and using parameterized queries.
|
||||
- **Denial of Service (DoS):** Limit query complexity and depth to prevent DoS attacks.
|
||||
- **Information Disclosure:** Avoid exposing sensitive data in error messages.
|
||||
- **Input Validation:**
|
||||
- Validate all user input on both the client and server sides.
|
||||
- Use GraphQL's built-in validation capabilities.
|
||||
- **Authentication and Authorization:**
|
||||
- Implement proper authentication and authorization mechanisms.
|
||||
- Use JWT (JSON Web Tokens) or other secure authentication protocols.
|
||||
- Follow the principle of least privilege when granting access to data.
|
||||
- **Data Protection:**
|
||||
- Encrypt sensitive data at rest and in transit.
|
||||
- Use HTTPS for all API communication.
|
||||
- **Secure API Communication:**
|
||||
- Use Apollo Link to add security headers to API requests.
|
||||
- Implement CORS (Cross-Origin Resource Sharing) to prevent cross-site scripting (XSS) attacks.
|
||||
|
||||
- **Testing Approaches:**
|
||||
- **Unit Testing:**
|
||||
- Test individual components and hooks in isolation.
|
||||
- Mock Apollo Client's API to control data fetching behavior.
|
||||
- **Integration Testing:**
|
||||
- Test the integration of components with Apollo Client.
|
||||
- Use a test GraphQL server to simulate real API interactions.
|
||||
- **End-to-end Testing:**
|
||||
- Test the entire application flow from the user interface to the backend.
|
||||
- Use tools like Cypress or Puppeteer for end-to-end testing.
|
||||
- **Test Organization:**
|
||||
- Organize tests in a directory structure that mirrors the source code.
|
||||
- Use descriptive names for test files and test cases.
|
||||
- **Mocking and Stubbing:**
|
||||
- Use mocking libraries (e.g., Jest, Sinon) to mock Apollo Client's API.
|
||||
- Create stub GraphQL responses for testing different scenarios.
|
||||
|
||||
- **Common Pitfalls and Gotchas:**
|
||||
- **Frequent Mistakes:**
|
||||
- Incorrect cache configuration leading to stale data.
|
||||
- Over-fetching data in GraphQL queries.
|
||||
- Ignoring error handling in `useQuery` and `useMutation`.
|
||||
- **Edge Cases:**
|
||||
- Handling network errors and offline scenarios.
|
||||
- Dealing with large datasets and complex queries.
|
||||
- Managing cache invalidation and updates.
|
||||
- **Version-Specific Issues:**
|
||||
- Be aware of breaking changes between Apollo Client versions.
|
||||
- Consult the Apollo Client documentation for migration guides.
|
||||
- **Compatibility Concerns:**
|
||||
- Ensure compatibility with different browsers and devices.
|
||||
- Test your application on different platforms.
|
||||
- **Debugging Strategies:**
|
||||
- Use Apollo Client DevTools to inspect queries, mutations, and cache state.
|
||||
- Log API requests and responses for debugging purposes.
|
||||
|
||||
- **Tooling and Environment:**
|
||||
- **Recommended Tools:**
|
||||
- Apollo Client DevTools for debugging and inspecting the cache.
|
||||
- GraphQL Playground or GraphiQL for exploring and testing GraphQL APIs.
|
||||
- Apollo VS Code extension for syntax highlighting and autocompletion.
|
||||
- **Build Configuration:**
|
||||
- Use a build tool like Webpack or Parcel to bundle your code.
|
||||
- Configure Babel to transpile your code to compatible JavaScript versions.
|
||||
- **Linting and Formatting:**
|
||||
- Use ESLint and Prettier to enforce code style and best practices.
|
||||
- Configure linting rules specific to Apollo Client and GraphQL.
|
||||
- **Deployment:**
|
||||
- Use a deployment platform like Netlify, Vercel, or AWS Amplify.
|
||||
- Configure environment variables for API endpoints and other sensitive data.
|
||||
- **CI/CD Integration:**
|
||||
- Use a CI/CD pipeline to automate testing and deployment.
|
||||
- Integrate linters, formatters, and test runners into the pipeline.
|
||||
|
||||
- **Specific Best Practices based on Exa Search Results:**
|
||||
- Ensure all queries for a specific type return an ID for normalization and caching (RedwoodJS Community).
|
||||
- Store queries in separate `.gql` files and use tools like `graphql-import` for better organization (Reddit).
|
||||
- Use Apollo Client's `watchQuery` method for real-time data updates (Medium).
|
||||
- Follow GraphQL query best practices for efficient API design (Apollo GraphQL).
|
||||
|
||||
- **Additional considerations**
|
||||
- When creating queries and mutations, follow best practices to get the most out of both GraphQL and Apollo tooling.
|
||||
|
||||
- **Example Directory Structure**
|
||||
|
||||
src/
|
||||
components/
|
||||
UserCard.tsx
|
||||
ProductDetails.tsx
|
||||
graphql/
|
||||
queries/
|
||||
getUser.gql
|
||||
getProduct.gql
|
||||
mutations/
|
||||
createUser.gql
|
||||
updateProduct.gql
|
||||
hooks/
|
||||
useUser.ts
|
||||
useProduct.ts
|
||||
utils/
|
||||
apolloClient.ts // Initialize Apollo Client here
|
||||
|
||||
|
||||
This is a comprehensive guide for using Apollo Client. Please adapt these guidelines to your specific project requirements and coding style.
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
description: This rule provides comprehensive best practices and coding standards for GraphQL development, covering code organization, performance, security, testing, and common pitfalls.
|
||||
globs: **/*.graphql
|
||||
---
|
||||
- **Naming Conventions**:
|
||||
- Use `camelCase` for field names, argument names, and directive names. This is a widely accepted convention that enhances readability and consistency.
|
||||
- Use the suffix `Input` when naming input types (e.g., `UserInput`). This clearly distinguishes input types from other types in your schema.
|
||||
- Avoid verb prefixes like `get` in field names (e.g., use `users` instead of `getUsers`). This maintains clarity and consistency in your schema.
|
||||
|
||||
- **Schema Design and Query Optimization**:
|
||||
- Design your schema to prevent over-fetching or under-fetching of data. Use fragments to request only the necessary data.
|
||||
- Use variables for parameters instead of hard-coded values. This enhances flexibility and maintainability, and allows for query caching.
|
||||
- Implement pagination for large datasets to avoid overwhelming the client and improve performance.
|
||||
- Use field aliases to rename fields in the response, which can be useful for backward compatibility or to simplify the client-side code.
|
||||
|
||||
- **Code Organization and Structure**:
|
||||
- **Directory Structure**: Organize your GraphQL schema files into a logical directory structure. Consider grouping related types and resolvers together (e.g., `schemas/user/`, `resolvers/user/`).
|
||||
- **File Naming Conventions**: Use descriptive names for schema files (e.g., `user.graphql`, `product.graphql`).
|
||||
- **Module Organization**: Break down your schema into smaller, reusable modules. Use schema stitching or federation to combine these modules into a single API.
|
||||
- **Component Architecture**: If using a GraphQL client library like Apollo Client or Relay, structure your components to efficiently manage GraphQL queries and data fetching.
|
||||
|
||||
- **Common Patterns and Anti-patterns**:
|
||||
- **Design Patterns**: Consider using patterns like the Facade pattern to simplify complex resolvers or the DataLoader pattern to batch and cache data fetching.
|
||||
- **Anti-patterns**: Avoid creating overly complex queries that fetch too much data in a single request. Also avoid using deeply nested resolvers, as this can lead to performance issues.
|
||||
- **State Management**: Choose a state management solution that integrates well with your GraphQL client library. Consider using Apollo Client's cache or Relay's store for client-side data management.
|
||||
- **Error Handling**: Implement robust error handling in your resolvers. Return user-friendly error messages and log detailed error information on the server.
|
||||
|
||||
- **Performance Considerations**:
|
||||
- **Optimization Techniques**: Use techniques like query batching, caching, and persisted queries to optimize performance.
|
||||
- **Memory Management**: Be mindful of memory usage in your resolvers, especially when dealing with large datasets.
|
||||
- **Lazy Loading**: Implement lazy loading for non-critical data to improve initial page load times.
|
||||
|
||||
- **Security Best Practices**:
|
||||
- **Input Validation**: Validate all user inputs to prevent injection attacks and other security vulnerabilities. Use appropriate data types and constraints in your schema.
|
||||
- **Authentication and Authorization**: Implement strong authentication and authorization mechanisms to protect your API. Use role-based access control (RBAC) to restrict access to sensitive data.
|
||||
- **Data Protection**: Protect sensitive data by encrypting it at rest and in transit. Use HTTPS to secure API communication.
|
||||
- **Rate Limiting**: Implement rate limiting to prevent denial-of-service (DoS) attacks.
|
||||
- **Query Complexity Analysis**: Limit the complexity of GraphQL queries to prevent malicious users from overloading the server. Tools like `graphql-cost-analysis` can help.
|
||||
|
||||
- **Testing Approaches**:
|
||||
- **Unit Testing**: Write unit tests for your resolvers to ensure they are functioning correctly.
|
||||
- **Integration Testing**: Write integration tests to verify that your GraphQL API integrates correctly with your data sources and other services.
|
||||
- **End-to-end Testing**: Write end-to-end tests to simulate user interactions with your API and verify that the entire system is working as expected.
|
||||
- **Test Organization**: Organize your tests into a logical directory structure. Use clear and descriptive names for your test files.
|
||||
- **Mocking and Stubbing**: Use mocking and stubbing to isolate your resolvers from external dependencies during testing.
|
||||
|
||||
- **Common Pitfalls and Gotchas**:
|
||||
- **N+1 Problem**: Be aware of the N+1 problem, where fetching a list of items requires N additional queries to fetch related data. Use DataLoader to batch and cache these queries.
|
||||
- **Circular Dependencies**: Avoid circular dependencies between your schema types, as this can lead to errors.
|
||||
|
||||
- **Tooling and Environment**:
|
||||
- **Recommended Development Tools**: Use tools like GraphQL Playground or GraphiQL for exploring and testing your API. Consider using a GraphQL IDE like Apollo Studio or Altair GraphQL Client for advanced features.
|
||||
- **Linting and Formatting**: Use a GraphQL linter and formatter to enforce code style and prevent errors. Consider using ESLint with the `eslint-plugin-graphql` plugin.
|
||||
- **Deployment Best Practices**: Deploy your GraphQL API behind a CDN to improve performance and availability. Use a GraphQL gateway to manage authentication, authorization, and rate limiting.
|
||||
- **CI/CD Integration**: Integrate your GraphQL API into your CI/CD pipeline to automate testing and deployment.
|
||||
Reference in New Issue
Block a user