zammad_schema.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Gql::ZammadSchema < GraphQL::Schema
  3. mutation Gql::EntryPoints::Mutations
  4. query Gql::EntryPoints::Queries
  5. subscription Gql::EntryPoints::Subscriptions
  6. context_class Gql::Context::CurrentUserAware
  7. use GraphQL::Subscriptions::ActionCableSubscriptions, broadcast: true, default_broadcastable: false
  8. # Enable batch loading
  9. use GraphQL::Batch
  10. description 'This is the Zammad GraphQL API'
  11. # Set maximum page size and depth to protect the system.
  12. # Values may need to be adjusted in future.
  13. default_max_page_size 1000
  14. # The GraphQL introspection query has a depth of 13, so allow that in the development env.
  15. max_depth Rails.env.eql?('development') ? 13 : 10
  16. TYPE_MAP = {
  17. ::Store => ::Gql::Types::StoredFileType
  18. }.freeze
  19. # Union and Interface Resolution
  20. def self.resolve_type(_abstract_type, obj, _ctx)
  21. TYPE_MAP[obj.class] || "Gql::Types::#{obj.class.name}Type".constantize
  22. rescue
  23. raise GraphQL::RequiredImplementationMissingError, "Cannot resolve type for '#{obj.class.name}'."
  24. end
  25. # Relay-style Object Identification:
  26. # Return a string UUID for the internal ID.
  27. def self.id_from_internal_id(klass, internal_id)
  28. GlobalID.new(::URI::GID.build(app: GlobalID.app, model_name: klass.to_s, model_id: internal_id)).to_s
  29. end
  30. # Return a string UUID for `object`
  31. def self.id_from_object(object, _type_definition = nil, _query_ctx = nil)
  32. object.to_global_id.to_s
  33. end
  34. # Given a string UUID, find the object.
  35. def self.object_from_id(id, _query_ctx = nil, type: ActiveRecord::Base)
  36. GlobalID.find(id, only: type)
  37. end
  38. # Find the object, but also ensure its type and that it was actually found.
  39. def self.verified_object_from_id(id, type:)
  40. object_from_id(id, type: type) || raise(ActiveRecord::RecordNotFound, "Could not find #{type} #{id}")
  41. end
  42. # Like .verified_object_from_id, but with additional Pundit autorization.
  43. # This is only needed for objects where no validation takes place through their GraphQL type.
  44. def self.authorized_object_from_id(id, type:, user:, query: :show?)
  45. verified_object_from_id(id, type: type).tap do |object|
  46. Pundit.authorize user, object, query
  47. rescue Pundit::NotAuthorizedError => e
  48. # Map Pundit errors since we are not in a GraphQL built-in authorization context here.
  49. raise Exceptions::Forbidden, e.message
  50. end
  51. end
  52. def self.unauthorized_object(error)
  53. raise Exceptions::Forbidden, error.message # Add a top-level error to the response instead of returning nil.
  54. end
  55. def self.unauthorized_field(error)
  56. raise Exceptions::Forbidden, error.message # Add a top-level error to the response instead of returning nil.
  57. end
  58. RETHROWABLE_ERRORS = [ArgumentError, IndexError, NameError, RangeError, RegexpError, SystemCallError, ThreadError, TypeError, ZeroDivisionError].freeze
  59. # Post-process errors and enrich them with meta information for processing on the client side.
  60. rescue_from(StandardError) do |err, _obj, _args, ctx, field|
  61. if field&.path&.start_with?('Mutations.')
  62. user_locale = ctx.current_user?&.locale
  63. if err.is_a? ActiveRecord::RecordInvalid
  64. user_errors = err.record.errors.map { |e| { field: e.attribute.to_s.camelize(:lower), message: e.localized_full_message(locale: user_locale, no_field_name: true) } }
  65. next { errors: user_errors }
  66. end
  67. if err.is_a? ActiveRecord::RecordNotUnique
  68. next { errors: [ message: Translation.translate(user_locale, 'This object already exists.') ] }
  69. end
  70. end
  71. # Re-throw built-in errors that point to programming errors rather than problems with input or data - causes GraphQL processing to be aborted.
  72. RETHROWABLE_ERRORS.each do |klass|
  73. raise err if err.is_a? klass
  74. end
  75. extensions = {
  76. type: err.class.name,
  77. }
  78. if Rails.env.development? || Rails.env.test?
  79. extensions[:backtrace] = Rails.backtrace_cleaner.clean(err.backtrace)
  80. end
  81. # We need to throw an ExecutionError, all others would cause the GraphQL processing to die.
  82. raise GraphQL::ExecutionError.new(err.message, extensions: extensions)
  83. end
  84. end
  85. # Temporary Hack: only process trigger events if ActionCable is enabled.
  86. # TODO: Remove when this switch is not needed any more.
  87. module GraphQL
  88. class Subscriptions # rubocop:disable GraphQL/ObjectDescription
  89. if !method_defined?(:orig_trigger)
  90. alias orig_trigger trigger
  91. def trigger(...)
  92. return if ENV['ENABLE_EXPERIMENTAL_MOBILE_FRONTEND'] != 'true'
  93. orig_trigger(...)
  94. end
  95. end
  96. end
  97. end