diff --git a/.cursor/rules/@graph-client.mdc b/.cursor/rules/@graph-client.mdc new file mode 100644 index 00000000..8181b505 --- /dev/null +++ b/.cursor/rules/@graph-client.mdc @@ -0,0 +1,406 @@ +--- +description: +globs: +alwaysApply: true +--- +# The Graph Client Tools + +@context { + "type": "project_documentation", + "project": "thegraph-client-monorepo", + "purpose": "GraphQL querying tools for The Graph's decentralized network", + "version": "3.0.7", + "license": "MIT", + "repository": "https://github.com/graphprotocol/graph-client", + "contributors": ["Dotan Simha", "Arda Tanrikulu"], + "keywords": ["thegraph", "web3", "graphql", "client", "tools"], + "environment": ["browser", "nodejs"], + "architecture": "monorepo" +} + +## Project Overview + +The Graph Client is a comprehensive monorepo providing consumer-side tools for querying The Graph's decentralized GraphQL network. It enables developers to build distributed applications (dApps) with advanced data consumption capabilities, supporting both browser and Node.js environments. + +@features { + "core_capabilities": [ + "Multiple indexer support with configurable fetch strategies", + "Client-side composition of multiple subgraphs", + "Build-time validation and optimization", + "Cross-chain subgraph handling", + "Automatic pagination beyond 1000 record limits", + "Automatic block tracking for distributed consistency", + "Real-time updates via @live directive", + "Integration with Apollo Client and Urql", + "TypeScript support with built-in GraphQL Codegen" + ], + "execution_modes": [ + "Standalone mode with direct execution", + "Integration with existing GraphQL clients" + ] +} + +## Architecture + +@architecture { + "execution_flows": { + "standalone": "Browser/Node → Graph-Client → Orchestrator/Query Planner → Multiple Subgraphs", + "integrated": "Any GraphQL Client → Compatibility Layer → Graph-Client → Orchestrator → Subgraphs" + }, + "key_components": { + "client_side_composition": "Uses GraphQL-Mesh for schema composition and execution planning", + "multiple_indexer_support": "Parallel querying with configurable fetch strategies", + "build_time_optimization": "Generates optimized runtime artifacts", + "cross_chain_support": "Handles similar subgraphs across different chains as unified sources" + }, + "fetch_strategies": { + "race": "Returns first successful response", + "fallback": "Tries sources sequentially on failure", + "retry": "Retries failed requests with exponential backoff", + "highestValue": "Returns response with highest _meta.block_number", + "timeout": "Configurable timeouts per source" + } +} + +## Package Structure + +@packages { + "core": { + "@graphprotocol/client-cli": { + "purpose": "Main CLI tool for build-time operations and artifact generation", + "bin": "graphclient", + "main_commands": ["build", "serve-dev"], + "dependencies": ["@graphql-mesh/cli", "@graphql-mesh/graphql"] + }, + "@graphprotocol/client-auto-pagination": { + "purpose": "Automatic unlimited pagination beyond 1000 record limits", + "implementation": "Breaks large queries into concurrent requests using first: and after: filters" + }, + "@graphprotocol/client-block-tracking": { + "purpose": "Automatic block number tracking for distributed systems", + "implementation": "Uses number_gte filter and _meta fields" + }, + "@graphprotocol/client-auto-type-merging": { + "purpose": "Automatic GraphQL type merging for composition", + "use_case": "Client-side schema composition" + }, + "@graphprotocol/client-add-source-name": { + "purpose": "Adds source identification to query results", + "benefit": "Track which indexer provided specific data" + }, + "@graphprotocol/client-polling-live": { + "purpose": "Implements @live directive for real-time queries", + "mechanism": "Polling-based subscription simulation" + } + }, + "integrations": { + "apollo": "Apollo Client integration layer", + "urql": "Urql integration layer" + } +} + +## CLI Tool + +@cli_tool { + "installation": { + "yarn": "yarn add -D @graphprotocol/client-cli", + "npm": "npm install --save-dev @graphprotocol/client-cli" + }, + "commands": { + "graphclient build": "Generate optimized runtime artifacts", + "graphclient serve-dev": "Start built-in GraphiQL development server at http://localhost:4000", + "graphclient --fileType js": "Generate JavaScript instead of TypeScript", + "graphclient --fileType json": "Generate JSON artifacts with CommonJS" + }, + "output": { + "directory": ".graphclient/", + "files": ["index.ts/js", "schema.graphql", "documents.ts/js", "types.ts"] + } +} + +## Configuration + +@configuration { + "file": ".graphclientrc.yml", + "required_sections": ["sources"], + "optional_sections": ["transforms", "documents", "plugins", "codegen"], + "format": "YAML" +} + +### Basic Configuration + +@config_basic { + "template": { + "sources": [ + { + "name": "subgraph_name", + "handler": { + "graphql": { + "endpoint": "https://api.thegraph.com/subgraphs/name/..." + } + } + } + ] + } +} + +```yaml +sources: + - name: uniswapv2 + handler: + graphql: + endpoint: https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2 +``` + +### Advanced Configuration + +@config_advanced { + "features": { + "operation_headers": "Custom headers including authentication", + "transforms": "Auto-pagination, block tracking, type merging", + "documents": "GraphQL query files to include", + "plugins": "Live queries and other extensions", + "environment_variables": "Runtime interpolation support" + } +} + +```yaml +sources: + - name: uniswap + handler: + graphql: + endpoint: https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2 + operationHeaders: + Authorization: Bearer {context.config.apiToken} + transforms: + - autoPagination: + validateSchema: true + limitOfRecords: 1000 + - blockTracking: + validateSchema: true + - autoTypeMerging: true + +documents: + - ./queries/*.graphql + +plugins: + - pollingLive: + defaultInterval: 1000 +``` + +## Feature Implementation + +@features_implementation { + "auto_pagination": { + "purpose": "Bypass graph-node's 1000 record limit", + "mechanism": "Breaks large queries into concurrent requests", + "filters_used": ["first:", "after:"], + "configuration": { + "validateSchema": "Validates schema contains required input filters", + "limitOfRecords": "Default 1000, configurable per indexer" + } + }, + "block_tracking": { + "purpose": "Ensures consistency across distributed indexers", + "mechanism": "Uses number_gte filter and _meta fields", + "use_case": "Critical for polling updated data across multiple indexers", + "documentation_reference": "https://thegraph.com/docs/en/developer/distributed-systems/#polling-for-updated-data" + }, + "live_queries": { + "directive": "@live", + "mechanism": "Polling-based real-time updates", + "syntax": ["@live", "@live(interval: 5000)"], + "compatibility": "Works with any GraphQL client supporting AsyncIterable" + }, + "client_side_composition": { + "purpose": "Combine multiple subgraphs into unified schema", + "implementation": "GraphQL-Mesh based composition", + "benefits": ["Cross-subgraph queries", "Type merging", "Schema stitching"] + } +} + +## Implementation Examples + +@examples { + "standalone_execution": { + "import": "import { execute } from '../.graphclient'", + "usage": "const result = await execute(myQuery, variables)", + "context": "Generated .graphclient directory provides execute function" + }, + "with_context": { + "usage": "execute(myQuery, variables, { config: { apiToken: 'TOKEN' } })", + "purpose": "Runtime configuration and authentication" + }, + "live_queries": { + "basic": "query ExampleQuery @live { ... }", + "with_interval": "query ExampleQuery @live(interval: 5000) { ... }" + } +} + +### Standalone Execution + +```typescript +import { execute } from '../.graphclient' + +const myQuery = gql` + query pairs { + pair(id: "0x00004ee988665cdda9a1080d5792cecd16dc1220") { + id + token0 { id symbol name } + token1 { id symbol name } + } + } +` + +const result = await execute(myQuery, {}) +``` + +### Live Queries + +```graphql +query ExampleQuery @live { + transactions(first: 2, orderBy: timestamp, orderDirection: desc) { + id + blockNumber + timestamp + } +} + +query WithInterval @live(interval: 5000) { + pairs { id } +} +``` + +### Cross-Chain Configuration + +```yaml +sources: + - name: ethereum-uniswap + handler: + graphql: + endpoint: https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2 + - name: polygon-uniswap + handler: + graphql: + endpoint: https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2-polygon +``` + +## Development Workflow + +@workflow { + "steps": [ + { + "step": 1, + "action": "Install CLI", + "command": "yarn add -D @graphprotocol/client-cli" + }, + { + "step": 2, + "action": "Create configuration", + "file": ".graphclientrc.yml", + "content": "sources and transforms" + }, + { + "step": 3, + "action": "Build artifacts", + "command": "graphclient build", + "output": ".graphclient/ directory" + }, + { + "step": 4, + "action": "Development server", + "command": "graphclient serve-dev", + "url": "http://localhost:4000" + }, + { + "step": 5, + "action": "Import and use", + "code": "import { execute } from './.graphclient'" + } + ] +} + +## Error Handling & Performance + +@resilience { + "error_handling": { + "timeout_handling": "Configurable timeouts per source", + "retry_logic": "Automatic retry with exponential backoff", + "fallback_sources": "Automatic failover between indexers", + "validation": "Build-time schema validation" + }, + "performance": { + "build_time_generation": "Pre-compiled execution plans", + "concurrent_requests": "Parallel querying across sources", + "response_caching": "Client-side caching integration", + "bundle_optimization": "Tree-shaking friendly artifacts" + } +} + +## Common Use Cases + +@use_cases [ + { + "id": "multi_indexer_resilience", + "description": "Query multiple indexers for high availability", + "implementation": "Fallback and race strategies" + }, + { + "id": "cross_chain_dapps", + "description": "Aggregate data from multiple blockchain networks", + "implementation": "Multiple sources with unified schema" + }, + { + "id": "real_time_updates", + "description": "Live data feeds with @live queries", + "implementation": "Polling-based subscriptions" + }, + { + "id": "large_data_sets", + "description": "Automatic pagination for complete data retrieval", + "implementation": "Auto-pagination transform" + }, + { + "id": "complex_compositions", + "description": "Multi-subgraph queries with type merging", + "implementation": "Client-side composition" + }, + { + "id": "development_testing", + "description": "Built-in GraphiQL for query development", + "implementation": "serve-dev command" + } +] + +## Troubleshooting + +@troubleshooting [ + { + "id": "build_issues", + "symptoms": "Build fails or generates incorrect artifacts", + "solutions": ["Check .graphclientrc.yml syntax", "Verify endpoint availability", "Validate schema compatibility"] + }, + { + "id": "type_errors", + "symptoms": "TypeScript compilation errors", + "solutions": ["Ensure TypeScript configuration matches generated types", "Regenerate artifacts after config changes"] + }, + { + "id": "runtime_errors", + "symptoms": "Query execution fails", + "solutions": ["Verify network connectivity", "Check API keys and authentication", "Validate query syntax"] + }, + { + "id": "performance_issues", + "symptoms": "Slow query execution", + "solutions": ["Use appropriate fetch strategies", "Enable auto-pagination for large datasets", "Optimize query structure"] + }, + { + "id": "debugging", + "symptoms": "Need to test queries and explore schema", + "solutions": ["Use serve-dev mode", "Inspect generated schema.graphql", "Check network requests in browser dev tools"] + } +] + +@version "3.0.7" +@last_updated "2024-12-19" diff --git a/.cursor/rules/mdc.mdc b/.cursor/rules/mdc.mdc new file mode 100644 index 00000000..9b3c5bf5 --- /dev/null +++ b/.cursor/rules/mdc.mdc @@ -0,0 +1,355 @@ +--- +description: +globs: +alwaysApply: true +--- +--- +description: Guidelines and best practices for creating .mdc (Markdown Configuration) files in Cursor, including structure, metadata annotations, and formatting rules +globs: ["**/*.mdc"] +--- + +# Cursor MDC File Guidelines + +@context { + "type": "documentation", + "purpose": "cursor_rules", + "format_version": "1.0.0", + "supported_content_types": [ + "guidelines", + "api_docs", + "examples", + "implementations" + ] +} + +@structure { + "required_sections": [ + "frontmatter", + "title", + "context", + "content_sections" + ], + "optional_sections": [ + "version", + "last_updated", + "examples", + "implementations", + "related_files" + ], + "recommended_sections": [ + "practical_examples", + "common_patterns", + "type_definitions" + ] +} + +## File Structure + +### 1. Frontmatter + +@frontmatter_rules [ + { + "id": "position", + "rule": "Must be at the very top of the file", + "severity": "error" + }, + { + "id": "description", + "rule": "Single sentence, clear purpose", + "severity": "error" + }, + { + "id": "globs", + "rule": "Array of relevant file patterns", + "severity": "error" + }, + { + "id": "related_docs", + "rule": "Optional array of related documentation files - MUST only reference existing files", + "severity": "error" + }, + { + "id": "file_validation", + "rule": "All referenced files must exist in the workspace", + "severity": "error" + } +] + +Example frontmatter: +```yaml +--- +description: Guidelines for implementing feature X +globs: ["**/*.{ts,tsx}"] +related_docs: ["docs/architecture/feature-x.md"] +--- +``` + +### 2. Metadata Annotations + +@annotations { + "syntax": "@annotation_name JSON_content", + "placement": "Before relevant sections", + "format": "Valid JSON with proper indentation", + "types": { + "context": "Project and document context", + "rules": "List of rules or requirements", + "format": "Format specifications", + "options": "Available options", + "examples": "Implementation examples", + "implementations": "Full implementation details", + "related": "Related documentation or code" + } +} + +### 3. Content Structure + +@content_rules { + "headings": { + "h1": "Single main title", + "h2": "Major sections", + "h3": "Subsections", + "h4": "Detailed points" + }, + "code_blocks": { + "syntax": "Always specify language", + "examples": "Include practical examples", + "formatting": "Use proper indentation", + "context": "Add explanatory comments" + }, + "implementation_blocks": { + "structure": "Group related implementations", + "documentation": "Include inline documentation", + "types": "Specify type information", + "validation": "Include validation rules" + } +} + +## Best Practices + +@best_practices { + "organization": { + "sections": "Use clear hierarchical structure", + "annotations": "Place before relevant content", + "examples": "Include practical examples" + }, + "formatting": { + "json": "Properly formatted, valid JSON", + "markdown": "Clean, consistent spacing", + "code": "Language-specific syntax highlighting" + }, + "metadata": { + "annotations": "Use semantic names", + "context": "Provide clear scope", + "versioning": "Include version information" + }, + "implementation": { + "examples": "Provide complete, working examples", + "types": "Include type definitions", + "validation": "Specify validation rules", + "error_handling": "Document error cases" + } +} + +## Implementation Guidelines + +@implementation_rules { + "code_examples": { + "completeness": "Must be fully functional", + "types": "Include all necessary type information", + "imports": "Show all required imports", + "context": "Provide setup and usage context" + }, + "documentation": { + "inline": "Add explanatory comments", + "types": "Document type constraints", + "errors": "Document error conditions", + "usage": "Show usage patterns" + } +} + +## Example Structure + +### Basic Example +```markdown +--- +description: Example implementation of feature X +globs: ["src/features/**/*.ts"] +--- + +# Feature X Implementation + +@context { + "type": "implementation", + "feature": "X", + "version": "1.0.0" +} + +## Overview +[Feature description] + +## Implementation + +@implementation { + "language": "typescript", + "dependencies": ["dep1", "dep2"], + "types": { + "TypeX": "Description of TypeX" + } +} + +```typescript +// Implementation code with types and validation +``` + +## Usage Examples +[Usage examples with code] +``` + +### Full Implementation Example +See the `examples` section in related .mdc files for complete implementation examples. + +## Common Patterns + +@patterns { + "rules_section": { + "format": "array of objects", + "properties": ["id", "severity", "description"], + "example": [ + { + "id": "rule_name", + "severity": "error", + "description": "Clear description" + } + ] + }, + "implementation_section": { + "format": "object with implementation details", + "required": [ + "language", + "types", + "validation" + ], + "example": { + "language": "typescript", + "types": { + "Type1": "Description" + }, + "validation": { + "rule1": "Description" + } + } + } +} + +## Examples + +### Minimal Valid MDC File +```markdown +--- +description: A simple example MDC file +globs: ["**/*.example"] +--- + +# Example Rules + +@context { + "type": "example", + "version": "1.0.0" +} + +## Section One + +@rules [ + { + "id": "rule_one", + "severity": "error", + "description": "Description of rule one" + } +] +``` + +### Common Section Examples + +#### Rules Section +```markdown +@rules [ + { + "id": "unique_identifier", + "severity": "error|warning|info", + "description": "Clear description of the rule" + } +] +``` + +#### Options Section +```markdown +@options { + "option_name": "What this option does", + "another_option": "Description of another option" +} +``` + +#### Format Section +```markdown +@format { + "base": "Template with {placeholders}", + "options": ["array", "of", "options"], + "paths": { + "key": "value" + } +} +``` + +### Common Mistakes to Avoid + +@mistakes [ + { + "id": "missing_frontmatter", + "wrong": "Starting directly with content", + "correct": "Include frontmatter at top", + "reason": "Frontmatter is required for Cursor to properly parse the file" + }, + { + "id": "invalid_json", + "wrong": "Malformed JSON in annotations", + "correct": "Properly formatted JSON with quotes around keys", + "reason": "Annotations must contain valid JSON for proper parsing" + }, + { + "id": "inconsistent_structure", + "wrong": "Mixed levels of headings", + "correct": "Clear hierarchical structure", + "reason": "Consistent structure helps with readability and parsing" + }, + { + "id": "nonexistent_files", + "wrong": "Referencing files that don't exist in the workspace", + "correct": "Only reference files that exist and have been verified", + "reason": "Prevents broken links and maintains documentation integrity" + } +] + +## Validation + +@validation { + "required": [ + "Frontmatter must be present and valid", + "All JSON must be properly formatted", + "Main title must be present", + "At least one content section", + "Complete implementation examples when relevant", + "All referenced files must exist in the workspace" + ], + "recommended": [ + "Version information", + "Last updated date", + "Clear examples", + "Proper code formatting", + "Type definitions", + "Validation rules", + "Verify file existence before referencing" + ] +} + +@version "1.1.0" +@last_updated "2024-03-19" \ No newline at end of file