graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
707 stars 94 forks source link

graphql-compose-mongoose with Typescript without extending Document #414

Open fotAlat opened 2 years ago

fotAlat commented 2 years ago

I was interested in using the library because i found it very handy in terms of translating Mongoose Schemas to GraphQLObjects. I was blown away by how good it works but i noticed something. In models that we use Typescript interfaces it complains if I do not use the Extends Documents in the interface.

Is there any reason why we should use the extends Document when the mongoose Documentation recommends against that?

https://mongoosejs.com/docs/typescript.html

TubbyStubby commented 1 year ago

Would it be a good idea to hack it like this?

type IUser = InferSchemaType<typeof userSchema>;

const User = mongoose.model('User', userSchema);

const UserTC = composeMongoose(User as unknown as Model<Document<IUser>>, {});
tatejones commented 1 year ago

I have a similar issue when attempting to use a model from typegoose when the class/interface (eg. User) doesn't extend document.

const userModel = getModelForClass(User) const UserModelTC = composeMongoose(userModel)

Argument of type 'ReturnModelType<typeof User, BeAnObject>' is not assignable to parameter of type 'Model<Document<any, any, any>, {}, {}, {}, Document<unknown, {}, Document<any, any, any>> & Document<any, any, any> & { _id: ObjectId; }, any>'.