From 2ed11308a84dae8ac2ae5f4257e86a28c5abbd77 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 1 Nov 2022 14:51:34 +0100 Subject: [PATCH] feat(graphql): improuve reptition champs schema --- app/graphql/schema.graphql | 7 +++- .../types/champs/repetition_champ_type.rb | 20 ++++++++--- app/graphql/types/dossier_type.rb | 4 --- app/services/serializer_service.rb | 6 ++-- spec/graphql/dossier_spec.rb | 36 +++++++++++++++++++ 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index 1e1e34119..5379787a5 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -1949,13 +1949,14 @@ type Query { } type RepetitionChamp implements Champ { - champs: [Champ!]! + champs: [Champ!]! @deprecated(reason: "Utilisez le champ `rows` à la place.") id: ID! """ Libellé du champ. """ label: String! + rows: [Row!]! """ La valeur du champ sous forme texte. @@ -1979,6 +1980,10 @@ type Revision { id: ID! } +type Row { + champs: [Champ!]! +} + type SelectionUtilisateur implements GeoArea { description: String geometry: GeoJSON! diff --git a/app/graphql/types/champs/repetition_champ_type.rb b/app/graphql/types/champs/repetition_champ_type.rb index 00d221be4..a3a98223b 100644 --- a/app/graphql/types/champs/repetition_champ_type.rb +++ b/app/graphql/types/champs/repetition_champ_type.rb @@ -2,13 +2,23 @@ module Types::Champs class RepetitionChampType < Types::BaseObject implements Types::ChampType - field :champs, [Types::ChampType], null: false + class Row < Types::BaseObject + field :champs, [Types::ChampType], null: false + end + + field :champs, [Types::ChampType], null: false, deprecation_reason: 'Utilisez le champ `rows` à la place.' + field :rows, [Row], null: false def champs - if object.champs.loaded? - object.champs - else - Loaders::Association.for(object.class, :champs).load(object) + Loaders::Association.for(object.class, champs: :type_de_champ).load(object).then do |champs| + champs.filter(&:visible?) + end + end + + def rows + Loaders::Association.for(object.class, champs: :type_de_champ).load(object).then do |champs| + object.association(:champs).target = champs.filter(&:visible?) + object.rows.map { { champs: _1 } } end end end diff --git a/app/graphql/types/dossier_type.rb b/app/graphql/types/dossier_type.rb index 76e65f8e0..75e810722 100644 --- a/app/graphql/types/dossier_type.rb +++ b/app/graphql/types/dossier_type.rb @@ -120,8 +120,6 @@ module Types Loaders::Champ .for(object, private: false) .load(ApplicationRecord.id_from_typed_id(id)) - elsif object.champs.loaded? - object.champs.filter(&:visible?) else Loaders::Association .for(object.class, champs: :type_de_champ) @@ -135,8 +133,6 @@ module Types Loaders::Champ .for(object, private: true) .load(ApplicationRecord.id_from_typed_id(id)) - elsif object.champs_private.loaded? - object.champs_private else Loaders::Association.for(object.class, champs_private: :type_de_champ).load(object) end diff --git a/app/services/serializer_service.rb b/app/services/serializer_service.rb index eea18aea0..2a0d441ec 100644 --- a/app/services/serializer_service.rb +++ b/app/services/serializer_service.rb @@ -194,8 +194,10 @@ class SerializerService } fragment RepetitionChampFragment on RepetitionChamp { - champs { - ...ChampFragment + rows { + champs { + ...ChampFragment + } } } diff --git a/spec/graphql/dossier_spec.rb b/spec/graphql/dossier_spec.rb index 70309a4aa..b189747b6 100644 --- a/spec/graphql/dossier_spec.rb +++ b/spec/graphql/dossier_spec.rb @@ -137,6 +137,27 @@ RSpec.describe Types::DossierType, type: :graphql do end end + describe 'dossier with repetition' do + let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :repetition, children: [{ libelle: 'Nom' }, { libelle: 'Age' }] }]) } + let(:dossier) { create(:dossier, :en_construction, :with_populated_champs, procedure: procedure) } + let(:linked_dossier) { create(:dossier, :en_construction) } + let(:query) { DOSSIER_WITH_REPETITION_QUERY } + let(:variables) { { number: dossier.id } } + + let(:rows) do + dossier.champs.first.rows.map do |champs| + { champs: champs.map { { id: _1.to_typed_id } } } + end + end + + it { + expect(data[:dossier][:champs].first).not_to be_nil + expect(data[:dossier][:champs].first[:rows]).not_to be_nil + expect(data[:dossier][:champs].first[:rows].size).to eq(2) + expect(data[:dossier][:champs].first[:rows]).to eq(rows) + } + end + DOSSIER_QUERY = <<-GRAPHQL query($number: Int!) { dossier(number: $number) { @@ -224,4 +245,19 @@ RSpec.describe Types::DossierType, type: :graphql do } } GRAPHQL + + DOSSIER_WITH_REPETITION_QUERY = <<-GRAPHQL + query($number: Int!) { + dossier(number: $number) { + champs { + id + ... on RepetitionChamp { + rows { + champs { id } + } + } + } + } + } + GRAPHQL end