连接符包括

问题描述 投票:0回答:1

我有以下设置:

class Product < ApplicationRecord
  has_many :variants
end

class Variant < ApplicationRecord
  belongs_to :product
end

Types::QueryType = GraphQL::ObjectType.define do
  connection :products, Types::ProductType.connection_type do
    resolve -> (obj, _, _) do
      Product.all.includes(:variants)
    end
  end
end

Types::ProductType = GraphQL::ObjectType.define do
  connection :variants, Types::VariantType.connection_type do
    resolve -> (obj, _, _) { obj.variants }
  end
end

并运行下面的查询:

{
  products {
    edges {
      nodes {
        variants {
          edges {
            node {
              id
            }
          }
        }
      }
    }
  }
}

产生下面的SQL查询:

  Product Load (2.7ms)  SELECT  "products".* FROM "products" LIMIT $1  [["LIMIT", 25]]
  Variant Load (8.6ms)  SELECT "variants".* FROM "variants" WHERE "variants"."product_id" IN (1, 2, 3)
  Variant Load (19.0ms)  SELECT  "variants".* FROM "variants" WHERE "variants"."product_id" = $1 LIMIT $2  [["product_id", 1], ["LIMIT", 25]]
  Variant Load (13.6ms)  SELECT  "variants".* FROM "variants" WHERE "variants"."product_id" = $1 LIMIT $2  [["product_id", 2], ["LIMIT", 25]]
  Variant Load (2.4ms)  SELECT  "variants".* FROM "variants" WHERE "variants"."product_id" = $1 LIMIT $2  [["product_id", 3], ["LIMIT", 25]]

正如我们可以在SQL输出,includes工作,但graphql不在乎,使一个N + 1反正看。这是正常的行为,我被迫使用解决方案,如graphql批来修复或东西是不正确的用我的设置?至于我看到在互联网上,使用includes应该够了这种简单的场景和graphql应该使用产生N + 1的渴望加载的数据。我做了什么错事会在这里?

我在graphql,红宝石1.7.9

ruby-on-rails ruby rails-activerecord graphql-ruby
1个回答
0
投票

我刚刚收到a reply上graphql,红宝石问题跟踪:

嘿,我注意到,限25套被应用到这些查询。你知道这是被应用在哪里?如果你想从最初的查询中使用的结果,你应该删除该限制条款。 (我猜,如果你问.limit(25),ActiveRecord的将不使用缓存的关系。)也许你有一个default_max_page_size?如果你删除它,会发生什么?

所以,长话短说,我删除从我的架构中的default_max_page_size配置,它解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.