@@ -4,6 +4,7 @@ import { castArray, last } from 'lodash';
44import pupa from 'pupa' ;
55import { ClassDeclarationStructure , StructureKind } from 'ts-morph' ;
66
7+ import { BeforeGenerateField } from '../event-names' ;
78import { getGraphqlImport } from '../helpers/get-graphql-import' ;
89import { getGraphqlInputType } from '../helpers/get-graphql-input-type' ;
910import { getPropertyType } from '../helpers/get-property-type' ;
@@ -72,11 +73,12 @@ export function inputType(
7273 const useInputType = config . useInputType . find ( x =>
7374 inputType . name . includes ( x . typeName ) ,
7475 ) ;
76+ const isWhereUnique = isWhereUniqueInputType ( inputType . name ) ;
7577
7678 for ( const field of inputType . fields ) {
7779 field . inputTypes = field . inputTypes . filter ( t => ! removeTypes . has ( String ( t . type ) ) ) ;
7880
79- eventEmitter . emitSync ( ' BeforeGenerateField' , field , args ) ;
81+ eventEmitter . emitSync ( BeforeGenerateField , field , args ) ;
8082
8183 const { inputTypes, isRequired, name } = field ;
8284
@@ -96,10 +98,14 @@ export function inputType(
9698 } ) ;
9799 const modelField = model ?. fields . find ( f => f . name === name ) ;
98100 const isCustomsApplicable = typeName === modelField ?. type ;
101+ const atLeastKeys = model && getWhereUniqueAtLeastKeys ( model ) ;
99102 const whereUniqueInputType =
100103 isWhereUniqueInputType ( typeName ) &&
101- model &&
102- `Prisma.AtLeast<${ typeName } , ${ getWhereUniqueAtLeastKeys ( model ) } >` ;
104+ atLeastKeys &&
105+ `Prisma.AtLeast<${ typeName } , ${ atLeastKeys
106+ . map ( name => `'${ name } '` )
107+ . join ( ' | ' ) } >`;
108+
103109 const propertyType = castArray (
104110 propertySettings ?. name ||
105111 whereUniqueInputType ||
@@ -108,12 +114,21 @@ export function inputType(
108114 type : typeName ,
109115 } ) ,
110116 ) ;
117+
118+ const hasExclamationToken = Boolean (
119+ isWhereUnique &&
120+ config . unsafeCompatibleWhereUniqueInput &&
121+ atLeastKeys ?. includes ( name ) ,
122+ ) ;
111123 const property = propertyStructure ( {
112124 name,
113125 isNullable : ! isRequired ,
126+ hasExclamationToken : hasExclamationToken || undefined ,
127+ hasQuestionToken : hasExclamationToken ? false : undefined ,
114128 propertyType,
115129 isList,
116130 } ) ;
131+
117132 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
118133 classStructure . properties ! . push ( property ) ;
119134
@@ -136,7 +151,7 @@ export function inputType(
136151 config . decorate . some (
137152 d =>
138153 d . name === 'HideField' &&
139- d . from === '@nestjs/graphql' &&
154+ d . from === moduleSpecifier &&
140155 d . isMatchField ( name ) &&
141156 d . isMatchType ( inputType . name ) ,
142157 ) ;
@@ -182,7 +197,7 @@ export function inputType(
182197 ok ( property . decorators , 'property.decorators is undefined' ) ;
183198
184199 if ( shouldHideField ) {
185- importDeclarations . add ( 'HideField' , '@nestjs/graphql' ) ;
200+ importDeclarations . add ( 'HideField' , moduleSpecifier ) ;
186201 property . decorators . push ( { name : 'HideField' , arguments : [ ] } ) ;
187202 } else {
188203 // Generate `@Field()` decorator
@@ -197,22 +212,6 @@ export function inputType(
197212 ] ,
198213 } ) ;
199214
200- // Debug
201- // if (classStructure.name === 'XInput') {
202- // console.log('------------');
203- // console.log({
204- // field,
205- // property,
206- // modelField,
207- // graphqlInputType,
208- // 'args.inputType': args.inputType,
209- // 'classStructure.name': classStructure.name,
210- // classTransformerTypeModels,
211- // modelName,
212- // graphqlType,
213- // });
214- // }
215-
216215 if ( graphqlType === 'GraphQLDecimal' ) {
217216 importDeclarations . add ( 'transformToDecimal' , 'prisma-graphql-type-decimal' ) ;
218217 importDeclarations . add ( 'Transform' , 'class-transformer' ) ;
0 commit comments