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

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

module Gitlab
  module Graphql
    module NegatableArguments
      class TypeDefiner
        def initialize(resolver_class, type_definition)
          @resolver_class = resolver_class
          @type_definition = type_definition
        end

        def define!
          negated_params_type.instance_eval(&@type_definition)
        end

        def negated_params_type
          @negated_params_type ||= existing_type || build_type
        end

        private

        def existing_type
          ::Types.const_get(type_class_name, false) if ::Types.const_defined?(type_class_name)
        end

        def build_type
          klass = Class.new(::Types::BaseInputObject)
          ::Types.const_set(type_class_name, klass)
          klass
        end

        def type_class_name
          @type_class_name ||= begin
            base_name = @resolver_class.name.sub('Resolvers::', '')
            base_name + 'NegatedParamsType'
          end
        end
      end

      def negated(param_key: :not, &block)
        definer = ::Gitlab::Graphql::NegatableArguments::TypeDefiner.new(self, block)
        definer.define!

        argument param_key, definer.negated_params_type,
                 required: false,
                 description: <<~MD
                     List of negated arguments.
                     Warning: this argument is experimental and a subject to change in future.
                 MD
      end
    end
  end
end