Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (generator): fix type issue on schemas containing relations #727

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading