feat(api): PieceJustificativeChamp: deprecate file in favor of files

This commit is contained in:
Colin Darie 2022-11-07 14:24:28 +01:00
parent 6a0379086d
commit 1138604748
4 changed files with 63 additions and 17 deletions

View file

@ -6,14 +6,18 @@ module Extensions
attr_reader :attachment_assoc
def apply
# Here we try to define the attachment name:
# Here we try to define the ActiveRecord association name:
# - it could be set explicitly via extension options
# - or we imply that is the same as the field name w/o "_url"
# suffix (e.g., "avatar_url" => "avatar")
attachment = options&.[](:attachment) || field.original_name.to_s.sub(/_url$/, "")
# that's the name of the Active Record association
@attachment_assoc = "#{attachment}_attachment"
@attachment_assoc = if options.key?(:attachment)
"#{options[:attachment]}_attachment"
elsif options.key?(:attachments)
"#{options[:attachments]}_attachments"
else
attachment = field.original_name.to_s.sub(/_url$/, "")
"#{attachment}_attachment"
end
end
# This method resolves (as it states) the field itself
@ -28,8 +32,26 @@ module Extensions
# This method is called if the result of the `resolve`
# is a lazy value (e.g., a Promise like in our case)
def after_resolve(value:, **_rest)
if value&.virus_scanner&.safe? || value&.virus_scanner&.pending?
value
if value.respond_to?(:map)
attachments = value.map { after_resolve_attachment(_1) }
if options[:flat_first]
attachments.first
else
attachments
end
else
after_resolve_attachment(value)
end
end
private
def after_resolve_attachment(attachment)
return unless attachment
if attachment.virus_scanner.safe? || attachment.virus_scanner.pending?
attachment
end
end
end

View file

@ -2028,7 +2028,8 @@ type PersonnePhysique implements Demandeur {
}
type PieceJustificativeChamp implements Champ {
file: File
file: File @deprecated(reason: "Utilisez le champ `files` à la place.")
files: [File!]
id: ID!
"""
@ -2429,4 +2430,4 @@ type ValidationError {
A description of the error
"""
message: String!
}
}

View file

@ -2,8 +2,12 @@ module Types::Champs
class PieceJustificativeChampType < Types::BaseObject
implements Types::ChampType
field :file, Types::File, null: true, extensions: [
{ Extensions::Attachment => { attachment: :piece_justificative_file } }
field :file, Types::File, null: true, deprecation_reason: "Utilisez le champ `files` à la place.", extensions: [
{ Extensions::Attachment => { attachments: :piece_justificative_file, flat_first: true } }
]
field :files, [Types::File], null: true, extensions: [
{ Extensions::Attachment => { attachments: :piece_justificative_file } }
]
end
end

View file

@ -755,11 +755,11 @@ describe API::V2::GraphqlController do
end
end
describe "champ" do
describe "champ piece_justificative" do
let(:champ) { create(:champ_piece_justificative, dossier: dossier) }
let(:byte_size) { 2712286911 }
context "byteSize" do
context "with deprecated file field" do
let(:query) do
"{
dossier(number: #{dossier.id}) {
@ -778,9 +778,28 @@ describe API::V2::GraphqlController do
}
end
context "byteSize" do
let(:query) do
"{
dossier(number: #{dossier.id}) {
champs(id: \"#{champ.to_typed_id}\") {
... on PieceJustificativeChamp {
files { byteSize }
}
}
}
}"
end
it {
expect(gql_errors).to be_nil
expect(gql_data).to eq(dossier: { champs: [{ files: [{ byteSize: 4 }] }] })
}
end
context "when the file is really big" do
before do
champ.piece_justificative_file.blob.update(byte_size: byte_size)
champ.piece_justificative_file.first.blob.update(byte_size: byte_size)
end
context "byteSize" do
@ -789,7 +808,7 @@ describe API::V2::GraphqlController do
dossier(number: #{dossier.id}) {
champs(id: \"#{champ.to_typed_id}\") {
... on PieceJustificativeChamp {
file { byteSize }
files { byteSize }
}
}
}
@ -807,7 +826,7 @@ describe API::V2::GraphqlController do
dossier(number: #{dossier.id}) {
champs(id: \"#{champ.to_typed_id}\") {
... on PieceJustificativeChamp {
file { byteSizeBigInt }
files { byteSizeBigInt }
}
}
}
@ -816,7 +835,7 @@ describe API::V2::GraphqlController do
it {
expect(gql_errors).to be_nil
expect(gql_data).to eq(dossier: { champs: [{ file: { byteSizeBigInt: '2712286911' } }] })
expect(gql_data).to eq(dossier: { champs: [{ files: [{ byteSizeBigInt: '2712286911' }] }] })
}
end
end