Help us understand the problem. What is going on with this article?

GraphQLでN+1を考える

More than 3 years have passed since last update.

RailsでGraphQLをつかうためには次のgemを使うことになると思う。
https://github.com/rmosolgo/graphql-ruby

GraphQLを利用すると、容易にN+1が発生する。
そこで次のようなコードを考えた。

module ShopsResolver
  def self.call(obj, args, ctx)
    limit    = args[:limit]
    order    = args[:order]
    bookable = args[:bookable]

    shops = Shop.visible
    shops = shops.limit(limit)              if limit.present?
    shops = shops.order('RAND()')           if order == "TRENDING" # 仮実装
    shops = shops.order('RAND()')           if order == "RANKING"  # 仮実装
    shops = shops.where(bookable: bookable) if bookable.present?

    shops = shops.includes(:area)
    shops = shops.includes(:categories)
    shops = shops.includes(:shop_categories)
    shops = shops.includes(:category_subs)
    shops = shops.includes(:shop_category_subs)
    shops = shops.includes(:situations)

    shops
  end
end

むやみにincludesをつけまくると、余計なSQL発行が発生して良くない。
そこで、対策を検索した。

とりあえずincludesを全部消して https://github.com/salsify/goldiloader を入れて対応した。
goldiloaderを使ってみたところ、GraphQLではうまく動いたが、一部システムが動かなくなった。

影響範囲が大きかったため https://github.com/Shopify/graphql-batch を試すことにした(続く)

yoneapp
徳島出身・東京在住の起業経験ありのソフトウエアエンジニアです。iOS & Firebase & React.jsがマイブームです。フリーランスプログラマとしてお仕事しつつMENTAでプログラミングのメンター(先生)をしています。
https://menta.work/invite/qbrcVjX00LlAjg0k
engineerlife
技術力をベースに人生を謳歌する人たちのコミュニティです。
https://community.camp-fire.jp/projects/view/280040
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした