fix balances query n+1
This commit is contained in:
18
app/graphql/sources/active_record.rb
Normal file
18
app/graphql/sources/active_record.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user