Skip to content

Commit

Permalink
use batch loader for graphql queries. #274
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 16, 2019
1 parent 8ed26e6 commit 79e7e86
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 5 additions & 1 deletion app/graphql/active_record_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ def initialize(model)
end

def perform(ids)
@model.where(id: ids).each { |record| fulfill(record.id, record) }
if @model.name == "Prefix"
@model.where(prefix: ids).each { |record| fulfill(record.id, record) }
else
@model.where(id: ids).each { |record| fulfill(record.id, record) }
end
ids.each { |id| fulfill(id, nil) unless fulfilled?(id) }
end
end
7 changes: 5 additions & 2 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class QueryType < BaseObject
end

def providers(query: nil)
Provider.query(query, page: { number: 1, size: 250 }).records.to_a
ids = Provider.query(query, page: { number: 1, size: 250 }).results.to_a.map { |p| p.uid }
ElasticsearchLoader.for(Provider).load_many(ids)
end

field :provider, ProviderType, null: false do
Expand All @@ -24,7 +25,8 @@ def provider(id:)
end

def clients(query: nil, year: nil, software: nil)
Client.query(query, year: year, software: software, page: { number: 1, size: 2000 }).records.to_a
ids = Client.query(query, year: year, software: software, page: { number: 1, size: 2000 }).results.to_a.map { |p| p.uid }
ElasticsearchLoader.for(Client).load_many(ids)
end

field :client, ClientType, null: false do
Expand Down Expand Up @@ -55,6 +57,7 @@ def prefixes(query: nil, first: nil)
end

def prefix(id:)
#ActiveRecordLoader.for(Prefix).load(id)
Prefix.where(prefix: id).first
end

Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ def find_by_id(ids, options={})
ids = ids.split(",") if ids.is_a?(String)
options[:page] ||= {}
options[:page][:number] ||= 1
options[:page][:size] ||= 25
options[:page][:size] ||= 2000
options[:sort] ||= { created: { order: "asc" }}

__elasticsearch__.search({
from: (options.dig(:page, :number) - 1) * options.dig(:page, :size),
size: options[:size] || 25,
size: options.dig(:page, :size),
sort: [options[:sort]],
query: {
terms: {
Expand Down
4 changes: 2 additions & 2 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,12 @@ def self.find_by_id(ids, options={})

options[:page] ||= {}
options[:page][:number] ||= 1
options[:page][:size] ||= 25
options[:page][:size] ||= 1000
options[:sort] ||= { created: { order: "asc" }}

__elasticsearch__.search({
from: (options.dig(:page, :number) - 1) * options.dig(:page, :size),
size: options[:size] || 25,
size: options.dig(:page, :size),
sort: [options[:sort]],
query: {
terms: {
Expand Down

0 comments on commit 79e7e86

Please sign in to comment.