-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add an ability to deliver messages asynchronously via adapters
- Loading branch information
1 parent
bef401d
commit ebc7e13
Showing
16 changed files
with
571 additions
and
36 deletions.
There are no files selected for viewing
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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module GraphQL | ||
module Adapters | ||
class ActiveJobAdapter < BaseAdapter | ||
def trigger(...) | ||
executor_class_job.set(queue: config.queue).perform_later( | ||
executor_object, | ||
executor_method, | ||
... | ||
) | ||
end | ||
|
||
private | ||
|
||
def executor_class_job | ||
config.job_class.constantize | ||
end | ||
|
||
def config | ||
GraphQL::AnyCable.config | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module GraphQL | ||
module Adapters | ||
class BaseAdapter | ||
attr_reader :executor_object, :executor_method | ||
|
||
def initialize(executor_object:) | ||
@executor_object = executor_object | ||
@executor_method = executor_object.class::EXECUTOR_METHOD_NAME | ||
end | ||
|
||
def trigger | ||
raise NoMethodError, "#{__method__} method should be implemented in concrete class" | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module GraphQL | ||
module Adapters | ||
class InlineAdapter < BaseAdapter | ||
def trigger(...) | ||
executor_object.public_send(executor_method, ...) | ||
end | ||
end | ||
end | ||
end |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require "graphql/adapters/base_adapter" | ||
require "graphql/adapters/inline_adapter" | ||
require "graphql/adapters/active_job_adapter" | ||
|
||
module GraphQL | ||
module AnyCable | ||
class DeliveryAdapter | ||
class << self | ||
def lookup(options) | ||
adapter_class_name = config.delivery_method.to_s.split("_").map(&:capitalize).join | ||
|
||
Adapters.const_get("#{adapter_class_name}Adapter").new(**(options || {})) | ||
rescue NameError => e | ||
raise e.class, "Delivery adapter :#{config.delivery_method} haven't been found", e.backtrace | ||
end | ||
|
||
def config | ||
GraphQL::AnyCable.config | ||
end | ||
end | ||
end | ||
end | ||
end |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module GraphQL | ||
module Jobs | ||
class TriggerJob < ActiveJob::Base | ||
def perform(executor_object, execute_method, event_name, args = {}, object = nil, options = {}) | ||
executor_object.public_send(execute_method, event_name, args, object, **options) | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
lib/graphql/serializers/anycable_subscription_serializer.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module GraphQL | ||
module Serializers | ||
class AnyCableSubscriptionSerializer < ActiveJob::Serializers::ObjectSerializer | ||
def serialize?(argument) | ||
argument.kind_of?(GraphQL::Subscriptions::AnyCableSubscriptions) | ||
end | ||
|
||
def serialize(subscription) | ||
super(subscription.collected_arguments) | ||
end | ||
|
||
def deserialize(payload) | ||
GraphQL::Subscriptions::AnyCableSubscriptions.new(**payload) | ||
end | ||
end | ||
end | ||
end |
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 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
Oops, something went wrong.