fix balances query n+1

This commit is contained in:
João Geonizeli
2021-08-11 21:45:15 -03:00
parent ee5edeb9a1
commit 5fc02f00f8
3 changed files with 24 additions and 1 deletions

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
module Sources
class ActiveRecord < GraphQL::Dataloader::Source
# rubocop:disable Lint/MissingSuper
def initialize(model_class)
@model_class = model_class
end
def fetch(ids)
@model_class
.where(id: ids)
.index_by(&:id)
.slice(*ids)
.values
end
end
end

View File

@@ -7,7 +7,11 @@ module Types
graphql_name "Balance"
field :id, ID, null: false
field :currency, CurrencyType, null: false
field :amount, String, null: false
field :currency, CurrencyType, null: false
def currency
dataloader.with(Sources::ActiveRecord, Currency).load(object.currency_id)
end
end
end

View File

@@ -2,6 +2,7 @@
class XStakeSchema < GraphQL::Schema
# mutation(Types::MutationType)
query(Types::QueryType)
use GraphQL::Dataloader
def self.resolve_type(abstract_type, obj, ctx)
case obj