fix(graphql): return empty arrays from loaders when loading collections

This commit is contained in:
Paul Chavard 2023-04-18 09:31:17 +02:00
parent 110a8aec97
commit 2a09f1d505
3 changed files with 34 additions and 2 deletions

View file

@ -13,7 +13,7 @@ module Loaders
def perform(keys)
query(keys).each { |record| fulfill(record.stable_id, [record].compact) }
keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
keys.each { |key| fulfill(key, []) unless fulfilled?(key) }
end
private

View file

@ -21,7 +21,7 @@ module Loaders
fulfilled_value = @array ? [record].compact : record
fulfill(record.public_send(@column), fulfilled_value)
end
keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
keys.each { |key| fulfill(key, @array ? [] : nil) unless fulfilled?(key) }
end
private

View file

@ -75,6 +75,27 @@ RSpec.describe Types::DossierType, type: :graphql do
end
end
describe 'dossier with selected champ' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ libelle: 'yolo' }, { libelle: 'toto' }]) }
let(:dossier) { create(:dossier, :en_construction, :with_populated_champs, procedure:) }
let(:query) { DOSSIER_WITH_SELECTED_CHAMP_QUERY }
let(:variables) { { number: dossier.id, id: champ.to_typed_id } }
let(:champ) { dossier.champs_public.last }
context 'when champ exists' do
it {
expect(data[:dossier][:champs].size).to eq 1
expect(data[:dossier][:champs][0][:label]).to eq "toto"
}
end
context "when champ dosen't exists" do
let(:variables) { { number: dossier.id, id: '1234' } }
it { expect(data[:dossier][:champs].size).to eq 0 }
end
end
describe 'dossier with conditional champs' do
include Logic
let(:stable_id) { 1234 }
@ -390,4 +411,15 @@ RSpec.describe Types::DossierType, type: :graphql do
}
}
GRAPHQL
DOSSIER_WITH_SELECTED_CHAMP_QUERY = <<-GRAPHQL
query($number: Int!, $id: ID!) {
dossier(number: $number) {
champs(id: $id) {
id
label
}
}
}
GRAPHQL
end