Welcome to mirror list, hosted at ThFree Co, Russian Federation.

graphql_channel.rb « channels « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d364cc2b64b2b4c0181c995e9524825e8f6b2a9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

# This is based on https://github.com/rmosolgo/graphql-ruby/blob/v1.11.8/lib/graphql/subscriptions/action_cable_subscriptions.rb#L19-L82
# modified to work with our own ActionCableLink client

class GraphqlChannel < ApplicationCable::Channel # rubocop:disable Gitlab/NamespacedClass
  def subscribed
    @subscription_ids = []

    query = params['query']
    variables = Gitlab::Graphql::Variables.new(params['variables']).to_h
    operation_name = params['operationName']

    result = GitlabSchema.execute(
      query,
      context: context,
      variables: variables,
      operation_name: operation_name
    )

    payload = {
      result: result.to_h,
      more: result.subscription?
    }

    # Track the subscription here so we can remove it
    # on unsubscribe.
    if result.context[:subscription_id]
      @subscription_ids << result.context[:subscription_id]
    end

    transmit(payload)
  end

  def unsubscribed
    @subscription_ids.each do |sid|
      GitlabSchema.subscriptions.delete_subscription(sid)
    end
  end

  rescue_from Gitlab::Graphql::Variables::Invalid do |exception|
    transmit({ errors: [{ message: exception.message }] })
  end

  private

  # When modifying the context, also update GraphqlController#context if needed
  # so that we have similar context when executing queries, mutations, and subscriptions
  #
  # Objects added to the context may also need to be reloaded in
  # `Subscriptions::BaseSubscription` so that they are not stale
  def context
    # is_sessionless_user is always false because we only support cookie auth in ActionCable
    { channel: self, current_user: current_user, is_sessionless_user: false }
  end
end