zammad_schema.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. class Gql::ZammadSchema < GraphQL::Schema
  3. mutation(Gql::EntryPoints::Mutations)
  4. query(Gql::EntryPoints::Queries)
  5. # subscription(Gql::Types::SubscriptionType)
  6. # Enable batch loading
  7. use GraphQL::Batch
  8. # # Enable ActionCable and GraphQL connection
  9. # use GraphQL::Subscriptions::ActionCableSubscriptions
  10. # Union and Interface Resolution
  11. def self.resolve_type(_abstract_type, obj, _ctx)
  12. "Gql::Types::#{obj.class.name}Type".constantize
  13. rescue
  14. raise(GraphQL::RequiredImplementationMissingError)
  15. end
  16. # Relay-style Object Identification:
  17. # Return a string UUID for `object`
  18. def self.id_from_object(object, _type_definition, _query_ctx)
  19. object.to_gid.to_param
  20. end
  21. # Given a string UUID, find the object
  22. def self.object_from_id(id, _query_ctx)
  23. GlobalID.find(id)
  24. end
  25. def self.unauthorized_object(error)
  26. raise Exceptions::Forbidden, error.message # Add a top-level error to the response instead of returning nil.
  27. end
  28. def self.unauthorized_field(error)
  29. raise Exceptions::Forbidden, error.message # Add a top-level error to the response instead of returning nil.
  30. end
  31. # Post-process errors and enrich them with meta information for processing on the client side.
  32. rescue_from(StandardError) do |err, _obj, _args, _ctx, _field|
  33. # Re-throw built-in errors that point to programming errors rather than problems with input or data - causes GraphQL processing to be aborted.
  34. [ArgumentError, IndexError, NameError, RangeError, RegexpError, SystemCallError, ThreadError, TypeError, ZeroDivisionError].each do |klass|
  35. raise err if err.is_a? klass
  36. end
  37. extensions = {
  38. type: err.class.name,
  39. }
  40. if Rails.env.development? || Rails.env.test?
  41. extensions[:backtrace] = Rails.backtrace_cleaner.clean(err.backtrace)
  42. end
  43. # We need to throw an ExecutionError, all others would cause the GraphQL processing to die.
  44. raise GraphQL::ExecutionError.new(err.message, extensions: extensions)
  45. end
  46. end