File: mock_gitlab_schema.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (41 lines) | stat: -rw-r--r-- 1,358 bytes parent folder | download
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
# frozen_string_literal: true

# A stub implementation of ActionCable.
# Any methods to support the mock backend have `mock` in the name.
module Graphql
  module Subscriptions
    module ActionCable
      class MockGitlabSchema < GraphQL::Schema
        class << self
          def find_by_gid(gid)
            return unless gid

            if gid.model_class < ApplicationRecord
              Gitlab::Graphql::Loaders::BatchModelLoader.new(gid.model_class, gid.model_id).find
            elsif gid.model_class.respond_to?(:lazy_find)
              gid.model_class.lazy_find(gid.model_id)
            else
              gid.find
            end
          end

          def id_from_object(object, _type = nil, _ctx = nil)
            unless object.respond_to?(:to_global_id)
              # This is an error in our schema and needs to be solved. So raise a
              # more meaningful error message
              raise "#{object} does not implement `to_global_id`. " \
                    "Include `GlobalID::Identification` into `#{object.class}"
            end

            object.to_global_id
          end
        end

        query(::Types::QueryType)
        subscription(::Types::SubscriptionType)

        use GraphQL::Subscriptions::ActionCableSubscriptions, action_cable: MockActionCable, action_cable_coder: JSON
      end
    end
  end
end