admin管理员组文章数量:1384295
Referencing the type-graphql sample repo in the NestJS repository here I am wondering how to create two more layers deep in the query.
Currently it is setup to query recipes, I was able to add another @ObjectType class
@ObjectType()
export class Author {
@Field(type => Int)
id: number;
@Field({ nullable: true })
firstName?: string;
@Field({ nullable: true })
lastName?: string;
}
and created a @ResolveProperty inside the Recipe Resolver with:
@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' });
}
This all works fine (I also created a seperate resolver for Author but it is not my base query so I am not includng it), using this GraphQL query
{
recipe(id: "1") {
title,
author {
firstName
}
}
}
This query returns
{
"data": {
"recipe": {
"title": "1",
"author": {
"firstName": "john"
}
}
}
}
as it should. My question now is how do I add another level? I tried to create a "Publisher" ObjectType
@ObjectType()
export class Publisher {
@Field(type => Int)
id: number;
}
But no bination of creating resolvers or adding ResolveProperty in the Author and Recipe resolvers made it work. Where am I supposed to put the resolver code so that when GraphQL resolvers the Author object it will also resolve the associated Publisher info.
My goal is to get it so that a query such as:
{
recipe(id: "1") {
title,
author {
firstName,
publisher: {
id
}
}
}
}
would return
{
"data": {
"recipe": {
"title": "1",
"author": {
"firstName": "jay",
"publisher": {
id: 4
}
}
}
}
}
Not sure if I am thinking about this wrong but this seems to be a key idea im missing to be able to expand on it! Thank you.
Referencing the type-graphql sample repo in the NestJS repository here I am wondering how to create two more layers deep in the query.
Currently it is setup to query recipes, I was able to add another @ObjectType class
@ObjectType()
export class Author {
@Field(type => Int)
id: number;
@Field({ nullable: true })
firstName?: string;
@Field({ nullable: true })
lastName?: string;
}
and created a @ResolveProperty inside the Recipe Resolver with:
@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' });
}
This all works fine (I also created a seperate resolver for Author but it is not my base query so I am not includng it), using this GraphQL query
{
recipe(id: "1") {
title,
author {
firstName
}
}
}
This query returns
{
"data": {
"recipe": {
"title": "1",
"author": {
"firstName": "john"
}
}
}
}
as it should. My question now is how do I add another level? I tried to create a "Publisher" ObjectType
@ObjectType()
export class Publisher {
@Field(type => Int)
id: number;
}
But no bination of creating resolvers or adding ResolveProperty in the Author and Recipe resolvers made it work. Where am I supposed to put the resolver code so that when GraphQL resolvers the Author object it will also resolve the associated Publisher info.
My goal is to get it so that a query such as:
{
recipe(id: "1") {
title,
author {
firstName,
publisher: {
id
}
}
}
}
would return
{
"data": {
"recipe": {
"title": "1",
"author": {
"firstName": "jay",
"publisher": {
id: 4
}
}
}
}
}
Not sure if I am thinking about this wrong but this seems to be a key idea im missing to be able to expand on it! Thank you.
Share Improve this question asked May 6, 2019 at 3:48 Jay BellJay Bell 4578 silver badges20 bronze badges1 Answer
Reset to default 4You would basically just define an AuthorResolver
that describes, how you would "work" with the Author
. Within this AuthorResolver
you would have a @ResolveProperty
decorated method, to resolve your publisher
property, like so:
// author.resolver.ts
@ResolveProperty('publisher', type => PublisherObjectType, {})
async resolvePublisher(@Parent() parent: AuthorEntity) {
return parent.getPublisher(); // this method would return your Publisher!
}
Note that you need to create your own PublisherObjectType
(with respective decorators) and make it available..
版权声明:本文标题:javascript - Using @nestjsgraphql to create nested queries using @ResolveProperty and @Resolvers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744510480a2609850.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论