Sha256: 27299a02671e6c47b29994c05713ef46fb17342c851e5e02fed7e13266198902

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module GraphQL
  class Subscriptions
    # Extend this module in your subscription root when using {GraphQL::Execution::Interpreter}.
    module SubscriptionRoot
      def field(*args, extensions: [], **rest, &block)
        extensions += [Extension]
        super(*args, extensions: extensions, **rest, &block)
      end

      class Extension < GraphQL::Schema::FieldExtension
        def after_resolve(value:, context:, object:, arguments:, **rest)
          if value.is_a?(GraphQL::ExecutionError)
            value
          elsif (events = context.namespace(:subscriptions)[:events])
            # This is the first execution, so gather an Event
            # for the backend to register:
            events << Subscriptions::Event.new(
              name: field.name,
              arguments: arguments,
              context: context,
              field: field,
            )
            context.skip
          elsif context.query.subscription_topic == Subscriptions::Event.serialize(
              field.name,
              arguments,
              field,
              scope: (field.subscription_scope ? context[field.subscription_scope] : nil),
            )
            # The root object is _already_ the subscription update,
            # it was passed to `.trigger`
            object.object
          else
            # This is a subscription update, but this event wasn't triggered.
            context.skip
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-1.9.0.pre2 lib/graphql/subscriptions/subscription_root.rb
graphql-1.9.0.pre1 lib/graphql/subscriptions/subscription_root.rb