Skip to content

Commit

Permalink
fix (generator): fix type issue on schemas containing relations (#727)
Browse files Browse the repository at this point in the history
This PR fixes the problem where the generated client may contain type
errors for models containing relations.
This is a known problem with Zod and Prisma and is described here:
chrishoermann/zod-prisma-types#98. That issue
also proposes a solution with a dirty type cast:
chrishoermann/zod-prisma-types#98 (comment)
which is implemented in this PR.

This PR removes the need for pinning the version of Zod
(#700).
  • Loading branch information
kevin-dp authored Dec 11, 2023
1 parent d5cdbf1 commit 38e1e44
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-years-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@electric-sql/prisma-generator": patch
---

Fix type issue in generated client for DB schemas containing relations.
22 changes: 20 additions & 2 deletions generator/src/functions/contentWriters/writeOutputObjectType.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { writeSelect } from './writeSelect'
import { writeNonScalarType, writeScalarType, writeSpecialType } from '..'
import { ExtendedDMMFSchemaField } from '../../classes'
import { ExtendedDMMF, ExtendedDMMFSchemaField } from '../../classes'
import { type ContentWriterOptions } from '../../types'

function modelHasRelation(
model: ExtendedDMMFSchemaField['modelType'],
dmmf: ExtendedDMMF
): boolean {
if (typeof model === 'string') {
const maybeModel = dmmf.datamodel.models.find((m) => m.name === model)
return maybeModel?.hasRelationFields ?? false
}
return false
}

export const writeOutputObjectType = (
{ fileWriter, dmmf, getSingleFileContent = false }: ContentWriterOptions,
field: ExtendedDMMFSchemaField
Expand Down Expand Up @@ -120,7 +131,14 @@ export const writeOutputObjectType = (
writer.newLine()
})
})
.write(`).strict()`)
.write(`).strict() `)
// There is a typing bug that occurs only with models that have relations.
// a dirty fix here is to typecast the value to the expected type:
// cf. https://github.com/chrishoermann/zod-prisma-types/issues/98#issuecomment-1800112669
.conditionalWrite(
modelHasRelation(field.modelType, dmmf),
`as ${field.customArgType}`
)

if (useMultipleFiles && !getSingleFileContent) {
writer.blankLine().writeLine(`export default ${field.argName}Schema;`)
Expand Down
3 changes: 2 additions & 1 deletion generator/src/functions/writeSingleFileArgTypeStatements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const writeSingleFileArgTypeStatements: WriteStatements = (

fileWriter.writeHeading(`ARGS`, 'FAT')

dmmf.schema.outputObjectTypes.argTypes.forEach((outputType) => {
const types = dmmf.schema.outputObjectTypes
types.argTypes.forEach((outputType) => {
outputType.prismaActionFields.forEach((field) => {
writeOutputObjectType({ dmmf, fileWriter }, field)
})
Expand Down

0 comments on commit 38e1e44

Please sign in to comment.