Skip to content

Commit 017e713

Browse files
committed
feat: add error handling guidance to generated ORM AGENTS.md
- Add Error Handling section with CRITICAL warning about silent error trap - Show correct vs incorrect patterns (try/catch with .execute() vs .unwrap()) - Document .unwrap(), .unwrapOr(), .unwrapOrElse() helpers - Update Stack and Conventions sections to mention .unwrap() alongside .execute()
1 parent 70d8a2b commit 017e713

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

graphql/codegen/src/core/codegen/orm/docs-generator.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export function generateOrmAgentsDocs(
177177
lines.push('');
178178
lines.push('- Prisma-like ORM client for a GraphQL API (TypeScript)');
179179
lines.push(`- ${tableCount} model${tableCount !== 1 ? 's' : ''}${customOpCount > 0 ? `, ${customOpCount} custom operation${customOpCount !== 1 ? 's' : ''}` : ''}`);
180-
lines.push('- All methods return a query builder; call `.execute()` to run');
180+
lines.push('- All methods return a QueryBuilder; call `.execute()` to run, or `.unwrap()` to throw on error');
181181
lines.push('');
182182

183183
lines.push('## Quick Start');
@@ -192,6 +192,30 @@ export function generateOrmAgentsDocs(
192192
lines.push('```');
193193
lines.push('');
194194

195+
lines.push('## Error Handling');
196+
lines.push('');
197+
lines.push('> **CRITICAL:** `.execute()` returns `{ ok, data, errors }` — it does **NOT** throw.');
198+
lines.push('> A bare `try/catch` around `.execute()` will silently swallow errors.');
199+
lines.push('');
200+
lines.push('```typescript');
201+
lines.push('// WRONG — errors are silently lost:');
202+
lines.push('try { const r = await db.model.findMany({...}).execute(); } catch (e) { /* never runs */ }');
203+
lines.push('');
204+
lines.push('// RIGHT — .unwrap() throws GraphQLRequestError on failure:');
205+
lines.push('const data = await db.model.findMany({...}).unwrap();');
206+
lines.push('');
207+
lines.push('// RIGHT — check .ok for control flow:');
208+
lines.push('const result = await db.model.findMany({...}).execute();');
209+
lines.push('if (!result.ok) { console.error(result.errors); return; }');
210+
lines.push('return result.data;');
211+
lines.push('```');
212+
lines.push('');
213+
lines.push('Available helpers on QueryBuilder (call **instead of** `.execute()`):');
214+
lines.push('- `.unwrap()` — throws on error, returns typed data');
215+
lines.push('- `.unwrapOr(default)` — returns default value on error');
216+
lines.push('- `.unwrapOrElse(fn)` — calls callback with errors on failure');
217+
lines.push('');
218+
195219
lines.push('## Resources');
196220
lines.push('');
197221
lines.push(`- **Full API reference:** [README.md](./README.md) — model docs for all ${tableCount} tables`);
@@ -203,7 +227,7 @@ export function generateOrmAgentsDocs(
203227
lines.push('');
204228
lines.push('- Access models via `db.<ModelName>` (e.g. `db.User`)');
205229
lines.push('- CRUD methods: `findMany`, `findOne`, `create`, `update`, `delete`');
206-
lines.push('- Always call `.execute()` to run the query');
230+
lines.push('- Call `.unwrap()` to run and throw on error, or `.execute()` for discriminated union result');
207231
lines.push('- Custom operations via `db.query.<name>` or `db.mutation.<name>`');
208232
lines.push('');
209233

0 commit comments

Comments
 (0)