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

deprecations.rb « graphql « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 61a49bd7473581f1a0134c4bcfe6cff598c214c2 (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
57
# frozen_string_literal: true

# Concern for handling GraphQL deprecations.
# https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#deprecating-schema-items
module Gitlab
  module Graphql
    module Deprecations
      extend ActiveSupport::Concern

      included do
        attr_accessor :deprecation
      end

      def initialize(*args, **kwargs, &block)
        init_gitlab_deprecation(kwargs)

        super

        update_deprecation_description
      end

      def visible?(ctx)
        super && ctx[:remove_deprecated] == true ? deprecation.nil? : true
      end

      private

      # Set deprecation, mutate the arguments
      def init_gitlab_deprecation(kwargs)
        if kwargs[:deprecation_reason].present?
          raise ArgumentError, <<~ERROR
            Use `deprecated` property instead of `deprecation_reason`. See
            #{Rails.application.routes.url_helpers.help_page_url('development/api_graphql_styleguide', anchor: 'deprecating-schema-items')}
          ERROR
        end

        # GitLab allows items to be marked as "alpha", which leverages GraphQL deprecations.
        deprecation_args = kwargs.extract!(:alpha, :deprecated)

        self.deprecation = Deprecation.parse(**deprecation_args)
        return unless deprecation

        unless deprecation.valid?
          raise ArgumentError, "Bad deprecation. #{deprecation.errors.full_messages.to_sentence}"
        end

        kwargs[:deprecation_reason] = deprecation.deprecation_reason
      end

      def update_deprecation_description
        return if deprecation.nil?

        description(deprecation.edit_description(description))
      end
    end
  end
end