2020-08-05 18:40:47 +02:00
|
|
|
class API::V2::Schema < GraphQL::Schema
|
2018-11-19 20:58:51 +01:00
|
|
|
default_max_page_size 100
|
2022-10-31 11:49:41 +01:00
|
|
|
default_page_size 100
|
|
|
|
# Disable max_complexity for now because of what looks like a bug in graphql gem.
|
|
|
|
# After some internal changes complexity for our avarage query went from < 300 to 25 000.
|
|
|
|
max_complexity nil
|
2018-11-19 20:58:51 +01:00
|
|
|
max_depth 15
|
|
|
|
|
2018-11-19 21:06:13 +01:00
|
|
|
query Types::QueryType
|
|
|
|
mutation Types::MutationType
|
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
context_class API::V2::Context
|
2020-12-18 11:16:18 +01:00
|
|
|
|
2018-11-20 22:59:51 +01:00
|
|
|
def self.id_from_object(object, type_definition, ctx)
|
2022-11-24 14:54:17 +01:00
|
|
|
if type_definition == Types::DemarcheDescriptorType
|
|
|
|
(object.is_a?(Procedure) ? object : object.procedure).to_typed_id
|
2023-03-21 13:29:51 +01:00
|
|
|
elsif type_definition == Types::DeletedDossierType
|
|
|
|
object.is_a?(DeletedDossier) ? object.to_typed_id : GraphQL::Schema::UniqueWithinType.encode('DeletedDossier', object.id)
|
2022-11-24 14:54:17 +01:00
|
|
|
elsif object.is_a?(Hash)
|
2022-05-04 11:34:47 +02:00
|
|
|
object[:id]
|
|
|
|
else
|
|
|
|
object.to_typed_id
|
|
|
|
end
|
2018-11-20 22:59:51 +01:00
|
|
|
end
|
|
|
|
|
2022-11-24 14:54:17 +01:00
|
|
|
def self.object_from_id(id, ctx)
|
2018-11-20 22:59:51 +01:00
|
|
|
ApplicationRecord.record_from_typed_id(id)
|
|
|
|
end
|
|
|
|
|
2022-11-24 14:54:17 +01:00
|
|
|
def self.resolve_type(type_definition, object, ctx)
|
|
|
|
case object
|
2018-11-20 22:59:51 +01:00
|
|
|
when Procedure
|
2022-11-24 14:54:17 +01:00
|
|
|
if type_definition == Types::DemarcheDescriptorType
|
|
|
|
type_definition
|
|
|
|
else
|
|
|
|
Types::DemarcheType
|
|
|
|
end
|
2018-11-20 22:59:51 +01:00
|
|
|
when Dossier
|
|
|
|
Types::DossierType
|
2018-11-23 18:54:51 +01:00
|
|
|
when Commentaire
|
|
|
|
Types::MessageType
|
2021-02-25 09:32:23 +01:00
|
|
|
when Instructeur, User, Expert
|
2018-11-20 22:59:51 +01:00
|
|
|
Types::ProfileType
|
2019-11-21 18:52:07 +01:00
|
|
|
when Individual
|
|
|
|
Types::PersonnePhysiqueType
|
|
|
|
when Etablissement
|
|
|
|
Types::PersonneMoraleType
|
2020-02-21 11:57:36 +01:00
|
|
|
when GroupeInstructeur
|
|
|
|
Types::GroupeInstructeurType
|
2018-11-20 22:59:51 +01:00
|
|
|
else
|
2023-04-18 09:48:38 +02:00
|
|
|
type_definition
|
2018-11-20 22:59:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-17 14:04:33 +01:00
|
|
|
orphan_types Types::Champs::AddressChampType,
|
|
|
|
Types::Champs::CarteChampType,
|
2019-08-28 11:25:41 +02:00
|
|
|
Types::Champs::CheckboxChampType,
|
2019-11-21 18:52:07 +01:00
|
|
|
Types::Champs::CiviliteChampType,
|
2021-11-30 19:44:07 +01:00
|
|
|
Types::Champs::CommuneChampType,
|
2019-08-28 11:25:41 +02:00
|
|
|
Types::Champs::DateChampType,
|
2020-12-18 11:21:03 +01:00
|
|
|
Types::Champs::DatetimeChampType,
|
2019-08-28 11:25:41 +02:00
|
|
|
Types::Champs::DecimalNumberChampType,
|
2022-12-21 09:24:37 +01:00
|
|
|
Types::Champs::DepartementChampType,
|
2019-08-28 11:25:41 +02:00
|
|
|
Types::Champs::DossierLinkChampType,
|
2023-01-19 09:43:19 +01:00
|
|
|
Types::Champs::EpciChampType,
|
2019-08-28 11:25:41 +02:00
|
|
|
Types::Champs::IntegerNumberChampType,
|
|
|
|
Types::Champs::LinkedDropDownListChampType,
|
|
|
|
Types::Champs::MultipleDropDownListChampType,
|
2022-12-21 17:35:05 +01:00
|
|
|
Types::Champs::PaysChampType,
|
2019-08-28 11:25:41 +02:00
|
|
|
Types::Champs::PieceJustificativeChampType,
|
2022-12-21 09:24:37 +01:00
|
|
|
Types::Champs::RegionChampType,
|
2019-08-28 11:25:41 +02:00
|
|
|
Types::Champs::RepetitionChampType,
|
|
|
|
Types::Champs::SiretChampType,
|
|
|
|
Types::Champs::TextChampType,
|
2020-09-24 12:02:14 +02:00
|
|
|
Types::Champs::TitreIdentiteChampType,
|
2019-08-28 11:25:41 +02:00
|
|
|
Types::GeoAreas::ParcelleCadastraleType,
|
2019-11-21 18:52:07 +01:00
|
|
|
Types::GeoAreas::SelectionUtilisateurType,
|
|
|
|
Types::PersonneMoraleType,
|
2023-04-04 16:58:47 +02:00
|
|
|
Types::PersonneMoraleIncompleteType,
|
2022-11-24 13:14:27 +01:00
|
|
|
Types::PersonnePhysiqueType,
|
|
|
|
Types::Champs::Descriptor::AddressChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::AnnuaireEducationChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::CarteChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::CheckboxChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::CiviliteChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::CnafChampDescriptorType,
|
2023-05-30 14:42:36 +02:00
|
|
|
Types::Champs::Descriptor::COJOChampDescriptorType,
|
2022-11-24 13:14:27 +01:00
|
|
|
Types::Champs::Descriptor::CommuneChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::DateChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::DatetimeChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::DecimalNumberChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::DepartementChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::DgfipChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::DossierLinkChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::DropDownListChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::EmailChampDescriptorType,
|
2023-01-19 09:43:19 +01:00
|
|
|
Types::Champs::Descriptor::EpciChampDescriptorType,
|
2022-11-24 13:14:27 +01:00
|
|
|
Types::Champs::Descriptor::ExplicationChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::HeaderSectionChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::IbanChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::IntegerNumberChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::LinkedDropDownListChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::MesriChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::MultipleDropDownListChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::NumberChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::PaysChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::PhoneChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::PieceJustificativeChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::PoleEmploiChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::RegionChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::RepetitionChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::RNAChampDescriptorType,
|
2023-10-27 14:22:00 +02:00
|
|
|
Types::Champs::Descriptor::RNFChampDescriptorType,
|
2022-11-24 13:14:27 +01:00
|
|
|
Types::Champs::Descriptor::SiretChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::TextareaChampDescriptorType,
|
2023-01-19 09:43:19 +01:00
|
|
|
Types::Champs::Descriptor::TextChampDescriptorType,
|
2022-11-24 13:14:27 +01:00
|
|
|
Types::Champs::Descriptor::TitreIdentiteChampDescriptorType,
|
2023-10-10 15:42:43 +02:00
|
|
|
Types::Champs::Descriptor::YesNoChampDescriptorType,
|
|
|
|
Types::Champs::Descriptor::ExpressionReguliereChampDescriptorType
|
2019-08-28 11:25:41 +02:00
|
|
|
|
2018-11-19 20:58:51 +01:00
|
|
|
def self.unauthorized_object(error)
|
|
|
|
# Add a top-level error to the response instead of returning nil:
|
|
|
|
raise GraphQL::ExecutionError.new("An object of type #{error.type.graphql_name} was hidden due to permissions", extensions: { code: :unauthorized })
|
|
|
|
end
|
|
|
|
|
2023-03-29 15:37:41 +02:00
|
|
|
def self.type_error(error, ctx)
|
|
|
|
# Capture type errors in Sentry. Thouse errors are our responsability and usually linked to
|
|
|
|
# instances of "bad data".
|
2023-04-04 14:45:22 +02:00
|
|
|
Sentry.capture_exception(error, extra: ctx.query_info)
|
2023-03-29 15:37:41 +02:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2023-04-18 10:08:18 +02:00
|
|
|
rescue_from(ActiveRecord::RecordNotFound) do |_error, _object, _args, _ctx, field|
|
|
|
|
raise GraphQL::ExecutionError.new("#{field.type.unwrap.graphql_name} not found", extensions: { code: :not_found })
|
|
|
|
end
|
|
|
|
|
2023-03-29 15:37:41 +02:00
|
|
|
class Timeout < GraphQL::Schema::Timeout
|
|
|
|
def handle_timeout(error, query)
|
2023-04-18 10:27:20 +02:00
|
|
|
error.extensions = { code: :timeout }
|
2023-03-29 15:37:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-02 10:11:29 +02:00
|
|
|
use Timeout, max_seconds: 30
|
2019-12-05 09:33:48 +01:00
|
|
|
use GraphQL::Batch
|
|
|
|
use GraphQL::Backtrace
|
2018-11-19 20:58:51 +01:00
|
|
|
|
|
|
|
if Rails.env.development?
|
2019-12-05 09:33:48 +01:00
|
|
|
class LogQueryDepth < GraphQL::Analysis::AST::QueryDepth
|
|
|
|
def result
|
|
|
|
Rails.logger.info("[GraphQL Query Depth] #{super}")
|
|
|
|
end
|
|
|
|
end
|
2018-11-19 20:58:51 +01:00
|
|
|
|
2019-12-05 09:33:48 +01:00
|
|
|
class LogQueryComplexity < GraphQL::Analysis::AST::QueryComplexity
|
|
|
|
def result
|
|
|
|
Rails.logger.info("[GraphQL Query Complexity] #{super}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
query_analyzer(LogQueryComplexity)
|
|
|
|
query_analyzer(LogQueryDepth)
|
|
|
|
end
|
2018-11-19 20:58:51 +01:00
|
|
|
end
|