GraphQLGuide / apollo-datasource-mongodb

Apollo data source for MongoDB
MIT License
285 stars 64 forks source link

Union type for model doesnt work #70

Closed vasily-polonsky closed 3 years ago

vasily-polonsky commented 3 years ago

Hello, union type on the top level doesn't supported. Ts understands union when I am using mongoose, but when I am trying to use datasource with mongoose model, have an error Argument of type 'Model<Pet, {}, {}>' is not assignable to parameter of type 'Model<Cat, {}, {}> | Model<Dog, {}, {}>'.

` import { MongoDataSource } from "apollo-datasource-mongodb"; import { Document, model, Schema, Types } from "mongoose";

enum AnimalType { Cat = "CAT", Dog = "DOG" } export interface Animal extends Document { _id: Types.ObjectId; }

export interface Cat extends Animal { type: AnimalType.Cat; } export interface Dog extends Animal { type: AnimalType.Dog; }

type Pet = Cat | Dog; export const petSchema = new Schema({ type: { type: String, required: true, enum: Object.values(AnimalType) } }); export const PetModel = model("Pets", petSchema); PetModel.create({ type: AnimalType.Dog }).then((pet) => {});

PetModel.find({}).then((pets) => {}); export class PetsDataSource extends MongoDataSource {} new PetsDataSource(PetModel); `

Created a PR