base_subscription.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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::MyScubscription.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')
  32. ::Gql::ZammadSchema.subscriptions.trigger(
  33. graphql_field_name,
  34. arguments,
  35. object,
  36. scope: scope
  37. )
  38. end
  39. #
  40. # Default subscribe implementation that returns nothing. For this to work, all fields must have null: true.
  41. # Otherwise, you can provide a subscribe method in the inheriting class.
  42. #
  43. def subscribe(...)
  44. {}
  45. end
  46. def no_update
  47. # Documentation suggests to cancel updates via 'NO_UPDATE', which does not seem to work:
  48. # NameError: uninitialized constant GraphQL::Schema::Subscription::NO_UPDATE
  49. :no_update
  50. end
  51. def self.register_in_schema(schema)
  52. schema.field graphql_field_name, resolver: self, broadcastable: broadcastable?
  53. end
  54. end
  55. end