base_subscription.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (C) 2012-2024 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 Zammad::SafeMode.enabled?
  32. # Do not trigger subscriptions in import mode,
  33. # except for configUpdates, otherwise it's not possible to finish the setup (e.g. an import).
  34. return if Setting.get('import_mode') == true && graphql_field_name != :configUpdates
  35. ::Gql::ZammadSchema.subscriptions.trigger(
  36. graphql_field_name,
  37. arguments,
  38. object,
  39. scope: scope
  40. )
  41. end
  42. #
  43. # Default subscribe implementation that returns nothing. For this to work, all fields must have null: true.
  44. # Otherwise, you can provide a subscribe method in the inheriting class.
  45. #
  46. def subscribe(...)
  47. {}
  48. end
  49. def no_update
  50. # Documentation suggests to cancel updates via 'NO_UPDATE', which does not seem to work:
  51. # NameError: uninitialized constant GraphQL::Schema::Subscription::NO_UPDATE
  52. :no_update
  53. end
  54. def self.register_in_schema(schema)
  55. schema.field graphql_field_name, resolver: self, broadcastable: broadcastable?
  56. end
  57. end
  58. end