Sha256: 3116c948d2fc52e1395e364aebcdc98d3faaf6a7bb152e7f3dc91784354485cf

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

module GraphQL
  module Streaming
    module AssignSubscriptionField
      # Wrap resolve_proc with subscription registration logic.
      #
      # @example Lookup a post and subscribe to it
      #   subscription :post, PostType do
      #     argument :id, !types.Int
      #     resolve -> (obj, args, ctx) {
      #       Post.find(args[:id])
      #     end
      #   end
      def self.call(*args, &block)
        underlying_field = GraphQL::Define::AssignObjectField.call(*args, &block)
        # ensure defined
        # TODO: resolve_proc should be a lazy attr reader
        field_name = underlying_field.name
        original_resolve = underlying_field.resolve_proc
        underlying_field.resolve = SubscriptionResolve.new(field_name, original_resolve)
        # Field was assigned to type_defn in AssignObjectField
      end

      class SubscriptionResolve
        def initialize(subscription_handle, resolve_proc)
          @subscription_handle = subscription_handle
          @resolve_proc = resolve_proc
        end

        def call(obj, args, ctx)
          subscriber = ctx[:subscriber]
          subscriber && subscriber.register(@subscription_handle, args.to_h)
          @resolve_proc.call(obj, args, ctx)
        end
      end

      GraphQL::ObjectType.accepts_definitions(subscription: AssignSubscriptionField)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
graphql-streaming-0.3.0 lib/graphql/streaming/assign_subscription_field.rb
graphql-streaming-0.2.0 lib/graphql/streaming/assign_subscription_field.rb
graphql-streaming-0.1.0 lib/graphql/streaming/assign_subscription_field.rb