@@ -6,29 +6,79 @@ import { resolverFactory } from './resolvers';
66import MongoID from './types/MongoID' ;
77
88export type TypeConverterInputTypeOpts = {
9+ /**
10+ * What should be input type name.
11+ * By default: baseTypeName + 'Input'
12+ */
913 name ?: string ;
14+ /**
15+ * Provide arbitrary description for generated type.
16+ */
1017 description ?: string ;
18+ /**
19+ * You can leave only whitelisted fields in type via this option.
20+ * Any other fields will be removed.
21+ */
22+ onlyFields ?: string [ ] ;
23+ /**
24+ * You an remove some fields from type via this option.
25+ */
26+ removeFields ?: string [ ] ;
27+ /**
28+ * This option makes provided fieldNames as required
29+ */
30+ requiredFields ?: string [ ] ;
31+
32+ /** @deprecated */
1133 fields ?: {
34+ /** @deprecated use `onlyFields` instead */
1235 only ?: string [ ] ;
36+ /** @deprecated use `removeFields` instead */
1337 remove ?: string [ ] ;
38+ /** @deprecated use `requiredFields` instead */
1439 required ?: string [ ] ;
1540 } ;
1641} ;
1742
18- export type ComposeMongooseOpts < TContext > = {
43+ export type ComposeMongooseOpts < TContext = any > = {
44+ /**
45+ * Which type registry use for generated types.
46+ * By default is used global default registry.
47+ */
1948 schemaComposer ?: SchemaComposer < TContext > ;
49+ /**
50+ * What should be base type name for generated type from mongoose model.
51+ */
2052 name ?: string ;
53+ /**
54+ * Provide arbitrary description for generated type.
55+ */
2156 description ?: string ;
22- fields ?: {
23- only ?: string [ ] ;
24- // rename?: { [oldName: string]: string },
25- remove ?: string [ ] ;
26- } ;
57+ /**
58+ * You can leave only whitelisted fields in type via this option.
59+ * Any other fields will be removed.
60+ */
61+ onlyFields ?: string [ ] ;
62+ /**
63+ * You an remove some fields from type via this option.
64+ */
65+ removeFields ?: string [ ] ;
66+ /**
67+ * You may configure generated InputType
68+ */
2769 inputType ?: TypeConverterInputTypeOpts ;
2870 /**
2971 * You can make fields as NonNull if they have default value in mongoose model.
3072 */
3173 defaultsAsNonNull ?: boolean ;
74+
75+ /** @deprecated */
76+ fields ?: {
77+ /** @deprecated use `onlyFields` */
78+ only ?: string [ ] ;
79+ /** @deprecated use `removeFields` */
80+ remove ?: string [ ] ;
81+ } ;
3282} ;
3383
3484export type GenerateResolverType < TDoc extends Document , TContext = any > = {
@@ -77,9 +127,7 @@ export function composeMongoose<TDoc extends Document, TContext = any>(
77127 tc . setDescription ( opts . description ) ;
78128 }
79129
80- if ( opts . fields ) {
81- prepareFields ( tc , opts . fields ) ;
82- }
130+ prepareFields ( tc , opts ) ;
83131
84132 // generate InputObjectType with required fields,
85133 // before we made fields with default values required too
@@ -135,20 +183,15 @@ function makeFieldsNonNullWithDefaultValues(
135183
136184export function prepareFields (
137185 tc : ObjectTypeComposer < any , any > ,
138- opts : {
139- only ?: string [ ] ;
140- remove ?: string [ ] ;
141- }
186+ opts : ComposeMongooseOpts < any > = { }
142187) : void {
143- if ( Array . isArray ( opts . only ) ) {
144- const onlyFieldNames : string [ ] = opts . only ;
145- const removeFields = Object . keys ( tc . getFields ( ) ) . filter (
146- ( fName ) => onlyFieldNames . indexOf ( fName ) === - 1
147- ) ;
148- tc . removeField ( removeFields ) ;
188+ const onlyFields = opts ?. onlyFields || opts ?. fields ?. only ;
189+ if ( onlyFields ) {
190+ tc . removeOtherFields ( onlyFields ) ;
149191 }
150- if ( opts . remove ) {
151- tc . removeField ( opts . remove ) ;
192+ const removeFields = opts ?. removeFields || opts ?. fields ?. remove ;
193+ if ( removeFields ) {
194+ tc . removeField ( removeFields ) ;
152195 }
153196}
154197
@@ -166,30 +209,25 @@ export function createInputType(
166209 inputTypeComposer . setDescription ( inputTypeOpts . description ) ;
167210 }
168211
169- if ( inputTypeOpts . fields ) {
170- prepareInputFields ( inputTypeComposer , inputTypeOpts . fields ) ;
171- }
212+ prepareInputFields ( inputTypeComposer , inputTypeOpts ) ;
172213}
173214
174215export function prepareInputFields (
175216 inputTypeComposer : InputTypeComposer < any > ,
176- inputFieldsOpts : {
177- only ?: string [ ] ;
178- remove ?: string [ ] ;
179- required ?: string [ ] ;
180- }
217+ inputTypeOpts : TypeConverterInputTypeOpts = { }
181218) : void {
182- if ( Array . isArray ( inputFieldsOpts . only ) ) {
183- const onlyFieldNames : string [ ] = inputFieldsOpts . only ;
184- const removeFields = Object . keys ( inputTypeComposer . getFields ( ) ) . filter (
185- ( fName ) => onlyFieldNames . indexOf ( fName ) === - 1
186- ) ;
187- inputTypeComposer . removeField ( removeFields ) ;
219+ const onlyFields = inputTypeOpts ?. onlyFields || inputTypeOpts ?. fields ?. only ;
220+ if ( onlyFields ) {
221+ inputTypeComposer . removeOtherFields ( onlyFields ) ;
188222 }
189- if ( inputFieldsOpts . remove ) {
190- inputTypeComposer . removeField ( inputFieldsOpts . remove ) ;
223+
224+ const removeFields = inputTypeOpts ?. removeFields || inputTypeOpts ?. fields ?. remove ;
225+ if ( removeFields ) {
226+ inputTypeComposer . removeField ( removeFields ) ;
191227 }
192- if ( inputFieldsOpts . required ) {
193- inputTypeComposer . makeFieldNonNull ( inputFieldsOpts . required ) ;
228+
229+ const requiredFields = inputTypeOpts ?. requiredFields || inputTypeOpts ?. fields ?. required ;
230+ if ( requiredFields ) {
231+ inputTypeComposer . makeFieldNonNull ( requiredFields ) ;
194232 }
195233}
0 commit comments