Resolvers created from discriminator type composer cause type error
I have a base Post schema and Discussion, Job, Event and Notice discriminator schemas built on Post. I've added different custom fields and relations to these TypeComposers, but I want to use resolvers of the form postById rather than more specific resolvers like discussionById or eventById.
While I can create posts with queries of the form:
mutation {
postCreateOne(
record: {
title: "title",
creator: "creator",
body: "body",
kind: Discussion
}
) {
record {
body
}
}
}
When I try to query for posts (for example):
query {
postMany {
body
}
}
I get the error message that "Runtime Object type \"Discussion\" is not a possible type for \"PostInterface\"."
Furthermore, the GraphQL playground shows two generated types: PostInterface and Post. Is it possible for me to get a Post resolver from PostDTC?
My base and discriminator schemas are defined as:
/ Create discriminator key
const DKey = "kind";
// Create types of posts possible
const enumPostType = {
Discussion: "Discussion",
Event: "Event",
Notice: "Notice",
Job: "Job",
};
const PostSchema = new mongoose.Schema({
kind: {
type: mongoose.Schema.Types.Mixed,
require: true,
enum: Object.keys(enumPostType),
description:
"The type of the post (whether event, discussion, or notice)",
},
title: {
type: String,
required: true,
},
body: {
type: String,
required: true,
},
date_created: {
type: Date,
required: false,
default: new Date().getTime(),
},
tags: {
type: [String],
required: false,
default: [],
},
creator: {
type: String,
required: true,
},
});
// Schema definitions for enum types
const DiscussionSchema = new mongoose.Schema();
const NoticeSchema = new mongoose.Schema({
deadline: {
type: Date,
required: true,
},
});
const EventSchema = new mongoose.Schema({
start: {
type: Date,
required: true,
},
end: {
type: Date,
required: true,
},
place: {
type: String,
required: false,
},
});
const JobSchema = new mongoose.Schema({
start: {
type: Date,
required: true,
},
end: {
type: Date,
required: true,
},
place: {
type: String,
required: true,
},
isPaid: {
type: Boolean,
required: true,
},
isClosed: {
type: Boolean,
required: true,
},
});
// Set discriminator key and create the base model
PostSchema.set("discriminatorKey", DKey);
const Post = mongoose.model("Post", PostSchema);
// Set the discriminator for other subtypes
const Discussion = Post.discriminator(
enumPostType.Discussion,
DiscussionSchema,
);
const Notice = Post.discriminator(enumPostType.Notice, NoticeSchema);
const Event = Post.discriminator(enumPostType.Event, EventSchema);
const Job = Post.discriminator(enumPostType.Job, JobSchema);
const PostDTC = composeWithMongooseDiscriminators(Post, paginationOptions);
console.log(PostDTC.getType());
const DiscussionTC = PostDTC.discriminator(Discussion);
const NoticeTC = PostDTC.discriminator(Notice);
const EventTC = PostDTC.discriminator(Event);
const JobTC = PostDTC.discriminator(Job);
I've defined my resolvers as follows:
const PostQuery = {
postById: PostDTC.getResolver("findById"),
postOne: PostDTC.getResolver("findOne"),
postMany: PostDTC.getResolver("findMany"),
postCount: PostDTC.getResolver("count"),
postPagination: PostDTC.getResolver("pagination"),
};
const PostMutation = {
postCreateOne: PostDTC.getResolver("createOne"),
postCreateMany: PostDTC.getResolver("createMany"),
postUpdateById: PostDTC.getResolver("updateById"),
postUpdateOne: PostDTC.getResolver("updateOne"),
postUpdateMany: PostDTC.getResolver("updateMany"),
postRemoveById: PostDTC.getResolver("removeById"),
postRemoveOne: PostDTC.getResolver("removeOne"),
postRemoveMany: PostDTC.getResolver("removeMany"),
};