zammad_schema.rb 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (C) 2012-2022 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. # Set maximum page size and depth to protect the system.
  11. # Values may need to be adjusted in future.
  12. default_max_page_size 1000
  13. max_depth 10
  14. # Union and Interface Resolution
  15. def self.resolve_type(_abstract_type, obj, _ctx)
  16. "Gql::Types::#{obj.class.name}Type".constantize
  17. rescue
  18. raise(GraphQL::RequiredImplementationMissingError)
  19. end
  20. # Relay-style Object Identification:
  21. # Return a string UUID for `object`
  22. def self.id_from_object(object, _type_definition, _query_ctx)
  23. object.to_gid.to_param
  24. end
  25. # Given a string UUID, find the object
  26. def self.object_from_id(id, _query_ctx)
  27. GlobalID.find(id)
  28. end
  29. def self.unauthorized_object(error)
  30. raise Exceptions::Forbidden, error.message # Add a top-level error to the response instead of returning nil.
  31. end
  32. def self.unauthorized_field(error)
  33. raise Exceptions::Forbidden, error.message # Add a top-level error to the response instead of returning nil.
  34. end
  35. # Post-process errors and enrich them with meta information for processing on the client side.
  36. rescue_from(StandardError) do |err, _obj, _args, _ctx, _field|
  37. # Re-throw built-in errors that point to programming errors rather than problems with input or data - causes GraphQL processing to be aborted.
  38. [ArgumentError, IndexError, NameError, RangeError, RegexpError, SystemCallError, ThreadError, TypeError, ZeroDivisionError].each do |klass|
  39. raise err if err.is_a? klass
  40. end
  41. extensions = {
  42. type: err.class.name,
  43. }
  44. if Rails.env.development? || Rails.env.test?
  45. extensions[:backtrace] = Rails.backtrace_cleaner.clean(err.backtrace)
  46. end
  47. # We need to throw an ExecutionError, all others would cause the GraphQL processing to die.
  48. raise GraphQL::ExecutionError.new(err.message, extensions: extensions)
  49. end
  50. end
  51. # Temporary Hack: only process trigger events if ActionCable is enabled.
  52. # TODO: Remove when this switch is not needed any more.
  53. module GraphQL
  54. class Subscriptions
  55. alias orig_trigger trigger
  56. def trigger(...)
  57. return if ENV['ENABLE_EXPERIMENTAL_MOBILE_FRONTEND'] != 'true'
  58. orig_trigger(...)
  59. end
  60. end
  61. end