Add id argument to most collections on dossier
This commit is contained in:
parent
0bd8721776
commit
bb072ba9e9
6 changed files with 92 additions and 19 deletions
27
app/graphql/loaders/champ.rb
Normal file
27
app/graphql/loaders/champ.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
# references:
|
||||
# https://github.com/Shopify/graphql-batch/blob/master/examples/record_loader.rb
|
||||
|
||||
module Loaders
|
||||
class Champ < GraphQL::Batch::Loader
|
||||
def initialize(dossier, private: false)
|
||||
@where = { dossier: dossier, private: private }
|
||||
end
|
||||
|
||||
def load(key)
|
||||
super(key.to_i)
|
||||
end
|
||||
|
||||
def perform(keys)
|
||||
query(keys).each { |record| fulfill(record.stable_id, [record].compact) }
|
||||
keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def query(keys)
|
||||
::Champ.where(@where)
|
||||
.includes(:type_de_champ)
|
||||
.where(types_de_champ: { stable_id: keys })
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,11 +3,13 @@
|
|||
|
||||
module Loaders
|
||||
class Record < GraphQL::Batch::Loader
|
||||
def initialize(model, column: model.primary_key, where: nil)
|
||||
def initialize(model, column: model.primary_key, where: nil, includes: nil, array: false)
|
||||
@model = model
|
||||
@column = column.to_s
|
||||
@column_type = model.type_for_attribute(@column)
|
||||
@where = where
|
||||
@includes = includes
|
||||
@array = array
|
||||
end
|
||||
|
||||
def load(key)
|
||||
|
@ -15,7 +17,10 @@ module Loaders
|
|||
end
|
||||
|
||||
def perform(keys)
|
||||
query(keys).each { |record| fulfill(record.public_send(@column), record) }
|
||||
query(keys).each do |record|
|
||||
fulfilled_value = @array ? [record].compact : record
|
||||
fulfill(record.public_send(@column), fulfilled_value)
|
||||
end
|
||||
keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
|
||||
end
|
||||
|
||||
|
@ -24,6 +29,7 @@ module Loaders
|
|||
def query(keys)
|
||||
scope = @model
|
||||
scope = scope.where(@where) if @where
|
||||
scope = scope.includes(@includes) if @includes
|
||||
scope.where(@column => keys)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue