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
-
modelFieldToSDL — add crud?: boolean to the destructured params interface, then pass it to idType(types[field.type], crud).
-
inputSDL — add crud?: boolean param; pass it through to modelFieldToSDL calls (there are two: for the filtered field set).
-
createInputSDL — add crud?: boolean param and forward to inputSDL.
-
updateInputSDL — same as above.
-
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.
Summary
In
packages/cli/src/commands/generate/sdl/sdlHandler.ts,modelFieldToSDLhas a branch that resolves the idType of a related model whenfield.kind === 'object':The problem:
idType(model, crud?)returnsundefinedwhencrudis falsy (line 184 — first thing it checks). Sincecrudis never passed here, the whole branch is a no-op. The original JS silently setfield.type = undefined; the TS migration added thetypeof resolvedType === 'string'guard to prevent that corruption, and surfaced this bug as a TODO.Files to change
packages/cli/src/commands/generate/sdl/sdlHandler.tsmodelFieldToSDL— addcrud?: booleanto the destructured params interface, then pass it toidType(types[field.type], crud).inputSDL— addcrud?: booleanparam; pass it through tomodelFieldToSDLcalls (there are two: for the filtered field set).createInputSDL— addcrud?: booleanparam and forward toinputSDL.updateInputSDL— same as above.sdlFromSchemaModel— already hascrud: boolean; thread it through tocreateInputSDLandupdateInputSDLcalls.querySDLcallsmodelFieldToSDLtoo, but query types don't have acrudcontext so leavingcrudundefined there is correct.Edge cases
idTypereturnsModelSchemaField[](not a string) in that case. Thetypeof resolvedType === 'string'guard already handles this correctly — composite key models won't havefield.typeoverwritten, which is the right behavior sincefield.typeis just the Prisma model name.querySDLcallers: nocrudcontext, no change needed.sdlHandlerhas a test suite — update any fixture snapshots that test SDL generation for models with related types whencrud: true.Background
Surfaced during the JS→TS migration in PR #1944. The original JS code did
field.type = idType(types[field.type])which silently assignedundefined— effectively a no-op that happened not to crash because thefield.typewas already correct. The TypeScript migration added thetypeofguard and this TODO to flag it for a follow-up fix.