graphql-compose / graphql-compose/graphql-compose-mongoose

Resolvers created from discriminator type composer cause type error

Open
#225 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
706
Forks
98
PR merge metrics
No merged PRs in 30d

Description

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:
```graphql
mutation {
postCreateOne(
record: {
title: "title",
creator: "creator",
body: "body",
kind: Discussion
}
) {
record {
body
}
}
}
```
When I try to query for posts (for example):
```graphql
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:
```javascript
/ 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:
```javascript
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"),
};
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.