From 37fdefae70574f70b7e80b8af3f421df683cd89d Mon Sep 17 00:00:00 2001 From: prog-supdex Date: Wed, 27 Sep 2023 09:55:56 +0200 Subject: [PATCH] fix rspecs, README and change TrigerJob --- README.md | 8 ++++---- lib/graphql-anycable.rb | 2 +- lib/graphql/adapters/delivery_adapter.rb | 2 +- lib/graphql/anycable/config.rb | 2 +- lib/graphql/anycable/railtie.rb | 5 +---- lib/graphql/{adapters => jobs}/trigger_job.rb | 2 +- .../subscriptions/anycable_subscriptions.rb | 2 +- spec/graphql/anycable_spec.rb | 6 +++--- spec/graphql/broadcast_spec.rb | 14 +++++++------- spec/{adapters => jobs}/trigger_job_spec.rb | 18 ++++++------------ 10 files changed, 26 insertions(+), 35 deletions(-) rename lib/graphql/{adapters => jobs}/trigger_job.rb (97%) rename spec/{adapters => jobs}/trigger_job_spec.rb (72%) diff --git a/README.md b/README.md index 3145807..eeb7a24 100644 --- a/README.md +++ b/README.md @@ -350,20 +350,20 @@ You have the next configuration # ... other configurations config.delivery_method = "inline" # the default value "inline", also can be "active_job" config.queue = "default" # the name of ActiveJob queue - config.job_class = "GraphQL::Adapters::TriggerJob" # the name executor job + config.job_class = "GraphQL::Jobs::TriggerJob" # the name executor job end ``` `delivery_method` can be either `inline` or `active_job`. `inline` means that delivering messaging will work sync. -`active_job` - It will adds delivering messages operations to `ActiveJob` with queue `default` and using job `GraphQL::Adapters::TriggerJob` +`active_job` - It will add delivering messages operations to `ActiveJob` with queue `default` and using job `GraphQL::Jobs::TriggerJob` -You can change the queue or job_class, buy changing it in configuration +You can change the queue or job_class by changing it in the configuration Or you can run code ```ruby -GraphQL::AnyCable.delivery_method("active_job", queue: "broadcasting", job_class: "GraphQL::Adapters::TriggerJob") +GraphQL::AnyCable.delivery_method("active_job", queue: "broadcasting", job_class: "GraphQL::Jobs::TriggerJob") ``` ## Testing applications which use `graphql-anycable` diff --git a/lib/graphql-anycable.rb b/lib/graphql-anycable.rb index 866e120..1c986ee 100644 --- a/lib/graphql-anycable.rb +++ b/lib/graphql-anycable.rb @@ -26,7 +26,7 @@ def self.stats(**options) Stats.new(**options).collect end - def self.delivery_method(kind, queue: "default", job_class: "GraphQL::Adapters::TriggerJob") + def self.delivery_method(kind, queue: "default", job_class: "GraphQL::Jobs::TriggerJob") config.delivery_method = kind config.queue = queue config.job_class = job_class diff --git a/lib/graphql/adapters/delivery_adapter.rb b/lib/graphql/adapters/delivery_adapter.rb index 62fba74..bcc95bb 100644 --- a/lib/graphql/adapters/delivery_adapter.rb +++ b/lib/graphql/adapters/delivery_adapter.rb @@ -35,7 +35,7 @@ def trigger(event_name, args, object, **elements) def executor_class_job custom_job_class = config.job_class - return Adapters::TriggerJob unless custom_job_class + return Jobs::TriggerJob unless custom_job_class custom_job_class.constantize end diff --git a/lib/graphql/anycable/config.rb b/lib/graphql/anycable/config.rb index cba1c19..a4204d8 100644 --- a/lib/graphql/anycable/config.rb +++ b/lib/graphql/anycable/config.rb @@ -13,7 +13,7 @@ class Config < Anyway::Config attr_config use_client_provided_uniq_id: true attr_config redis_prefix: "graphql" # Here, we set clear redis_prefix without any hyphen. The hyphen is added at the end of this value on our side. - attr_config delivery_method: "inline", queue: "default", job_class: "GraphQL::Adapters::TriggerJob" + attr_config delivery_method: "inline", queue: "default", job_class: "GraphQL::Jobs::TriggerJob" end end end diff --git a/lib/graphql/anycable/railtie.rb b/lib/graphql/anycable/railtie.rb index 82d7ace..1470698 100644 --- a/lib/graphql/anycable/railtie.rb +++ b/lib/graphql/anycable/railtie.rb @@ -6,12 +6,9 @@ module GraphQL module AnyCable class Railtie < ::Rails::Railtie initializer 'graphql_anycable.load_trigger_job' do - #ActiveSupport.on_load(:active_job) do - require "graphql/adapters/trigger_job" if defined?(ActiveJob::Base) - #end + require "graphql/jobs/trigger_job" if defined?(ActiveJob::Base) end - rake_tasks do path = File.expand_path(__dir__) Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f } diff --git a/lib/graphql/adapters/trigger_job.rb b/lib/graphql/jobs/trigger_job.rb similarity index 97% rename from lib/graphql/adapters/trigger_job.rb rename to lib/graphql/jobs/trigger_job.rb index d231624..609b8d2 100644 --- a/lib/graphql/adapters/trigger_job.rb +++ b/lib/graphql/jobs/trigger_job.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module GraphQL - module Adapters + module Jobs class TriggerJob < ActiveJob::Base def perform(payload, execute_method, event_name, args = {}, object = nil, options = {}) schema = schema_parse(payload) diff --git a/lib/graphql/subscriptions/anycable_subscriptions.rb b/lib/graphql/subscriptions/anycable_subscriptions.rb index 19aa5fc..2d25153 100644 --- a/lib/graphql/subscriptions/anycable_subscriptions.rb +++ b/lib/graphql/subscriptions/anycable_subscriptions.rb @@ -55,9 +55,9 @@ class AnyCableSubscriptions < GraphQL::Subscriptions extend Forwardable def_delegators :"GraphQL::AnyCable", :redis, :config - alias_method :trigger_sync, :trigger attr_reader :collected_arguments + alias_method :trigger_sync, :trigger SUBSCRIPTION_PREFIX = "subscription:" # HASH: Stores subscription data: query, context, … FINGERPRINTS_PREFIX = "fingerprints:" # ZSET: To get fingerprints by topic diff --git a/spec/graphql/anycable_spec.rb b/spec/graphql/anycable_spec.rb index cf3649b..934a0e8 100644 --- a/spec/graphql/anycable_spec.rb +++ b/spec/graphql/anycable_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "active_job" -require "graphql/adapters/trigger_job" +require "graphql/jobs/trigger_job" RSpec.describe GraphQL::AnyCable do subject do @@ -286,13 +286,13 @@ after do config.delivery_method = "inline" config.queue = "default" - config.job_class = "GraphQL::Adapters::TriggerJob" + config.job_class = "GraphQL::Jobs::TriggerJob" end it "changes config" do expect(config.delivery_method).to eq("inline") expect(config.queue).to eq("default") - expect(config.job_class).to eq("GraphQL::Adapters::TriggerJob") + expect(config.job_class).to eq("GraphQL::Jobs::TriggerJob") described_class.delivery_method("active_job", queue: "test", job_class: "CustomJob") diff --git a/spec/graphql/broadcast_spec.rb b/spec/graphql/broadcast_spec.rb index 4a642c4..ed3ab6a 100644 --- a/spec/graphql/broadcast_spec.rb +++ b/spec/graphql/broadcast_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "active_job" -require "graphql/adapters/trigger_job" +require "graphql/jobs/trigger_job" RSpec.describe "Broadcasting" do include_context 'Global ID' @@ -67,7 +67,7 @@ def subscribe(query) it "uses broadcasting to resolve query only once" do 2.times { subscribe(query) } - expect_any_instance_of(GraphQL::Adapters::TriggerJob).to receive(:perform).and_call_original + expect_any_instance_of(GraphQL::Jobs::TriggerJob).to receive(:perform).and_call_original BroadcastSchema.subscriptions.trigger(:post_created, {}, object) @@ -86,7 +86,7 @@ def subscribe(query) it "resolves query for every client" do 2.times { subscribe(query) } - expect_any_instance_of(GraphQL::Adapters::TriggerJob).to receive(:perform).and_call_original + expect_any_instance_of(GraphQL::Jobs::TriggerJob).to receive(:perform).and_call_original BroadcastSchema.subscriptions.trigger(:post_created, {}, object) expect(object).to have_received(:title).twice @@ -108,7 +108,7 @@ def subscribe(query) redis.keys("graphql-subscription:*").last.tap(&redis.method(:del)) expect(redis.keys("graphql-subscription:*").size).to eq(2) - expect_any_instance_of(GraphQL::Adapters::TriggerJob).to receive(:perform).and_call_original + expect_any_instance_of(GraphQL::Jobs::TriggerJob).to receive(:perform).and_call_original expect { BroadcastSchema.subscriptions.trigger(:post_created, {}, object) }.not_to raise_error expect(object).to have_received(:title).once @@ -141,7 +141,7 @@ def subscribe(query) it "uses broadcasting to resolve query only once" do 2.times { subscribe(query) } - expect_any_instance_of(GraphQL::Adapters::TriggerJob).to_not receive(:perform) + expect_any_instance_of(GraphQL::Jobs::TriggerJob).to_not receive(:perform) BroadcastSchema.subscriptions.trigger(:post_created, {}, object) expect(object).to have_received(:title).once @@ -159,7 +159,7 @@ def subscribe(query) it "resolves query for every client" do 2.times { subscribe(query) } - expect_any_instance_of(GraphQL::Adapters::TriggerJob).to_not receive(:perform) + expect_any_instance_of(GraphQL::Jobs::TriggerJob).to_not receive(:perform) BroadcastSchema.subscriptions.trigger(:post_created, {}, object) expect(object).to have_received(:title).twice @@ -181,7 +181,7 @@ def subscribe(query) redis.keys("graphql-subscription:*").last.tap(&redis.method(:del)) expect(redis.keys("graphql-subscription:*").size).to eq(2) - expect_any_instance_of(GraphQL::Adapters::TriggerJob).to_not receive(:perform) + expect_any_instance_of(GraphQL::Jobs::TriggerJob).to_not receive(:perform) expect { BroadcastSchema.subscriptions.trigger(:post_created, {}, object) }.not_to raise_error expect(object).to have_received(:title).once diff --git a/spec/adapters/trigger_job_spec.rb b/spec/jobs/trigger_job_spec.rb similarity index 72% rename from spec/adapters/trigger_job_spec.rb rename to spec/jobs/trigger_job_spec.rb index d7c8a08..7ffbf76 100644 --- a/spec/adapters/trigger_job_spec.rb +++ b/spec/jobs/trigger_job_spec.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true require "active_job" -require "graphql/adapters/trigger_job" +require "graphql/jobs/trigger_job" -RSpec.describe GraphQL::Adapters::TriggerJob do +RSpec.describe GraphQL::Jobs::TriggerJob do subject(:job) { described_class.perform_later(*job_payload) } subject(:trigger_changes) { AnycableSchema.subscriptions.trigger(*trigger_sync_arguments) } @@ -63,11 +63,8 @@ end it "executes AnyCableSubscriptions" do - expect_any_instance_of(GraphQL::Subscriptions::AnyCableSubscriptions) - .to receive(:trigger_sync).with(*trigger_sync_arguments) - - expect_any_instance_of(GraphQL::Adapters::TriggerJob).to receive(:perform).and_call_original - expect(GraphQL::Adapters::TriggerJob).to receive(:set).with(queue: "default").and_call_original + expect_any_instance_of(GraphQL::Jobs::TriggerJob).to receive(:perform).and_call_original + expect(GraphQL::Jobs::TriggerJob).to receive(:set).with(queue: "default").and_call_original trigger_changes end @@ -83,11 +80,8 @@ end it "executes AnyCableSubscriptions" do - expect_any_instance_of(GraphQL::Subscriptions::AnyCableSubscriptions) - .to receive(:trigger_sync).with(*trigger_sync_arguments) - - expect_any_instance_of(GraphQL::Adapters::TriggerJob).to receive(:perform).and_call_original - expect(GraphQL::Adapters::TriggerJob).to receive(:set).with(queue: "test").and_call_original + expect_any_instance_of(GraphQL::Jobs::TriggerJob).to receive(:perform).and_call_original + expect(GraphQL::Jobs::TriggerJob).to receive(:set).with(queue: "test").and_call_original trigger_changes end