Skip to content

Loading associations of multiple records creates N+1 queries #4

@ChrisKaun

Description

@ChrisKaun

Hey,
found this fork while trying to upgrade my application to rails v6, which didn't work because the original repo isn't maintained anymore, so first of all: Thanks for sharing your work.

I've run into a problem when loading multiple records. The app is on rails 5.2.6 and graphql 1.12.8.

A offer has a assigned customer.

module Types
  class OfferType < Types::BaseObject
    graphql_name 'Offer'
    field :customer, Unions::ContactsUnion, null: false do
      preload :customer
    end
  end
end

When loading multiple offers and their customers the customers are not loaded in a single sql-query

query offers {
  offers(ids: [6, 7]) {
    id
    customer {
      ... on Company {
        id
      }
    }
  }
}
Offer Load (0.5ms)  SELECT "offers".* FROM "offers" WHERE "offers"."id" IN ($1, $2)  [["id", 6], ["id", 7]]
Company Load (0.6ms)  SELECT  "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
Company Load (0.4ms)  SELECT  "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]

I've tested this repo with my current production branch, where i simply replaced the ConsultingMD repo version with this forks version and everything worked as expected, so I guess this might have something to do with the newer graphql version I've installed.
The following deprecation warning gets raised once I trigger a query:

DEPRECATION WARNING: Field instrumentation (#<GraphQL::Preload::Instrument:0x00007f61b733bf38>) will be removed in GraphQL-Ruby 2.0, please upgrade to field extensions: https://graphql-ruby.org/type_definitions/field_extensions.html (called from enable_preloading at /home/xx/xxx/vendor/bundle/bundler/gems/graphql-preload-5040f7851bca/lib/graphql/preload.rb:28)

Is it possible that the issue I'm facing comes from the new GraphQL version?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions