Skip to content

Comments

fix(codegen): fix CLI codegen bugs - ORM signatures, String.call, type coercion, field filtering#733

Open
pyramation wants to merge 1 commit intomainfrom
devin/1771726269-cli-codegen-fixes
Open

fix(codegen): fix CLI codegen bugs - ORM signatures, String.call, type coercion, field filtering#733
pyramation wants to merge 1 commit intomainfrom
devin/1771726269-cli-codegen-fixes

Conversation

@pyramation
Copy link
Contributor

fix(codegen): fix CLI codegen bugs — ORM signatures, String.call, type coercion, field filtering

Summary

Upstreams five bugs discovered during end-to-end CLI testing in constructive-db#509 into the codegen AST templates so future CLI generation produces correct code out of the box.

Bug File Fix
String.call(val) returns undefined — tokens never stored infra-generator.ts String.call(val)String(val)
update()/delete() called with { id } instead of { where: { id } } table-command-generator.ts Wrap PK in where object
CLI args are always strings but GraphQL expects Boolean/Int/JSON/etc table-command-generator.ts + new utils-generator.ts Generate per-table fieldSchema, pipe through coerceAnswers()
Extra minimist fields (_, tty) leak into mutation data table-command-generator.ts + new utils-generator.ts stripUndefined() filters to schema-defined keys only
Create mutations used raw answers instead of cleaned data table-command-generator.ts Use cleanedData (post-coercion, post-filtering) for data props

Key changes:

  • infra-generator.ts: Fixed String.call()String() in buildSetTokenHandler (line 1648)
  • table-command-generator.ts:
    • Added buildFieldSchemaObject() to generate runtime type schemas from table fields
    • Fixed update/delete to use { where: { id } } ORM signature
    • Added type coercion pipeline: rawAnswerscoerceAnswers()answersstripUndefined()cleanedData
    • Import coerceAnswers, stripUndefined from ../utils or ../../utils (target-aware)
  • utils-generator.ts (NEW): Generates utils.ts with coerceAnswers(), stripUndefined(), parseMutationInput() runtime helpers
  • index.ts: Wired generateUtilsFile() into both single-target and multi-target CLI generation
  • Tests: Updated file count expectations and snapshots (301/301 tests pass)

Review & Testing Checklist for Human

  • Verify ORM signature: Confirm that client.table.update({ where: { id }, data }) and client.table.delete({ where: { id } }) match the actual ORM API. This was tested in constructive-db but worth double-checking the ORM source.
  • Review parseMutationInput dead code: The generated utils.ts includes parseMutationInput() but it's never imported by generated table commands. It was used in hand-patched custom mutation commands in constructive-db. Should we wire it up for custom mutations in the codegen, or remove it?
  • Review as string type assertion: Update/delete handlers cast answers.id as string in the generated code. This assumes id is always a string after coercion (true for UUID type). Is this safe or should we use runtime validation?
  • Test empty string behavior: coerceAnswers() converts empty strings to undefined for ALL types including strings. This prevents sending "" for optional fields, but means you can never intentionally set a string field to empty. Is this the desired behavior?
  • Regenerate constructive-db CLI: Run the codegen on constructive-db schema and verify the generated output matches the manually patched code from PR fix(pgpm): use workspace-level module detection for export flow #509. This confirms the codegen produces correct output end-to-end.

Notes

  • All 301 unit tests pass with updated snapshots
  • Lint check failed due to pre-existing ESLint 9 migration issue (unrelated to this PR)
  • The utils-generator.ts uses string concatenation instead of Babel AST construction (different pattern from other generators, but simpler for this use case)

Link to Devin run: https://app.devin.ai/sessions/d41228699b3a46de9e73cc13dda02882
Requested by: @pyramation

…e coercion, field filtering

- Fix String.call() -> String() in infra-generator.ts buildSetTokenHandler
  (String.call() returns undefined, breaking token storage)

- Fix update/delete ORM signatures in table-command-generator.ts to use
  { where: { id } } instead of flat { id } (matching ORM API)

- Add type coercion for CLI string args -> proper GraphQL types
  (Boolean, Int, Float, JSON, UUID, Enum) via generated fieldSchema

- Add field filtering to strip extra minimist fields (_, tty, etc.)
  via stripUndefined() with schema-aware filtering

- Generate utils.ts with coerceAnswers(), stripUndefined(), parseMutationInput()
  runtime helpers for all generated CLI commands

- Update test expectations and snapshots for new utils.ts file and
  updated generated output (301/301 tests pass)
@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

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.

1 participant