graphql-compose / graphql-compose/graphql-compose-mongoose

Type 'Cat' is not assignable to type 'Document<any, any, any>'

Open
#424 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
706
Forks
98
PR merge metrics
No merged PRs in 30d

Description

Latest version installed
`package.json`
```json
"@nestjs/apollo": "^10.1.7",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/graphql": "^10.1.7",
"@nestjs/mongoose": "^9.2.1",
"@nestjs/platform-express": "^9.0.0",
"apollo-server-express": "^3.11.1",
"graphql": "^16.6.0",
"graphql-compose": "9.0.10",
"graphql-compose-mongoose": "9.7.2",
"mongoose": "6.8.3",
```
Following [instructions](https://github.com/graphql-compose/graphql-compose-mongoose#example) and try using graphql-compose-mongoose in NestJS project
```ts
import { Field, Int, ObjectType } from '@nestjs/graphql';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { schemaComposer } from 'graphql-compose';
import { composeMongoose } from 'graphql-compose-mongoose';
import mongoose from 'mongoose';

@Schema({ timestamps: true })
@ObjectType()
export class Cat {
@Prop()
@Field(() => String)
name: string;

@Prop()
@Field(() => Int)
age: number;

@Prop()
@Field(() => String)
breed: string;
}

const schema = SchemaFactory.createForClass(Cat);

const CatModel = mongoose.model('Cat', schema);
const CatTC = composeMongoose(CatModel, {});

schemaComposer.Query.addFields({
catById: CatTC.mongooseResolvers.findById(),
});

export const CatSchema = schemaComposer.buildSchema();
```
But got error at `const CatTC = composeMongoose(CatModel, {});`
```
Argument of type 'Model, {}, {}, {}, {}, DefaultSchemaOptions, Cat>>' is not assignable to parameter of type 'Model, {}, {}, {}, any>'.
The types returned by 'castObject(...)' are incompatible between these types.
Type 'Cat' is not assignable to type 'Document'.ts(2345)
```
~~I can pass any generic `const CatModel = mongoose.model('Cat', schema);` but still having problem
NestJS saying~~
```ts
[Nest] 11348 - 15.01.2023, 20:05:21 ERROR [ExceptionHandler] The 2nd parameter to `mongoose.model()` should be a schema or a POJO
Error: The 2nd parameter to `mongoose.model()` should be a schema or a POJO

Schema injected in nestjs at module file
```
`cat.module.ts`
```ts
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { Cat, CatSchema } from './cat.model';
import { CatService } from './cat.service';
import { CatResolver } from './cat.resolver';

@Module({
imports: [MongooseModule.forFeature([{ name: Cat.name, schema: CatSchema }])],
providers: [CatService, CatResolver],
})
export class CatModule {}
```

Another way - cloning [example](https://github.com/graphql-compose/graphql-compose-examples) and seen you using any heneric too `export const Category = model('Category', CategorySchema);`
If i remove it i got error
```ts
Argument of type 'Model' is not assignable to parameter of type 'Model, {}, {}>'.
Types of property 'find' are incompatible.
Type '{ (callback?: Callback<(Document & { _id: unknown; })[]>): Query<(Document & { _id: unknown; })[], Document & { _id: unknown; }, {}, unknown>; (filter: FilterQuery<...>, callback?: Callback<...>): Query<...>; (filter: FilterQuery<...>, projection?: any, option...' is not assignable to type '{ (callback?: Callback<(Document & { _id: any; })[]>): Query<(Document & { _id: any; })[], Document & { ...; }, {}, Document<...>>; (filter: FilterQuery<...>, callback?: Callback<...>): Query<...>; (filter: FilterQuery<...>, projection?: any, options?: QueryOptions, callb...'.
Types of parameters 'callback' and 'callback' are incompatible.
Types of parameters 'result' and 'result' are incompatible.
Type 'unknown[]' is not assignable to type 'Document[]'.
Type 'unknown' is not assignable to type 'Document'.ts(2345)
```
If update packages to latest version - got same error as in my own project

**Based on the above, there are two questions:**
1. How using package without generic?
2. How integrate with NestJS?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.