Hello. I can't figure out how to create multiple levels of nested queries with @ResolveFiled. I hope for your help.
What I'm doing and Context:
I have a product. The product has a supplier. A vendor-specific product contains product variants. Variants contain options.
I need to make a request in 4 levels:
- Product
- ProductHasProvider
- Product Variants
- Variant Options
I use the "Code First" approach, created an ObjectType for each entity. Next, I create a "Product" resolver.
Creating a second level "ProductHasProvider" with @ResolveField
When adding a ResolveField ("Providers") - it appears inside the main resolver "Product" and resolves the Providers ObjectType. Okay, it works, I can make requests at the 2nd level correctly.
@ResolveField('Providers', () => [ProductHasProvider])
async getProductProviders (@Parent() product: Product) {
const { id } = product;
return await this.productListService.ProductsProviders( { id });
}
- I want to make third level where ProductHasProvider has Variants. I decorate ProductHasProvider as the parent.
@ResolveField(('variants'), type => [Variant])
async getVariants (@Parent() productHasProvider: ProductHasProvider) {
const { id } = productHasProvider;
return await this.productListService.getVariants({ id });
}
In this case, this ResolveField defines the ObjectType for [Variants], but for some reason at the first level. That is, in Apollo studio, the field is displayed in "Product". I can't query Variants for ProductHasProvider.
query Products {
getProducts {
Providers {
id
}
variants {
id
options {
id
}
}
}
}
Expected behavior:
I add a new @ResolveField(() => [Variants]) with "ProductHasProvider" parent (Which is already @ResorveField for Product). And I can do 3rd and 4th level queries.
query Products {
getProducts {
id
Providers {
id
variants {
id
options {
id
}
}
}
}
}
Please tell me what I'm doing wrong and how to achieve what I want. Thank you.