File: base_dsl_methods.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (128 lines) | stat: -rw-r--r-- 3,945 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# frozen_string_literal: true

require "graphql/schema/find_inherited_value"

module GraphQL
  class Schema
    class Member
      # DSL methods shared by lots of things in the GraphQL Schema.
      # @api private
      # @see Classes that extend this, eg {GraphQL::Schema::Object}
      module BaseDSLMethods
        include GraphQL::Schema::FindInheritedValue

        # Call this with a new name to override the default name for this schema member; OR
        # call it without an argument to get the name of this schema member
        #
        # The default name is implemented in default_graphql_name
        # @param new_name [String]
        # @return [String]
        def graphql_name(new_name = nil)
          if new_name
            GraphQL::NameValidator.validate!(new_name)
            @graphql_name = new_name
          else
            @graphql_name ||= default_graphql_name
          end
        end

        # Just a convenience method to point out that people should use graphql_name instead
        def name(new_name = nil)
          return super() if new_name.nil?

          fail(
            "The new name override method is `graphql_name`, not `name`. Usage: "\
            "graphql_name \"#{new_name}\""
          )
        end

        # Call this method to provide a new description; OR
        # call it without an argument to get the description
        # @param new_description [String]
        # @return [String]
        def description(new_description = nil)
          if new_description
            @description = new_description
          elsif defined?(@description)
            @description
          else
            @description = nil
          end
        end

        # This pushes some configurations _down_ the inheritance tree,
        # in order to prevent repetitive lookups at runtime.
        module ConfigurationExtension
          def inherited(child_class)
            child_class.introspection(introspection)
            child_class.description(description)
            child_class.default_graphql_name = nil

            if defined?(@graphql_name) && @graphql_name && (self.name.nil? || graphql_name != default_graphql_name)
              child_class.graphql_name(graphql_name)
            else
              child_class.graphql_name = nil
            end
            super
          end
        end

        # @return [Boolean] If true, this object is part of the introspection system
        def introspection(new_introspection = nil)
          if !new_introspection.nil?
            @introspection = new_introspection
          elsif defined?(@introspection)
            @introspection
          else
            false
          end
        end

        def introspection?
          !!@introspection
        end

        # The mutation this type was derived from, if it was derived from a mutation
        # @return [Class]
        def mutation(mutation_class = nil)
          if mutation_class
            @mutation = mutation_class
          elsif defined?(@mutation)
            @mutation
          else
            nil
          end
        end

        alias :unwrap :itself

        # Creates the default name for a schema member.
        # The default name is the Ruby constant name,
        # without any namespaces and with any `-Type` suffix removed
        def default_graphql_name
          @default_graphql_name ||= begin
            raise GraphQL::RequiredImplementationMissingError, 'Anonymous class should declare a `graphql_name`' if name.nil?
            g_name = -name.split("::").last
            g_name.end_with?("Type") ? g_name.sub(/Type\Z/, "") : g_name
          end
        end

        def visible?(context)
          true
        end

        def authorized?(object, context)
          true
        end

        def default_relay
          false
        end

        protected

        attr_writer :default_graphql_name, :graphql_name
      end
    end
  end
end