|
1 | 1 | import { writeSelect } from './writeSelect'
|
2 | 2 | import { writeNonScalarType, writeScalarType, writeSpecialType } from '..'
|
3 |
| -import { ExtendedDMMFSchemaField } from '../../classes' |
| 3 | +import { ExtendedDMMF, ExtendedDMMFSchemaField } from '../../classes' |
4 | 4 | import { type ContentWriterOptions } from '../../types'
|
5 | 5 |
|
| 6 | +function modelHasRelation( |
| 7 | + model: ExtendedDMMFSchemaField['modelType'], |
| 8 | + dmmf: ExtendedDMMF |
| 9 | +): boolean { |
| 10 | + if (typeof model === 'string') { |
| 11 | + const maybeModel = dmmf.datamodel.models.find((m) => m.name === model) |
| 12 | + return maybeModel?.hasRelationFields ?? false |
| 13 | + } |
| 14 | + return false |
| 15 | +} |
| 16 | + |
6 | 17 | export const writeOutputObjectType = (
|
7 | 18 | { fileWriter, dmmf, getSingleFileContent = false }: ContentWriterOptions,
|
8 | 19 | field: ExtendedDMMFSchemaField
|
@@ -120,7 +131,14 @@ export const writeOutputObjectType = (
|
120 | 131 | writer.newLine()
|
121 | 132 | })
|
122 | 133 | })
|
123 |
| - .write(`).strict()`) |
| 134 | + .write(`).strict() `) |
| 135 | + // There is a typing bug that occurs only with models that have relations. |
| 136 | + // a dirty fix here is to typecast the value to the expected type: |
| 137 | + // cf. https://github.com/chrishoermann/zod-prisma-types/issues/98#issuecomment-1800112669 |
| 138 | + .conditionalWrite( |
| 139 | + modelHasRelation(field.modelType, dmmf), |
| 140 | + `as ${field.customArgType}` |
| 141 | + ) |
124 | 142 |
|
125 | 143 | if (useMultipleFiles && !getSingleFileContent) {
|
126 | 144 | writer.blankLine().writeLine(`export default ${field.argName}Schema;`)
|
|
0 commit comments