From ac75afb77c3575eb5c6526e18526626a132b504d Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 31 Oct 2022 10:37:40 +0100 Subject: [PATCH] fix(graphql): do not load brouillon or deleted linked dossier --- app/graphql/loaders/dossier.rb | 18 ++++++++ .../types/champs/dossier_link_champ_type.rb | 2 +- spec/graphql/dossier_spec.rb | 45 +++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 app/graphql/loaders/dossier.rb diff --git a/app/graphql/loaders/dossier.rb b/app/graphql/loaders/dossier.rb new file mode 100644 index 000000000..315f37da2 --- /dev/null +++ b/app/graphql/loaders/dossier.rb @@ -0,0 +1,18 @@ +module Loaders + class Dossier < GraphQL::Batch::Loader + def load(key) + super(key.to_i) + end + + def perform(keys) + query(keys).each { |record| fulfill(record.id, record) } + keys.each { |key| fulfill(key, nil) unless fulfilled?(key) } + end + + private + + def query(keys) + ::Dossier.visible_by_administration.where(id: keys) + end + end +end diff --git a/app/graphql/types/champs/dossier_link_champ_type.rb b/app/graphql/types/champs/dossier_link_champ_type.rb index 8737d2ab4..9bfaead01 100644 --- a/app/graphql/types/champs/dossier_link_champ_type.rb +++ b/app/graphql/types/champs/dossier_link_champ_type.rb @@ -6,7 +6,7 @@ module Types::Champs def dossier if object.value.present? - Loaders::Record.for(Dossier).load(object.value) + Loaders::Dossier.for.load(object.value) end end end diff --git a/spec/graphql/dossier_spec.rb b/spec/graphql/dossier_spec.rb index 877c21798..70309a4aa 100644 --- a/spec/graphql/dossier_spec.rb +++ b/spec/graphql/dossier_spec.rb @@ -108,6 +108,35 @@ RSpec.describe Types::DossierType, type: :graphql do } end + describe 'dossier with linked dossier' do + let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :dossier_link }]) } + let(:dossier) { create(:dossier, :en_construction, :with_populated_champs, procedure: procedure) } + let(:linked_dossier) { create(:dossier, :en_construction) } + let(:query) { DOSSIER_WITH_LINKED_DOSIER_QUERY } + let(:variables) { { number: dossier.id } } + + before do + dossier.champs.first.update(value: linked_dossier.id) + end + + context 'en_construction' do + it { + expect(data[:dossier][:champs].first).not_to be_nil + expect(data[:dossier][:champs].first[:dossier][:id]).to eq(linked_dossier.to_typed_id) + expect(data[:dossier][:champs].first[:dossier][:state]).to eq('en_construction') + } + end + + context 'brouillon' do + let(:linked_dossier) { create(:dossier, :brouillon) } + + it { + expect(data[:dossier][:champs].first).not_to be_nil + expect(data[:dossier][:champs].first[:dossier]).to be_nil + } + end + end + DOSSIER_QUERY = <<-GRAPHQL query($number: Int!) { dossier(number: $number) { @@ -179,4 +208,20 @@ RSpec.describe Types::DossierType, type: :graphql do streetNumber } GRAPHQL + + DOSSIER_WITH_LINKED_DOSIER_QUERY = <<-GRAPHQL + query($number: Int!) { + dossier(number: $number) { + champs { + id + ... on DossierLinkChamp { + dossier { + id + state + } + } + } + } + } + GRAPHQL end