graphql-compose / graphql-compose-mongoose

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

Type 'Doc' does not satisfy the constraint 'Document<any, any, any>' when using discriminators #366

Closed Oluwatemilorun closed 2 years ago

Oluwatemilorun commented 2 years ago

After upgrading to v9.5.2 using composeWithMongooseDiscriminators throws a typescript error when casting Model types.

// 1. Create an interface representing a document in MongoDB.
export interface User {
    name: string;
    email: string;
    phone: string;
    type: UserType;
}

// 2. Create a Schema corresponding to the document interface.
export const UserSchema = new Schema<User>(
    {
        name: { type: String, required: true, maxlength: 50 },
        email: { type: String, unique: true, required: true },
        phone: { type: String, unique: true, required: true },

        type: {
            type: String,
            enum: Object.keys(UserType),
            default: UserType.CLIENT,
            required: true,
        },
    },
    {
        timestamps: true,
    }
);

// 3. set discriminator Key
UserSchema.set('discriminatorKey', 'type');

// 4. Create base model
const User = model<User>('User', UserSchema);

// 5. Create DiscriminatorTypeComposer
const UserDTC = composeWithMongooseDiscriminators<User, AppContext>(User, {});

The above example will throw an error

Type 'User' does not satisfy the constraint 'Document<any, any, any>'.
  Type 'User' is missing the following properties from type 'Document<any, any, any>': $getAllSubdocs, $ignore, $isDefault, $isDeleted, and 48 more.

graphql-compose-mongoose: ^9.5.2 mongoose: ^5.13.7

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 9.5.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket: