Skip to content

Commit

Permalink
fix rspecs, README and change TrigerJob
Browse files Browse the repository at this point in the history
  • Loading branch information
prog-supdex committed Sep 27, 2023
1 parent e94d36f commit 1dee021
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 36 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql-anycable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/adapters/delivery_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion lib/graphql/anycable/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module GraphQL
module AnyCable
class Config < Anyway::Config
ALLOWED_DELIVERY_METHODS = %w[inline active_job].freeze

config_name :graphql_anycable
env_prefix :graphql_anycable

Expand All @@ -13,7 +15,25 @@ 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"

def job_class=(value)
if value.nil? || value == ""
raise_validation_error "job_class can not be empty"
end

super
end

def delivery_method=(value)
unless ALLOWED_DELIVERY_METHODS.include?(value)
error_text = "Unknown delivery method - #{value}. Allowed methods: #{ALLOWED_DELIVERY_METHODS.join(", ")}"

raise_validation_error error_text
end

super
end
end
end
end
7 changes: 2 additions & 5 deletions lib/graphql/anycable/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
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
initializer "graphql_anycable.load_trigger_job" do
require "graphql/jobs/trigger_job"
end


rake_tasks do
path = File.expand_path(__dir__)
Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/subscriptions/anycable_subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/graphql/anycable_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")

Expand Down
14 changes: 7 additions & 7 deletions spec/graphql/broadcast_spec.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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) }

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1dee021

Please sign in to comment.