Add id argument to most collections on dossier
This commit is contained in:
parent
0bd8721776
commit
bb072ba9e9
6 changed files with 92 additions and 19 deletions
27
app/graphql/loaders/champ.rb
Normal file
27
app/graphql/loaders/champ.rb
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
# references:
|
||||||
|
# https://github.com/Shopify/graphql-batch/blob/master/examples/record_loader.rb
|
||||||
|
|
||||||
|
module Loaders
|
||||||
|
class Champ < GraphQL::Batch::Loader
|
||||||
|
def initialize(dossier, private: false)
|
||||||
|
@where = { dossier: dossier, private: private }
|
||||||
|
end
|
||||||
|
|
||||||
|
def load(key)
|
||||||
|
super(key.to_i)
|
||||||
|
end
|
||||||
|
|
||||||
|
def perform(keys)
|
||||||
|
query(keys).each { |record| fulfill(record.stable_id, [record].compact) }
|
||||||
|
keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def query(keys)
|
||||||
|
::Champ.where(@where)
|
||||||
|
.includes(:type_de_champ)
|
||||||
|
.where(types_de_champ: { stable_id: keys })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,11 +3,13 @@
|
||||||
|
|
||||||
module Loaders
|
module Loaders
|
||||||
class Record < GraphQL::Batch::Loader
|
class Record < GraphQL::Batch::Loader
|
||||||
def initialize(model, column: model.primary_key, where: nil)
|
def initialize(model, column: model.primary_key, where: nil, includes: nil, array: false)
|
||||||
@model = model
|
@model = model
|
||||||
@column = column.to_s
|
@column = column.to_s
|
||||||
@column_type = model.type_for_attribute(@column)
|
@column_type = model.type_for_attribute(@column)
|
||||||
@where = where
|
@where = where
|
||||||
|
@includes = includes
|
||||||
|
@array = array
|
||||||
end
|
end
|
||||||
|
|
||||||
def load(key)
|
def load(key)
|
||||||
|
@ -15,7 +17,10 @@ module Loaders
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform(keys)
|
def perform(keys)
|
||||||
query(keys).each { |record| fulfill(record.public_send(@column), record) }
|
query(keys).each do |record|
|
||||||
|
fulfilled_value = @array ? [record].compact : record
|
||||||
|
fulfill(record.public_send(@column), fulfilled_value)
|
||||||
|
end
|
||||||
keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
|
keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -24,6 +29,7 @@ module Loaders
|
||||||
def query(keys)
|
def query(keys)
|
||||||
scope = @model
|
scope = @model
|
||||||
scope = scope.where(@where) if @where
|
scope = scope.where(@where) if @where
|
||||||
|
scope = scope.includes(@includes) if @includes
|
||||||
scope.where(@column => keys)
|
scope.where(@column => keys)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -508,15 +508,15 @@ type DirectUpload {
|
||||||
Un dossier
|
Un dossier
|
||||||
"""
|
"""
|
||||||
type Dossier {
|
type Dossier {
|
||||||
annotations: [Champ!]!
|
annotations(id: ID): [Champ!]!
|
||||||
archived: Boolean!
|
archived: Boolean!
|
||||||
|
|
||||||
"""
|
"""
|
||||||
L’URL de l’attestation au format PDF.
|
L’URL de l’attestation au format PDF.
|
||||||
"""
|
"""
|
||||||
attestation: File
|
attestation: File
|
||||||
avis: [Avis!]!
|
avis(id: ID): [Avis!]!
|
||||||
champs: [Champ!]!
|
champs(id: ID): [Champ!]!
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Date de la dernière modification.
|
Date de la dernière modification.
|
||||||
|
@ -546,7 +546,7 @@ type Dossier {
|
||||||
groupeInstructeur: GroupeInstructeur!
|
groupeInstructeur: GroupeInstructeur!
|
||||||
id: ID!
|
id: ID!
|
||||||
instructeurs: [Profile!]!
|
instructeurs: [Profile!]!
|
||||||
messages: [Message!]!
|
messages(id: ID): [Message!]!
|
||||||
motivation: String
|
motivation: String
|
||||||
motivationAttachment: File
|
motivationAttachment: File
|
||||||
|
|
||||||
|
|
|
@ -35,11 +35,19 @@ module Types
|
||||||
field :demandeur, Types::DemandeurType, null: false
|
field :demandeur, Types::DemandeurType, null: false
|
||||||
|
|
||||||
field :instructeurs, [Types::ProfileType], null: false
|
field :instructeurs, [Types::ProfileType], null: false
|
||||||
field :messages, [Types::MessageType], null: false
|
|
||||||
field :avis, [Types::AvisType], null: false
|
|
||||||
|
|
||||||
field :champs, [Types::ChampType], null: false
|
field :messages, [Types::MessageType], null: false do
|
||||||
field :annotations, [Types::ChampType], null: false
|
argument :id, ID, required: false
|
||||||
|
end
|
||||||
|
field :avis, [Types::AvisType], null: false do
|
||||||
|
argument :id, ID, required: false
|
||||||
|
end
|
||||||
|
field :champs, [Types::ChampType], null: false do
|
||||||
|
argument :id, ID, required: false
|
||||||
|
end
|
||||||
|
field :annotations, [Types::ChampType], null: false do
|
||||||
|
argument :id, ID, required: false
|
||||||
|
end
|
||||||
|
|
||||||
def state
|
def state
|
||||||
object.state
|
object.state
|
||||||
|
@ -69,20 +77,44 @@ module Types
|
||||||
Loaders::Association.for(object.class, :followers_instructeurs).load(object)
|
Loaders::Association.for(object.class, :followers_instructeurs).load(object)
|
||||||
end
|
end
|
||||||
|
|
||||||
def messages
|
def messages(id: nil)
|
||||||
Loaders::Association.for(object.class, commentaires: [:instructeur, :user]).load(object)
|
if id.present?
|
||||||
|
Loaders::Record
|
||||||
|
.for(Commentaire, where: { dossier: object }, includes: [:instructeur, :user], array: true)
|
||||||
|
.load(ApplicationRecord.id_from_typed_id(id))
|
||||||
|
else
|
||||||
|
Loaders::Association.for(object.class, commentaires: [:instructeur, :user]).load(object)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def avis
|
def avis(id: nil)
|
||||||
Loaders::Association.for(object.class, avis: [:instructeur, :claimant]).load(object)
|
if id.present?
|
||||||
|
Loaders::Record
|
||||||
|
.for(Avis, where: { dossier: object }, includes: [:instructeur, :claimant], array: true)
|
||||||
|
.load(ApplicationRecord.id_from_typed_id(id))
|
||||||
|
else
|
||||||
|
Loaders::Association.for(object.class, avis: [:instructeur, :claimant]).load(object)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def champs
|
def champs(id: nil)
|
||||||
Loaders::Association.for(object.class, champs: [:type_de_champ]).load(object)
|
if id.present?
|
||||||
|
Loaders::Champ
|
||||||
|
.for(object, private: false)
|
||||||
|
.load(ApplicationRecord.id_from_typed_id(id))
|
||||||
|
else
|
||||||
|
Loaders::Association.for(object.class, champs: :type_de_champ).load(object)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def annotations
|
def annotations(id: nil)
|
||||||
Loaders::Association.for(object.class, champs_private: [:type_de_champ]).load(object)
|
if id.present?
|
||||||
|
Loaders::Champ
|
||||||
|
.for(object, private: true)
|
||||||
|
.load(ApplicationRecord.id_from_typed_id(id))
|
||||||
|
else
|
||||||
|
Loaders::Association.for(object.class, champs_private: :type_de_champ).load(object)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def pdf
|
def pdf
|
||||||
|
|
|
@ -19,7 +19,11 @@ module Types
|
||||||
end
|
end
|
||||||
|
|
||||||
def dossier(number:)
|
def dossier(number:)
|
||||||
Dossier.state_not_brouillon.for_api_v2.find(number)
|
if context.internal_use?
|
||||||
|
Dossier.state_not_brouillon.with_discarded.for_api_v2.find(number)
|
||||||
|
else
|
||||||
|
Dossier.state_not_brouillon.for_api_v2.find(number)
|
||||||
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
raise GraphQL::ExecutionError.new(e.message, extensions: { code: :not_found })
|
raise GraphQL::ExecutionError.new(e.message, extensions: { code: :not_found })
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,6 +13,10 @@ class ApplicationRecord < ActiveRecord::Base
|
||||||
raise ActiveRecord::RecordNotFound, e.message
|
raise ActiveRecord::RecordNotFound, e.message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.id_from_typed_id(id)
|
||||||
|
GraphQL::Schema::UniqueWithinType.decode(id)[1]
|
||||||
|
end
|
||||||
|
|
||||||
def to_typed_id
|
def to_typed_id
|
||||||
GraphQL::Schema::UniqueWithinType.encode(self.class.name, id)
|
GraphQL::Schema::UniqueWithinType.encode(self.class.name, id)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue