Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+428 -45
View File
@@ -1,55 +1,438 @@
---
description: This rule provides comprehensive best practices and coding standards for GraphQL development, covering code organization, performance, security, testing, and common pitfalls.
globs: *.graphql
description: Enforce modern, performant, and secure GraphQL schema design and operation best practices for maintainable and scalable APIs.
globs: **/*.{graphql,gql}
---
- **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.
# GraphQL Best Practices
- **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.
This guide outlines the definitive best practices for designing, implementing, and operating GraphQL APIs. Adhere to these rules to ensure your schemas are consistent, performant, secure, and developer-friendly.
- **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.
## 1. Schema Design & Naming Conventions
- **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.
Consistency is paramount. Follow standard GraphQL naming conventions strictly.
- **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.
### 1.1 Casing Rules
- **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.
* **Types (Object, Input, Enum, Interface, Union, Scalar)**: `PascalCase`
* **Fields, Arguments, Directives**: `camelCase`
* **Enum Values**: `SCREAMING_SNAKE_CASE`
- **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.
❌ **BAD**
```graphql
type user { # Type name not PascalCase
firstName: String!
getProducts(productId: ID): [product] # Field name has verb, arg not camelCase
}
- **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.
enum UserRole { # Enum value not SCREAMING_SNAKE_CASE
admin
guest
}
```
- **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.
✅ **GOOD**
```graphql
type User {
id: ID!
firstName: String!
products(limit: Int): [Product!]! # No verb, arg is camelCase
role: UserRole!
}
enum UserRole {
ADMIN
GUEST
}
```
### 1.2 Query Field Naming
Avoid verb prefixes (`get`, `list`, `fetch`) on query (read) fields. This maintains consistency with nested fields.
❌ **BAD**
```graphql
type Query {
getUsers: [User!]!
listProducts: [Product!]!
}
query MyQuery {
getUsers {
id
getProducts { # Inconsistent with root field
name
}
}
}
```
✅ **GOOD**
```graphql
type Query {
users: [User!]!
products: [Product!]!
}
query MyQuery {
users {
id
products { # Consistent with root field
name
}
}
}
```
### 1.3 Mutation Field Naming
Start mutation fields with an imperative verb (e.g., `create`, `update`, `delete`).
❌ **BAD**
```graphql
type Mutation {
userCreate(input: CreateUserInput!): CreateUserPayload!
}
```
✅ **GOOD**
```graphql
type Mutation {
createUser(input: CreateUserInput!): CreateUserPayload!
updateUser(input: UpdateUserInput!): UpdateUserPayload!
deleteUser(input: DeleteUserInput!): DeleteUserPayload!
}
```
### 1.4 Input & Payload Types
* **Input Types**: Suffix with `Input`.
* **Mutation Payload Types**: Suffix with `Payload` or `Response`. These should typically include the created/updated object and a `success` boolean or `errors` array.
❌ **BAD**
```graphql
input UserData { # Generic name
name: String!
}
type Mutation {
createUser(data: UserData!): User! # Returns raw object, no success/errors
}
```
✅ **GOOD**
```graphql
input CreateUserInput {
name: String!
email: String!
}
type CreateUserPayload {
success: Boolean!
user: User
errors: [Error!] # Custom error type for detailed feedback
}
type Mutation {
createUser(input: CreateUserInput!): CreateUserPayload!
}
```
## 2. Global Object Identification & Pagination
Implement Relay-style global object identification and cursor-based pagination for robust caching and efficient data fetching.
### 2.1 Global Object ID
Every object that can be refetched or cached independently MUST implement the `Node` interface and expose a globally unique `id: ID!`.
```graphql
interface Node {
id: ID!
}
type User implements Node {
id: ID! # Global ID
username: String!
email: String!
}
type Query {
node(id: ID!): Node
user(id: ID!): User # Specific fetch for convenience
}
```
### 2.2 Cursor-Based Pagination
Always use cursor-based pagination (Relay Connection Specification) for lists that can grow large. Avoid offset-based pagination.
❌ **BAD**
```graphql
type Query {
users(offset: Int = 0, limit: Int = 10): [User!]! # Offset-based pagination
}
```
✅ **GOOD**
```graphql
type Query {
users(first: Int, after: String, last: Int, before: String): UserConnection!
}
type UserConnection {
edges: [UserEdge!]!
pageInfo: PageInfo!
}
type UserEdge {
node: User!
cursor: String!
}
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: String
endCursor: String
}
```
## 3. Nullability & Input Validation
Be explicit about nullability and always validate inputs server-side.
### 3.1 Explicit Nullability
Use `!` for non-nullable fields when the data is always expected. Default to nullable if the field might legitimately be null.
❌ **BAD**
```graphql
type User {
name: String # Is name always present?
email: String!
}
```
✅ **GOOD**
```graphql
type User {
id: ID!
name: String! # Name is always required
email: String # Email might be null if user hasn't provided it
}
```
### 3.2 Server-Side Input Validation
GraphQL's type system provides basic validation, but complex business logic validation MUST occur in resolvers. Return specific errors in the `errors` array.
```graphql
input CreateUserInput {
name: String! # GraphQL ensures it's a string and non-null
email: String!
}
type CreateUserPayload {
success: Boolean!
user: User
errors: [Error!] # Custom error type for detailed feedback
}
type Error {
message: String!
code: String! # e.g., "INVALID_EMAIL", "NAME_TOO_SHORT"
path: [String!] # Field path where error occurred
}
```
## 4. Performance Considerations
Prevent N+1 query problems and optimize data fetching.
### 4.1 DataLoader for N+1 Problems
Always use `DataLoader` (or equivalent batching/caching mechanism) within your resolvers to prevent N+1 query issues when fetching related data. This is a server-side implementation detail but critical for GraphQL API performance.
❌ **BAD** (Conceptual - direct resolver calls)
```javascript
// In a resolver for User.products
user.productIds.map(productId => db.products.findById(productId)); // N+1 query
```
✅ **GOOD** (Conceptual - using DataLoader)
```javascript
// In a resolver for User.products
dataLoaders.productLoader.loadMany(user.productIds); // Batches and caches
```
## 5. Security
Implement robust security measures to protect your API from common attack vectors.
### 5.1 Query Depth and Complexity Limiting
Configure your GraphQL server to limit the maximum depth and complexity of incoming queries. This prevents malicious or accidental denial-of-service (DoS) attacks.
```graphql
# This is a server-side configuration, not schema.
# Example: Max depth 10, max complexity 1000.
# If exceeded, the server rejects the query before execution.
```
### 5.2 Rate Limiting
Implement rate limiting at the HTTP layer to prevent abuse and DoS attacks. This should be applied to all API endpoints, including your GraphQL endpoint.
## 6. Error Handling
Provide consistent and informative error responses.
### 6.1 Standardized Error Responses
Return errors in the `errors` array of the GraphQL response. Use the `extensions` field for custom error codes, details, and stack traces (in development environments only).
❌ **BAD**
```json
{
"data": null,
"httpStatus": 400,
"customError": {
"code": "INVALID_INPUT",
"details": "Email is malformed"
}
}
```
✅ **GOOD**
```json
{
"data": null,
"errors": [
{
"message": "Invalid input for field 'email'",
"locations": [{ "line": 2, "column": 3 }],
"path": ["createUser", "input", "email"],
"extensions": {
"code": "BAD_USER_INPUT",
"validationErrors": [
{
"field": "email",
"message": "Email address is not valid."
}
]
}
}
]
}
```
## 7. Request/Response Patterns (HTTP)
Adhere to the "GraphQL Over HTTP" specification.
### 7.1 HTTP Method and Content-Type
Always use `POST` requests for GraphQL queries and mutations. The `Content-Type` header MUST be `application/json`.
❌ **BAD**
```
GET /graphql?query={users{id}}
```
✅ **GOOD**
```
POST /graphql
Content-Type: application/json
{
"query": "query { users { id name } }",
"variables": {},
"operationName": null
}
```
### 7.2 HTTP Status Codes
A GraphQL server MUST respond with `200 OK` for all well-formed GraphQL requests, even if the GraphQL response contains errors in the `errors` array. Only use `4xx` or `5xx` status codes for HTTP-level errors (e.g., malformed request, authentication failure *before* GraphQL execution, server unavailable).
❌ **BAD**
```json
// HTTP Status: 400 Bad Request
{
"errors": [{ "message": "User not found" }]
}
```
✅ **GOOD**
```json
// HTTP Status: 200 OK
{
"data": { "user": null },
"errors": [{ "message": "User not found" }]
}
```
## 8. Testing Approaches
Implement a comprehensive testing strategy covering schema, resolvers, and operations.
### 8.1 Schema Stability
Use snapshot testing for your GraphQL schema to catch unintended or breaking changes early in the development cycle. Integrate schema diff tools into your CI/CD pipeline.
```javascript
// Example using Jest and jest-serializer-graphql-schema
import { buildSchema } from 'graphql';
import { lexicographicSortSchema } from 'graphql/utilities';
const schema = buildSchema(`
type User {
id: ID!
name: String!
}
type Query {
users: [User!]!
}
`);
test('schema should not change unexpectedly', () => {
expect(lexicographicSortSchema(schema)).toMatchSnapshot();
});
```
### 8.2 Resolver Unit Tests
Unit test individual resolvers, especially those containing complex business logic, authorization checks, or data transformations. Mock external dependencies.
### 8.3 Operation Integration Tests
Write integration tests that execute full GraphQL operations (queries, mutations) against your server. These tests validate the end-to-end flow, including resolver composition and data fetching.
```javascript
// Example using Apollo Server's test utility
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
const typeDefs = `...`; // Your schema
const resolvers = { ... }; // Your resolvers
const server = new ApolloServer({ typeDefs, resolvers });
test('fetches users correctly', async () => {
const { url } = await startStandaloneServer(server, { listen: { port: 0 } });
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `query { users { id name } }`,
}),
});
const { data } = await response.json();
expect(data.users).toEqual(expect.arrayContaining([
{ id: '1', name: 'Alice' },
]));
});
```
### 8.4 Schema Linting
Use schema linting tools (e.g., `graphql-schema-linter`, `@graphql-eslint/eslint-plugin`) in your CI/CD to enforce naming conventions, descriptions, and deprecation patterns.