Add DateTime champ to GraphQL

This commit is contained in:
Paul Chavard 2020-12-18 11:21:03 +01:00
parent d509fe2edb
commit dc08993a89
6 changed files with 144 additions and 3 deletions

View file

@ -43,6 +43,7 @@ class Api::V2::Schema < GraphQL::Schema
Types::Champs::CheckboxChampType,
Types::Champs::CiviliteChampType,
Types::Champs::DateChampType,
Types::Champs::DatetimeChampType,
Types::Champs::DecimalNumberChampType,
Types::Champs::DossierLinkChampType,
Types::Champs::IntegerNumberChampType,

View file

@ -180,6 +180,33 @@ type CreateDirectUploadPayload {
}
type DateChamp implements Champ {
"""
La valeur du champ formaté en ISO8601 (Date).
"""
date: ISO8601Date
id: ID!
"""
Libellé du champ.
"""
label: String!
"""
La valeur du champ sous forme texte.
"""
stringValue: String
"""
La valeur du champ formaté en ISO8601 (DateTime).
"""
value: ISO8601DateTime @deprecated(reason: "Utilisez le champ 'date' ou le fragment 'DatetimeChamp' à la place.")
}
type DatetimeChamp implements Champ {
"""
La valeur du champ formaté en ISO8601 (DateTime).
"""
datetime: ISO8601DateTime
id: ID!
"""
@ -191,7 +218,6 @@ type DateChamp implements Champ {
La valeur du champ sous forme texte.
"""
stringValue: String
value: ISO8601DateTime
}
type DecimalNumberChamp implements Champ {

View file

@ -11,8 +11,14 @@ module Types
case object
when ::Champs::EngagementChamp, ::Champs::YesNoChamp, ::Champs::CheckboxChamp
Types::Champs::CheckboxChampType
when ::Champs::DateChamp, ::Champs::DatetimeChamp
when ::Champs::DateChamp
Types::Champs::DateChampType
when ::Champs::DatetimeChamp
if context.has_fragment?(:DatetimeChamp)
Types::Champs::DatetimeChampType
else
Types::Champs::DateChampType
end
when ::Champs::DossierLinkChamp
Types::Champs::DossierLinkChampType
when ::Champs::PieceJustificativeChamp

View file

@ -2,12 +2,19 @@ module Types::Champs
class DateChampType < Types::BaseObject
implements Types::ChampType
field :value, GraphQL::Types::ISO8601DateTime, null: true
field :value, GraphQL::Types::ISO8601DateTime, "La valeur du champ formaté en ISO8601 (DateTime).", null: true, deprecation_reason: "Utilisez le champ 'date' ou le fragment 'DatetimeChamp' à la place."
field :date, GraphQL::Types::ISO8601Date, "La valeur du champ formaté en ISO8601 (Date).", null: true
def value
if object.value.present?
Time.zone.parse(object.value)
end
end
def date
if object.value.present?
Date.parse(object.value)
end
end
end
end

View file

@ -0,0 +1,13 @@
module Types::Champs
class DatetimeChampType < Types::BaseObject
implements Types::ChampType
field :datetime, GraphQL::Types::ISO8601DateTime, "La valeur du champ formaté en ISO8601 (DateTime).", null: true
def datetime
if object.value.present?
Time.zone.parse(object.value)
end
end
end
end

View file

@ -409,6 +409,7 @@ describe API::V2::GraphqlController do
}
}"
end
context "in the nominal case" do
it "should be returned" do
expect(gql_errors).to eq(nil)
@ -497,6 +498,93 @@ describe API::V2::GraphqlController do
end
end
end
context "champs" do
let(:procedure) { create(:procedure, :published, :for_individual, administrateurs: [admin], types_de_champ: [type_de_champ_date, type_de_champ_datetime]) }
let(:dossier) { create(:dossier, :en_construction, procedure: procedure) }
let(:type_de_champ_date) { build(:type_de_champ_date) }
let(:type_de_champ_datetime) { build(:type_de_champ_datetime) }
let(:champ_date) { dossier.champs.first }
let(:champ_datetime) { dossier.champs.second }
before do
champ_date.update(value: '2019-07-10')
champ_datetime.update(value: '15/09/1962 15:35')
end
context "with Date" do
let(:query) do
"{
dossier(number: #{dossier.id}) {
champs {
id
label
... on DateChamp {
value
}
}
}
}"
end
it "should be returned" do
expect(gql_errors).to eq(nil)
expect(gql_data).to eq(dossier: {
champs: [
{
id: champ_date.to_typed_id,
label: champ_date.libelle,
value: '2019-07-10T00:00:00+02:00'
},
{
id: champ_datetime.to_typed_id,
label: champ_datetime.libelle,
value: '1962-09-15T15:35:00+01:00'
}
]
})
end
end
context "with Datetime" do
let(:query) do
"{
dossier(number: #{dossier.id}) {
champs {
id
label
... on DateChamp {
value
date
}
... on DatetimeChamp {
datetime
}
}
}
}"
end
it "should be returned" do
expect(gql_errors).to eq(nil)
expect(gql_data).to eq(dossier: {
champs: [
{
id: champ_date.to_typed_id,
label: champ_date.libelle,
value: '2019-07-10T00:00:00+02:00',
date: '2019-07-10'
},
{
id: champ_datetime.to_typed_id,
label: champ_datetime.libelle,
datetime: '1962-09-15T15:35:00+01:00'
}
]
})
end
end
end
end
context "groupeInstructeur" do