Skip to content

[TODO]: Thread crud through modelFieldToSDL so idType resolution is no longer a no-op #1974

Description

@lisa-assistant

Summary

In packages/cli/src/commands/generate/sdl/sdlHandler.ts, modelFieldToSDL has a branch that resolves the idType of a related model when field.kind === 'object':

if (Object.entries(types).length && field.kind === 'object') {
  // TODO: `idType()` called without `crud` always returns `undefined`, so
  // this branch is a no-op in practice. Fix in a separate PR by threading
  // `crud` through the call-site.
  const resolvedType = idType(types[field.type])
  if (typeof resolvedType === 'string') {
    field.type = resolvedType
  }
}

The problem: idType(model, crud?) returns undefined when crud is falsy (line 184 — first thing it checks). Since crud is never passed here, the whole branch is a no-op. The original JS silently set field.type = undefined; the TS migration added the typeof resolvedType === 'string' guard to prevent that corruption, and surfaced this bug as a TODO.

Files to change

packages/cli/src/commands/generate/sdl/sdlHandler.ts

  1. modelFieldToSDL — add crud?: boolean to the destructured params interface, then pass it to idType(types[field.type], crud).

  2. inputSDL — add crud?: boolean param; pass it through to modelFieldToSDL calls (there are two: for the filtered field set).

  3. createInputSDL — add crud?: boolean param and forward to inputSDL.

  4. updateInputSDL — same as above.

  5. sdlFromSchemaModel — already has crud: boolean; thread it through to createInputSDL and updateInputSDL calls.

querySDL calls modelFieldToSDL too, but query types don't have a crud context so leaving crud undefined there is correct.

Edge cases

  • Composite primary keys: idType returns ModelSchemaField[] (not a string) in that case. The typeof resolvedType === 'string' guard already handles this correctly — composite key models won't have field.type overwritten, which is the right behavior since field.type is just the Prisma model name.
  • querySDL callers: no crud context, no change needed.
  • Tests: sdlHandler has a test suite — update any fixture snapshots that test SDL generation for models with related types when crud: true.

Background

Surfaced during the JS→TS migration in PR #1944. The original JS code did field.type = idType(types[field.type]) which silently assigned undefined — effectively a no-op that happened not to crash because the field.type was already correct. The TypeScript migration added the typeof guard and this TODO to flag it for a follow-up fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions