11/* eslint-disable no-use-before-define, no-param-reassign, global-require */
22
3- import type { ObjectTypeComposer , InputTypeComposer , SchemaComposer } from 'graphql-compose' ;
3+ import type { ObjectTypeComposer , SchemaComposer } from 'graphql-compose' ;
44import { schemaComposer as globalSchemaComposer } from 'graphql-compose' ;
55import type { Model , Document } from 'mongoose' ;
66import { convertModelToGraphQL } from './fieldsConverter' ;
77import { resolverFactory , AllResolversOpts } from './resolvers' ;
88import MongoID from './types/MongoID' ;
99import { GraphQLResolveInfo } from 'graphql' ;
10+ import { TypeConverterInputTypeOpts , prepareFields , createInputType } from './composeMongoose' ;
1011
1112export type ComposeWithMongooseOpts < TContext > = {
1213 schemaComposer ?: SchemaComposer < TContext > ;
@@ -29,16 +30,6 @@ export type TransformRecordIdFn<TContext = any> = (
2930 info : GraphQLResolveInfo
3031) => any ;
3132
32- export type TypeConverterInputTypeOpts = {
33- name ?: string ;
34- description ?: string ;
35- fields ?: {
36- only ?: string [ ] ;
37- remove ?: string [ ] ;
38- required ?: string [ ] ;
39- } ;
40- } ;
41-
4233export function composeWithMongoose < TDoc extends Document , TContext = any > (
4334 model : Model < TDoc > ,
4435 opts : ComposeWithMongooseOpts < TContext > = { }
@@ -81,67 +72,6 @@ export function composeWithMongoose<TDoc extends Document, TContext = any>(
8172 return tc ;
8273}
8374
84- export function prepareFields (
85- tc : ObjectTypeComposer < any , any > ,
86- opts : {
87- only ?: string [ ] ;
88- remove ?: string [ ] ;
89- }
90- ) : void {
91- if ( Array . isArray ( opts . only ) ) {
92- const onlyFieldNames : string [ ] = opts . only ;
93- const removeFields = Object . keys ( tc . getFields ( ) ) . filter (
94- ( fName ) => onlyFieldNames . indexOf ( fName ) === - 1
95- ) ;
96- tc . removeField ( removeFields ) ;
97- }
98- if ( opts . remove ) {
99- tc . removeField ( opts . remove ) ;
100- }
101- }
102-
103- export function prepareInputFields (
104- inputTypeComposer : InputTypeComposer < any > ,
105- inputFieldsOpts : {
106- only ?: string [ ] ;
107- remove ?: string [ ] ;
108- required ?: string [ ] ;
109- }
110- ) : void {
111- if ( Array . isArray ( inputFieldsOpts . only ) ) {
112- const onlyFieldNames : string [ ] = inputFieldsOpts . only ;
113- const removeFields = Object . keys ( inputTypeComposer . getFields ( ) ) . filter (
114- ( fName ) => onlyFieldNames . indexOf ( fName ) === - 1
115- ) ;
116- inputTypeComposer . removeField ( removeFields ) ;
117- }
118- if ( inputFieldsOpts . remove ) {
119- inputTypeComposer . removeField ( inputFieldsOpts . remove ) ;
120- }
121- if ( inputFieldsOpts . required ) {
122- inputTypeComposer . makeFieldNonNull ( inputFieldsOpts . required ) ;
123- }
124- }
125-
126- export function createInputType (
127- tc : ObjectTypeComposer < any , any > ,
128- inputTypeOpts : TypeConverterInputTypeOpts = { }
129- ) : void {
130- const inputTypeComposer = tc . getInputTypeComposer ( ) ;
131-
132- if ( inputTypeOpts . name ) {
133- inputTypeComposer . setTypeName ( inputTypeOpts . name ) ;
134- }
135-
136- if ( inputTypeOpts . description ) {
137- inputTypeComposer . setDescription ( inputTypeOpts . description ) ;
138- }
139-
140- if ( inputTypeOpts . fields ) {
141- prepareInputFields ( inputTypeComposer , inputTypeOpts . fields ) ;
142- }
143- }
144-
14575export function createResolvers (
14676 model : Model < any > ,
14777 tc : ObjectTypeComposer < any , any > ,
0 commit comments