Merge pull request #7989 from tchak/fix-graphql-rows

feat(graphql): improuve reptition champs schema
This commit is contained in:
Paul Chavard 2022-11-02 11:25:03 +01:00 committed by GitHub
commit ea4df913ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 12 deletions

View file

@ -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!

View file

@ -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

View file

@ -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

View file

@ -194,8 +194,10 @@ class SerializerService
}
fragment RepetitionChampFragment on RepetitionChamp {
champs {
...ChampFragment
rows {
champs {
...ChampFragment
}
}
}

View file

@ -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