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 { type RepetitionChamp implements Champ {
champs: [Champ!]! champs: [Champ!]! @deprecated(reason: "Utilisez le champ `rows` à la place.")
id: ID! id: ID!
""" """
Libellé du champ. Libellé du champ.
""" """
label: String! label: String!
rows: [Row!]!
""" """
La valeur du champ sous forme texte. La valeur du champ sous forme texte.
@ -1979,6 +1980,10 @@ type Revision {
id: ID! id: ID!
} }
type Row {
champs: [Champ!]!
}
type SelectionUtilisateur implements GeoArea { type SelectionUtilisateur implements GeoArea {
description: String description: String
geometry: GeoJSON! geometry: GeoJSON!

View file

@ -2,13 +2,23 @@ module Types::Champs
class RepetitionChampType < Types::BaseObject class RepetitionChampType < Types::BaseObject
implements Types::ChampType implements Types::ChampType
class Row < Types::BaseObject
field :champs, [Types::ChampType], null: false 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 def champs
if object.champs.loaded? Loaders::Association.for(object.class, champs: :type_de_champ).load(object).then do |champs|
object.champs champs.filter(&:visible?)
else end
Loaders::Association.for(object.class, :champs).load(object) 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 end
end end

View file

@ -120,8 +120,6 @@ module Types
Loaders::Champ Loaders::Champ
.for(object, private: false) .for(object, private: false)
.load(ApplicationRecord.id_from_typed_id(id)) .load(ApplicationRecord.id_from_typed_id(id))
elsif object.champs.loaded?
object.champs.filter(&:visible?)
else else
Loaders::Association Loaders::Association
.for(object.class, champs: :type_de_champ) .for(object.class, champs: :type_de_champ)
@ -135,8 +133,6 @@ module Types
Loaders::Champ Loaders::Champ
.for(object, private: true) .for(object, private: true)
.load(ApplicationRecord.id_from_typed_id(id)) .load(ApplicationRecord.id_from_typed_id(id))
elsif object.champs_private.loaded?
object.champs_private
else else
Loaders::Association.for(object.class, champs_private: :type_de_champ).load(object) Loaders::Association.for(object.class, champs_private: :type_de_champ).load(object)
end end

View file

@ -194,10 +194,12 @@ class SerializerService
} }
fragment RepetitionChampFragment on RepetitionChamp { fragment RepetitionChampFragment on RepetitionChamp {
rows {
champs { champs {
...ChampFragment ...ChampFragment
} }
} }
}
fragment CarteChampFragment on CarteChamp { fragment CarteChampFragment on CarteChamp {
geoAreas { geoAreas {

View file

@ -137,6 +137,27 @@ RSpec.describe Types::DossierType, type: :graphql do
end end
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 DOSSIER_QUERY = <<-GRAPHQL
query($number: Int!) { query($number: Int!) {
dossier(number: $number) { dossier(number: $number) {
@ -224,4 +245,19 @@ RSpec.describe Types::DossierType, type: :graphql do
} }
} }
GRAPHQL GRAPHQL
DOSSIER_WITH_REPETITION_QUERY = <<-GRAPHQL
query($number: Int!) {
dossier(number: $number) {
champs {
id
... on RepetitionChamp {
rows {
champs { id }
}
}
}
}
}
GRAPHQL
end end