apollo-datasource-mongodb icon indicating copy to clipboard operation
apollo-datasource-mongodb copied to clipboard

TypeScript Error: Argument of type 'Model<T, {}, {}, {}>' is not assignable to parameter of type 'Collection<T>'.

Open sojharo opened this issue 4 years ago • 5 comments

I am getting this weird typescript error when I put Mongoose Model in constructor.

const ListingsDataSource = new Listings(ListingModel)

My Listing Model is defined like this:

export default class Listings extends MongoDataSource<Listing> {
  getListing(_id: string) {
    return this.findOneById(_id, { ttl: MINUTE });
  }
}

My Mongoose Model is defined like this:

const listingSchema = new Schema<Listing>({
  name: { type: String, required: true },
  description: { type: [String], required: true }
});

const ListingModel = model<Listing>("Listing", listingSchema);

export default ListingModel;

My Listing Interfact is defined like this:

export interface Listing {
  _id: ObjectId;
  name: string;
  description: string[];
}

But when I pass my Mongoose Model to DataSource based class. I get following TypScript error:

TypeScript Error: Argument of type 'Model<Listing, {}, {}, {}>' is not assignable to parameter of type 'Collection<Listing>'.

I have tried many ways but this is not getting solved.

sojharo avatar Nov 28 '21 14:11 sojharo

I'm having similar error

vnugent avatar Dec 22 '21 12:12 vnugent

I have found a workaround for this but it is discouraged by Mongoose documentation here.

Workaround is to extend your interface with Document:

import { Document } from "mongoose";

export interface Listing extends Document {
  _id: ObjectId;
  name: string;
  description: string[];
}

I am not good with TypeScript so I just don't know how to fix this in library. But, I think we need to do something here: https://github.com/GraphQLGuide/apollo-datasource-mongodb/blob/master/index.d.ts#L18

sojharo avatar Jan 23 '22 21:01 sojharo

I'd be happy to look at a PR for this!

lorensr avatar Mar 31 '22 04:03 lorensr

I tried using the same mongoose version for my application as specified for apollo-datasource-mongodb but to no avail.

GeorgGroenendaal avatar Apr 21 '22 10:04 GeorgGroenendaal

@lorensr can you please review the following PR: https://github.com/GraphQLGuide/apollo-datasource-mongodb/pull/110

sojharo avatar Sep 27 '22 13:09 sojharo