|
| 1 | +# Cursor Rules for DQL (Dgraph Query Language) |
| 2 | + |
| 3 | +This directory contains Cursor AI rules that help generate valid and optimized DQL (Dgraph Query Language) queries. These rules guide the AI assistant to understand DQL syntax, patterns, and best practices when working with Dgraph graph databases. |
| 4 | + |
| 5 | +## How to Configure Cursor to Use These Rules |
| 6 | + |
| 7 | +### Option 1: Project Rules (Recommended) |
| 8 | + |
| 9 | +1. **Copy the rules to your project's `.cursor/rules` directory:** |
| 10 | + ```bash |
| 11 | + # From your project root |
| 12 | + mkdir -p .cursor/rules |
| 13 | + cp dql-helper/cursor-rules/*.mdc .cursor/rules/ |
| 14 | + ``` |
| 15 | + |
| 16 | +2. **Rules will automatically apply** when working on files matching the patterns specified in each rule's metadata (or when explicitly referenced). |
| 17 | + |
| 18 | +### Option 2: Global Rules |
| 19 | + |
| 20 | +1. Open Cursor Settings: `Settings > General > Rules for AI` |
| 21 | +2. Copy the content from relevant `.mdc` files into the global rules section |
| 22 | +3. These rules will apply across all your projects |
| 23 | + |
| 24 | +### Option 3: Reference Rules Directly |
| 25 | + |
| 26 | +You can reference these rules in your prompts using the `@` syntax: |
| 27 | +- `@dql` - References the master DQL rule |
| 28 | +- `@dql-simple-query` - References simple query patterns |
| 29 | +- `@dql-aggregation` - References aggregation patterns |
| 30 | + |
| 31 | +### Additional Resources |
| 32 | + |
| 33 | +- [Cursor Rules Documentation](https://docs.cursor.com/en/context/rules) - Official guide on configuring and using Cursor rules |
| 34 | +- [Dgraph DQL Documentation](https://dgraph.io/docs/query-language/) - Official DQL language reference |
| 35 | +- [Dgraph Query Examples](https://dgraph.io/docs/query-language/query-examples/) - Real-world query examples |
| 36 | + |
| 37 | +## What These Rules Do |
| 38 | + |
| 39 | +These rules provide specialized guidance for generating DQL queries, mutations, and understanding Dgraph's graph database patterns. Each rule focuses on a specific aspect of DQL: |
| 40 | + |
| 41 | +### Core Rules |
| 42 | + |
| 43 | +#### `dql.mdc` - Master Rule |
| 44 | +The main entry point that orchestrates query generation. It: |
| 45 | +- Identifies the appropriate query pattern based on user input |
| 46 | +- Routes to specialized rules for specific query types |
| 47 | +- Ensures queries are parameterized with meaningful names |
| 48 | +- Validates query syntax before responding |
| 49 | +- **When to use**: General query requests or when unsure which pattern to use |
| 50 | + |
| 51 | +#### `dql-language.mdc` - Language Structure & Validation |
| 52 | +Provides fundamental understanding of DQL syntax: |
| 53 | +- Query structure (query blocks, var blocks, parameters) |
| 54 | +- Parameter syntax and types (`int`, `float`, `bool`, `string`) |
| 55 | +- Variable usage and validation rules |
| 56 | +- Critical rules for avoiding unused variables |
| 57 | +- **When to use**: Automatically referenced by other rules for syntax validation |
| 58 | + |
| 59 | +### Query Pattern Rules |
| 60 | + |
| 61 | +#### `dql-simple-query.mdc` - Basic Query Patterns |
| 62 | +Covers fundamental query patterns for retrieving entities: |
| 63 | +- **Basic node retrieval** by unique identifier |
| 64 | +- **Multiple nodes** with filtering using `anyofterms`, `allofterms`, `has` |
| 65 | +- **Advanced filtering** with `@filter` and logical operators |
| 66 | +- **Nested relationships** (1st, 2nd, and deeper degree connections) |
| 67 | +- **Common root functions**: `eq()`, `anyofterms()`, `allofterms()`, `has()`, `type()` |
| 68 | +- **When to use**: Standard entity retrieval, filtering, and relationship traversal queries |
| 69 | + |
| 70 | +#### `dql-aggregation.mdc` - Aggregation Operations |
| 71 | +Handles mathematical operations and statistics: |
| 72 | +- **Counting**: Entities, relationships, filtered subsets |
| 73 | +- **Statistical functions**: `sum()`, `avg()`, `min()`, `max()` |
| 74 | +- **Hierarchical aggregations**: Parent-child rollups |
| 75 | +- **Time-based aggregations**: Grouping by date ranges |
| 76 | +- **Ranking**: Top-K queries with `orderdesc`/`orderasc` |
| 77 | +- **Conditional aggregations**: Complex filtering with aggregations |
| 78 | +- **When to use**: Analytics, reporting, statistics, counting operations |
| 79 | + |
| 80 | +#### `dql-nth-degree-count.mdc` - Nth-Degree Counting |
| 81 | +Specialized for counting unique related nodes across multiple relationship levels: |
| 82 | +- Counting distinct nodes at various degrees of separation |
| 83 | +- Avoiding duplicate counts in complex graph traversals |
| 84 | +- **When to use**: "How many unique X are connected to Y through Z" type queries |
| 85 | + |
| 86 | +#### `dql-generic-nth-degree-recommendation.mdc` - Recommendation Queries |
| 87 | +Generates queries for finding related nodes based on relationship counting: |
| 88 | +- Finding entities with the most connections |
| 89 | +- Recommendation algorithms (e.g., "users who liked X also liked Y") |
| 90 | +- Relationship-based similarity queries |
| 91 | +- **When to use**: Recommendation systems, similarity searches, "find related" queries |
| 92 | + |
| 93 | +### Mutation Rules |
| 94 | + |
| 95 | +#### `dql-upsert.mdc` - Mutations & Upserts |
| 96 | +Handles data modifications: |
| 97 | +- **Conditional upserts**: Create if not exists, update if exists |
| 98 | +- Prevents duplicate data using `@upsert` directive |
| 99 | +- Mutation patterns for safe data writing |
| 100 | +- **When to use**: Creating or updating nodes, preventing duplicates |
| 101 | + |
| 102 | +## Rule Selection Guide |
| 103 | + |
| 104 | +The AI automatically selects the appropriate rule based on your query intent: |
| 105 | + |
| 106 | +``` |
| 107 | +User Query Type → Rule Applied |
| 108 | +───────────────────────────────── |
| 109 | +"Find entities matching X" → dql-simple-query |
| 110 | +"Count how many..." → dql-aggregation or dql-nth-degree-count |
| 111 | +"Find related/recommended..." → dql-generic-nth-degree-recommendation |
| 112 | +"Create or update X" → dql-upsert |
| 113 | +General query → dql (master rule, routes automatically) |
| 114 | +``` |
| 115 | + |
| 116 | +## Best Practices |
| 117 | + |
| 118 | +These rules enforce several best practices: |
| 119 | + |
| 120 | +1. **Parameterization**: All queries use meaningful parameter names with default values |
| 121 | +2. **Comments**: Queries include explanatory comments for complex logic |
| 122 | +3. **Validation**: Syntax validation is performed before returning queries |
| 123 | +4. **Performance**: Filters are applied early, avoiding unnecessary data retrieval |
| 124 | +5. **Variable Usage**: Unused variables are avoided to prevent errors |
| 125 | +6. **Type Safety**: Appropriate data types are used for parameters and predicates |
| 126 | + |
| 127 | +## Example Usage |
| 128 | + |
| 129 | +When you ask Cursor AI: |
| 130 | + |
| 131 | +> "Find all movies released after 2020 directed by directors who have won awards" |
| 132 | +
|
| 133 | +The AI will: |
| 134 | +1. Use `dql.mdc` to identify this as a filtering query with relationships |
| 135 | +2. Reference `dql-simple-query.mdc` for the pattern |
| 136 | +3. Generate a parameterized DQL query with proper filtering and nested relationships |
| 137 | +4. Validate the syntax before returning |
| 138 | + |
| 139 | +## Contributing |
| 140 | + |
| 141 | +To add or modify rules: |
| 142 | +1. Create or edit `.mdc` files in this directory |
| 143 | +2. Follow the MDC format with frontmatter metadata |
| 144 | +3. Include clear descriptions and examples |
| 145 | +4. Test with various query scenarios |
| 146 | +5. Update this README to document new rules |
| 147 | + |
| 148 | + |
| 149 | + |
0 commit comments