Last active
June 10, 2024 13:07
-
-
Save rmosolgo/e8d196b77e4a212e28bae5442f7357d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This module will add a method | |
# to clean up empty topics: | |
module RemoveEmptyTopicsStorageExtension | |
def remove_empty_topics | |
all_topics, _topics_count = topics(limit: nil, offset: nil) | |
topics_to_remove = all_topics.select { |t| t.subscriptions_count == 0 } | |
if topics_to_remove.any? | |
topic_names = topics_to_remove.map(&:name) | |
fingerprint_key = topic_names.map { |t| fingerprints_key(t) } | |
with_redis do |r| | |
r.del(fingerprint_key) | |
r.srem(all_topics_key, topic_names) | |
end | |
end | |
end | |
end | |
# Add the method to GraphQL-Pro's storage class: | |
GraphQL::Pro::Subscriptions::RedisStorage.include(RemoveEmptyTopicsStorageExtension) | |
# Call the method on your schema's subscriptions system: | |
MySchema.subscriptions.instance_variable_get(:@storage).remove_empty_topics |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment