Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityHydrator fails to nested relations #1161

Closed
heiko-r opened this issue Oct 18, 2021 · 1 comment
Closed

EntityHydrator fails to nested relations #1161

heiko-r opened this issue Oct 18, 2021 · 1 comment
Assignees
Labels
type: bug 🐛 Something isn't working

Comments

@heiko-r
Copy link
Contributor

heiko-r commented Oct 18, 2021

Describe the bug
I'm trying to populate the 'product.facetValues.facet' relation of a ProductVariant. I obtain the ProductVariants from ProductVariantService.findByIds(), so they don't have the product relation yet.

There seem to be two related bugs in EntityHydrator.getMissingRelations:

  • Line 151 doesn't take the possibility of an empty array into account, setting entity to undefined, which throws an error when trying to hydrate a further nested relation.
  • After Line 160, entity is not updated, so that the next part of the relation path is checked against the wrong entity.

To Reproduce
Steps to reproduce the behavior:

  1. Create a Product with some facet values, and a ProductVariant without facets
  2. Use this.entityHydrator.hydrate(ctx, variant, { relations: ['product.facetValues.facet'] }):
    -> It throws the error "Cannot read property 'facet' of undefined" due to:
    • The missing product relation is correctly added to missingRelations, however entity is not updated
    • facetValues is confirmed to exist on the ProductVariant (not ProductVariant.product!), but in this case it's an empty array, so the new value for entity becomes undefined
  3. Use this.entityHydrator.hydrate(ctx, variant, { relations: ['product.facetValues'] }):
    • It only hydrates the product relation, because facetValues is checked against the ProductVariant, not the Product.
  4. It works when hydrating the nested relations level-by-level:
    this.entityHydrator.hydrate(ctx, variant, { relations: ['product'] });
    this.entityHydrator.hydrate(ctx, variant, { relations: ['product.facetValues'] });
    this.entityHydrator.hydrate(ctx, variant, { relations: ['product.facetValues.facet'] });

Expected behavior
EntityHydrator should work with nested relations.

Environment (please complete the following information):

  • @vendure/core version: 1.3.0
  • Nodejs version: 14.17.0
  • Database (mysql/postgres etc): PostgreSQL
@heiko-r heiko-r added the type: bug 🐛 Something isn't working label Oct 18, 2021
@michaelbromley
Copy link
Member

Thanks for the detailed report! Will get this fixed in the next patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants