base_subscription.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Subscriptions
  3. class BaseSubscription < GraphQL::Schema::Subscription
  4. include Gql::Concerns::HandlesAuthorization
  5. include Gql::Concerns::HasNestedGraphqlName
  6. object_class Gql::Types::BaseObject
  7. field_class Gql::Fields::BaseField
  8. argument_class Gql::Types::BaseArgument
  9. description 'Base class for all subscriptions'
  10. def self.authorize(_obj, ctx)
  11. ctx.current_user
  12. end
  13. # Add DSL to specify if a subscription is broadcastable.
  14. def self.broadcastable(broadcastable = nil)
  15. if broadcastable.nil?
  16. @broadcastable
  17. else
  18. @broadcastable = broadcastable
  19. end
  20. end
  21. def self.broadcastable?
  22. !!broadcastable
  23. end
  24. # Shortcut method to trigger a subscription. Just call:
  25. #
  26. # Gql::Subscriptions::MySubscription.trigger(
  27. # self, # object to pass as payload,
  28. # arguments: { 'filter' => arg }, # custom arguments
  29. # )
  30. def self.trigger(object, arguments: {}, scope: nil)
  31. return if Setting.get('import_mode') || Zammad::SafeMode.enabled?
  32. return if ENV['ENABLE_EXPERIMENTAL_MOBILE_FRONTEND'] != 'true'
  33. ::Gql::ZammadSchema.subscriptions.trigger(
  34. graphql_field_name,
  35. arguments,
  36. object,
  37. scope: scope
  38. )
  39. end
  40. #
  41. # Default subscribe implementation that returns nothing. For this to work, all fields must have null: true.
  42. # Otherwise, you can provide a subscribe method in the inheriting class.
  43. #
  44. def subscribe(...)
  45. {}
  46. end
  47. def no_update
  48. # Documentation suggests to cancel updates via 'NO_UPDATE', which does not seem to work:
  49. # NameError: uninitialized constant GraphQL::Schema::Subscription::NO_UPDATE
  50. :no_update
  51. end
  52. def self.register_in_schema(schema)
  53. schema.field graphql_field_name, resolver: self, broadcastable: broadcastable?
  54. end
  55. end
  56. end