Skip to content

Commit 619ab62

Browse files
Copilothuangyiirene
andcommitted
Address code review feedback: fix mutations, add legend, clarify examples
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent 812b9f3 commit 619ab62

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

docs/guide/query-best-practices.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ ObjectQL provides **three distinct query interfaces**, each optimized for differ
1414
| **REST API** | Simple CRUD, mobile apps | Low | ⭐⭐⭐⭐ | ⭐⭐⭐ |
1515
| **GraphQL** | Complex data graphs, modern SPAs | High | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
1616

17+
*Rating scale: ⭐ = lowest, ⭐⭐⭐⭐⭐ = highest*
18+
1719
---
1820

1921
## 2. JSON-DSL Query Protocol (Recommended Default)
@@ -117,11 +119,17 @@ await app.object('order').find({
117119

118120
**Bad:**
119121
```typescript
120-
// Multiple round trips
122+
// Multiple round trips (N+1 query problem)
121123
const tasks = await app.object('task').find({});
124+
const enrichedTasks = [];
122125
for (const task of tasks) {
123-
task.project = await app.object('project').findOne(task.project_id);
124-
task.assignee = await app.object('user').findOne(task.assignee_id);
126+
const project = await app.object('project').findOne(task.project_id);
127+
const assignee = await app.object('user').findOne(task.assignee_id);
128+
enrichedTasks.push({
129+
...task,
130+
project,
131+
assignee
132+
});
125133
}
126134
```
127135

@@ -408,11 +416,14 @@ query GetDashboardData {
408416
When building custom resolvers, use DataLoader pattern to batch database queries:
409417

410418
```typescript
411-
// Bad: N+1 queries
419+
// Bad: N+1 queries (inefficient)
412420
const tasks = await taskRepo.find();
413-
for (const task of tasks) {
414-
task.assignee = await userRepo.findOne(task.assignee_id);
415-
}
421+
const tasksWithAssignee = await Promise.all(
422+
tasks.map(async (task) => ({
423+
...task,
424+
assignee: await userRepo.findOne(task.assignee_id),
425+
})),
426+
);
416427

417428
// Good: Batched loading (1+1 queries)
418429
const tasks = await taskRepo.find();
@@ -421,9 +432,10 @@ const users = await userRepo.find({
421432
filters: [['id', 'in', userIds]]
422433
});
423434
const userMap = new Map(users.map(u => [u.id, u]));
424-
tasks.forEach(task => {
425-
task.assignee = userMap.get(task.assignee_id);
426-
});
435+
const tasksWithAssigneeBatched = tasks.map((task) => ({
436+
...task,
437+
assignee: userMap.get(task.assignee_id),
438+
}));
427439
```
428440

429441
---
@@ -549,6 +561,8 @@ async function autoAssign(task: any) {
549561
```
550562

551563
**Why NOT SQL strings:**
564+
565+
*Example of AI hallucination:*
552566
```sql
553567
-- AI might hallucinate invalid syntax
554568
SELECT * FROM tasks WHERE due_date < NOW()
@@ -801,7 +815,6 @@ query {
801815
- [Querying Guide](./querying.md) - Step-by-step query examples
802816
- [GraphQL API Documentation](../api/graphql.md) - GraphQL setup and usage
803817
- [REST API Documentation](../api/rest.md) - REST endpoint reference
804-
- [Performance Tuning](./performance.md) - Advanced optimization strategies
805818

806819
---
807820

0 commit comments

Comments
 (0)