Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sojharo opened this issue Nov 28, 2021 · 5 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@sojharo
Copy link

sojharo commented Nov 28, 2021

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.

@vnugent
Copy link

vnugent commented Dec 22, 2021

I'm having similar error

@sojharo
Copy link
Author

sojharo commented Jan 23, 2022

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

@lorensr
Copy link
Member

lorensr commented Mar 31, 2022

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

@lorensr lorensr added bug Something isn't working help wanted Extra attention is needed labels Mar 31, 2022
@GeorgGroenendaal
Copy link

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

drinkataco pushed a commit to drinkataco/location-events-api that referenced this issue Sep 19, 2022
Ok, so I really didn't want to do this. It was a last resort...

apollo-datasource-mongodb supports mongoose fine, in javascript. But the
typing expects the model object type to extend Document from mongoose,
so that it determine it to be a mongoose model, rather than a mongodb
collection.

But - not only is this recommended against, but it is not easily
supported without a tonne more hacks whilst using codegen to generate
the types based off of GraphQL types...

As overriding types was not possible (in src/@types/environment.d.ts),
only extending, this commit creates a package patch which removes the
type inferrence and forces the datasource constructor parameter to be a
Mongoose Model. This doesn't fix the root problem, that will have to be
done on the type inferrence - ModelOrCollection. But I can safetly
assume that we'll never use mongodb collections here - just mongoose
models.

🫠V

see: https://mongoosejs.com/docs/typescript.html#using-extends-document
see: GraphQLGuide/apollo-datasource-mongodb#88
drinkataco pushed a commit to drinkataco/location-events-api that referenced this issue Sep 21, 2022
Ok, so I really didn't want to do this. It was a last resort...

apollo-datasource-mongodb supports mongoose fine, in javascript. But the
typing expects the model object type to extend Document from mongoose,
so that it determine it to be a mongoose model, rather than a mongodb
collection.

But - not only is this recommended against, but it is not easily
supported without a tonne more hacks whilst using codegen to generate
the types based off of GraphQL types...

As overriding types was not possible (in src/@types/environment.d.ts),
only extending, this commit creates a package patch which removes the
type inferrence and forces the datasource constructor parameter to be a
Mongoose Model. This doesn't fix the root problem, that will have to be
done on the type inferrence - ModelOrCollection. But I can safetly
assume that we'll never use mongodb collections here - just mongoose
models.

🫠V

see: https://mongoosejs.com/docs/typescript.html#using-extends-document
see: GraphQLGuide/apollo-datasource-mongodb#88
@sojharo
Copy link
Author

sojharo commented Sep 27, 2022

@lorensr can you please review the following PR: #110

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants