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

Adding Taxons to Products + HasManyThrough BatchLoader #148

Closed
fhirzall opened this issue Dec 30, 2020 · 2 comments
Closed

Adding Taxons to Products + HasManyThrough BatchLoader #148

fhirzall opened this issue Dec 30, 2020 · 2 comments
Assignees
Labels
wontfix This will not be worked on

Comments

@fhirzall
Copy link

fhirzall commented Dec 30, 2020

Hello,

Thanks for the great work on this gem. I noticed while adding a taxons field to Product following the example here:

https://github.com/solidusio-contrib/solidus_graphql_api#adding-a-new-field

BatchLoader::HasManyThrough ends up resolving the query and produces duplicate taxons for each product. Here's a sample query that's generated with the joins on some test data:

`SELECT `spree_taxons`.`id`, `spree_taxons`.`parent_id`, `spree_taxons`.`position`, `spree_taxons`.`taxonomy_id`, `spree_taxons`.`lft`, `spree_taxons`.`rgt`, `spree_taxons`.`icon_file_name`, `spree_taxons`.`icon_content_type`, `spree_taxons`.`icon_file_size`, `spree_taxons`.`icon_updated_at`, `spree_taxons`.`created_at`, `spree_taxons`.`updated_at`, `spree_taxons`.`depth` FROM `spree_taxons` INNER JOIN `spree_products_taxons` ON `spree_products_taxons`.`taxon_id` = `spree_taxons`.`id` WHERE `spree_products_taxons`.`product_id` IN (2, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 29)
`

This happens here in BatchLoader::HasManyThrough:


      def retrieve_records_for(object_ids)
        through_reflection = reflection.through_reflection

        base_relation
          .joins(through_reflection.name)
          .where("#{through_reflection.table_name}.#{through_reflection.foreign_key}" => object_ids)
      end

We have two potential solutions

  1. include a distinct when batching HasManyThrough associations to prevent duplicate records
  2. replace the joins with an includes

Thoughts? Please let me know if I'm missing something - happy to send in a PR once this is discussed.

Thanks!

@AlessioRocco
Copy link
Contributor

Hi @fhirzall, sorry for the late response but it was a busy start of the year. The first solution with the distinct should work and seems the right direction, let me know if you want to send the PR or if we should take care of that. Thanks a lot!

@stale
Copy link

stale bot commented Mar 16, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Mar 16, 2021
@stale stale bot closed this as completed Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants