Skip to content

Conversation

@pyramation
Copy link
Contributor

@pyramation pyramation commented Jan 14, 2026

Summary

Replaces string concatenation (lines.push(), props.push()) and ts-morph usage with pure Babel AST generation using @babel/types and @babel/generator across all React Query hook generators.

Files Converted to Babel AST

File Description
query-keys.ts Query key factories for all entities
mutation-keys.ts Mutation key factories for tracking in-flight mutations
invalidation.ts Type-safe cache invalidation helpers
queries.ts List/single query hooks
mutations.ts Create/update/delete mutation hooks
custom-queries.ts Custom query hooks
custom-mutations.ts Custom mutation hooks
schema-types-generator.ts Schema type definitions
barrel.ts Index file generators

A new babel-ast.ts helper file provides reusable AST builders for common patterns (JSDoc comments, etc.).

Note: The ts-morph dependency and ts-ast.ts file remain because ORM generators (orm/ directory) still use them. This PR only migrates the React Query codegen files.

Updates Since Last Revision

Added comprehensive snapshot tests for all React Query hook generators:

  • 33 new snapshot tests covering query hooks, mutation hooks, custom operations, schema types, and barrel files
  • Total test count: 135 tests, 56 snapshots (all passing)

Review & Testing Checklist for Human

  • Verify generated output: Run the codegen CLI against a real schema and confirm all generated files are valid TypeScript and work correctly in a React application
  • Review snapshot diffs: The Babel generator produces slightly different formatting. Review the new snapshots in react-query-hooks.test.ts.snap to ensure the semantic output is correct
  • Test edge cases: Verify parent-child relationships, custom queries/mutations, and cascade invalidation work correctly with the new AST-based generation
  • Check for regressions: The scope expanded significantly - verify that queries.ts, mutations.ts, and other converted files still generate correct hooks

Recommended Test Plan

  1. Run pnpm build in graphql/codegen to verify compilation
  2. Run pnpm test to verify all 135 tests pass
  3. Use the codegen CLI on a real project to generate files and verify they compile and function correctly in a React app

Notes

  • Babel comment helpers (commentBlock, commentLine) are used for JSDoc comments
  • The pnpm-lock.yaml changes are mostly YAML formatting normalization, not actual dependency changes
  • New test file react-query-hooks.test.ts validates generated output for all hook types

Link to Devin run: https://app.devin.ai/sessions/4d5df52ad251452da815b8cb06618596
Requested by: Dan Lynch (@pyramation)

yyyyaaa and others added 4 commits January 14, 2026 13:51
…tion

- Add babel-ast.ts helper file with reusable AST builders for:
  - Export const declarations with object literals
  - Object properties with string/array/function values
  - Arrow function expressions with parameters
  - TSAsExpression for 'as const'
  - Type aliases and imports
  - Block statements with conditionals
  - JSDoc and line comments using Babel's comment system

- Rewrite query-keys.ts using pure Babel AST (@babel/types + @babel/generator)
- Rewrite mutation-keys.ts using pure Babel AST
- Rewrite invalidation.ts using pure Babel AST

- Add @babel/generator, @babel/types, @types/babel__generator dependencies
- Update snapshots to match Babel generator output

All code generation now uses AST nodes instead of string concatenation.
Prettier formatting is applied at file-write time in the CLI pipeline.
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

…arrel to pure Babel AST

- Remove unnecessary helper abstractions from babel-ast.ts
- Use raw t.* calls instead of wrapper functions
- Convert barrel.ts from string concatenation to Babel AST
- Fix if statement formatting to use block statements
- All 102 tests pass
- Rewrite queries.ts with pure Babel AST (replace ts-morph)
- Rewrite mutations.ts with pure Babel AST (replace ts-morph)
- Rewrite custom-queries.ts with pure Babel AST (replace ts-morph)
- Rewrite custom-mutations.ts with pure Babel AST (replace ts-morph)
- Rewrite schema-types-generator.ts with pure Babel AST
- Update type-resolver.ts to remove ts-ast dependency (keep ts-ast.ts for ORM generators)

All 102 tests pass, all 23 snapshots pass.
…generators

Add 33 new snapshot tests covering:
- Query hooks (list/single) with/without centralized keys
- Mutation hooks (create/update/delete) with/without centralized keys
- Custom query hooks with/without arguments
- Custom mutation hooks with various argument types
- Schema types generator
- Barrel file generators (queries, mutations, main, custom)
@pyramation pyramation merged commit 760cf19 into main Jan 14, 2026
36 checks passed
@pyramation pyramation deleted the devin/1768379715-babel-ast-codegen branch January 14, 2026 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants