【发布时间】:2019-09-23 16:44:09
【问题描述】:
引用 NestJS 存储库here 中的 type-graphql 示例存储库我想知道如何在查询的深处再创建两个层。
目前它已设置为查询食谱,我可以添加另一个 @ObjectType 类
@ObjectType()
export class Author {
@Field(type => Int)
id: number;
@Field({ nullable: true })
firstName?: string;
@Field({ nullable: true })
lastName?: string;
}
并在配方解析器中创建了一个@ResolveProperty:
@ResolveProperty('author', type => Author)
async getAuthor(@Parent() recipe) {
console.log('Resolver auth in recipe', recipe);
// This will be a database call, just mocking data for now.
return Promise.resolve({ id: 1, firstName: 'john', lastName: 'smith' });
}
这一切都很好(我还为作者创建了一个单独的解析器,但它不是我的基本查询,所以我不包括它),使用这个 GraphQL 查询
{
recipe(id: "1") {
title,
author {
firstName
}
}
}
这个查询返回
{
"data": {
"recipe": {
"title": "1",
"author": {
"firstName": "john"
}
}
}
}
应该如此。我现在的问题是如何添加另一个级别?我试图创建一个“发布者”对象类型
@ObjectType()
export class Publisher {
@Field(type => Int)
id: number;
}
但是,在 Author 和 Recipe 解析器中创建解析器或添加 ResolveProperty 的组合无法使其正常工作。我应该把解析器代码放在哪里,这样当 GraphQL 解析作者对象时,它也会解析相关的发布者信息。
我的目标是得到它,以便查询,例如:
{
recipe(id: "1") {
title,
author {
firstName,
publisher: {
id
}
}
}
}
会返回
{
"data": {
"recipe": {
"title": "1",
"author": {
"firstName": "jay",
"publisher": {
id: 4
}
}
}
}
}
不确定我是否在考虑这个错误,但这似乎是我缺少能够扩展它的关键想法!谢谢。
【问题讨论】:
标签: javascript typescript graphql graphql-js nestjs