[英]How to do a Field Resolver for a Union Type in type-graphql
Consider the following考虑以下
The stack is type-graphql / type-orm / postgres堆栈是 type-graphql / type-orm / postgres
I am using Single Table Inheritance (STI) and Union Types to represent multiple feed types.我使用单表 Inheritance (STI) 和联合类型来表示多种提要类型。
All is working nicely -- until I wanted to add a Field Resolver -- and I could not find a way to get that to work.一切都很好——直到我想添加一个字段解析器——我找不到让它工作的方法。
It looks like you can not pass an interface type to @Resolver看起来您无法将接口类型传递给 @Resolver
Some code snippits to illustrate:一些代码片段来说明:
The abstract top level entity抽象的顶级实体
@InterfaceType()
@Entity({ orderBy: { createdAt: 'DESC' } })
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
export abstract class FeedItem {
@Field(() => ID)
@PrimaryGeneratedColumn('uuid')
id: string
@Field()
@Column({ nullable: false })
type: string
@Field(() => Customer, { nullable: true })
@ManyToOne(
() => Customer,
customer => customer.feedItems,
{ onDelete: 'CASCADE' }
)
customer: Lazy<Customer>
@Field(() => [FeedItemComment])
@OneToMany(
() => FeedItemComment,
feedItemComment => feedItemComment.feedItem,
{ lazy: true }
)
comments: Lazy<FeedItemComment[]>
The resolver解析器
export const FeedResultUnion = createUnionType({
name: 'FeedItemResult', // the name of the GraphQL union
types: [AuthoredFeedItem, MilestoneFeedItem, TaskFeedItem, AssessmentFeedItem, WeeklySummaryFeedItem], // array of object types classes
@Resolver(() => FeedItem) // <---- this does not work, nor does FeedItemResult
export class FeedItemResolver {
// would love to do something like the following, but can't ...
@Authorized()
@FieldResolver(() => CommentsResponse)
async comments(@Root() feedItem: FeedItem) {
const [feedItems, count] = await getRepository(FeedItemComment).findAndCount({
where: {
feedItem,
},
order: { createdOn: 'DESC' },
})
return {
items: feedItems,
count,
}
}
}
})
Anyone can point me to a path to victory?!谁能给我指出一条通往胜利的道路?!
I also had problems with @FieldResolver()
I just made a change in tsconfig.json, I changed "target": "es5"
to "target": "es6"
我也遇到了@FieldResolver()
的问题我刚刚在 tsconfig.json 中进行了更改,我将"target": "es5"
更改为"target": "es6"
The type-graphql documentation says that you must configure the tsconfig.json for it to work type-graphql 文档说您必须配置 tsconfig.json 才能使其工作
I hope I have helped我希望我有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.