Class: GraphQL::Schema::Subset

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/schema/subset.rb

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Subset

Returns a new instance of Subset.



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
# File 'lib/graphql/schema/subset.rb', line 6

def initialize(query)
  @query = query
  @context = query.context
  @schema = query.schema
  @all_types = {}
  @all_types_loaded = false
  @unvisited_types = []
  @referenced_types = Hash.new { |h, type_defn| h[type_defn] = [] }.compare_by_identity
  @cached_possible_types = nil
  @cached_visible = Hash.new { |h, member|
    h[member] = @schema.visible?(member, @context)
  }.compare_by_identity

  @cached_visible_fields = Hash.new { |h, owner|
    h[owner] = Hash.new do |h2, field|
      h2[field] = if @cached_visible[field] &&
          (ret_type = field.type.unwrap) &&
          @cached_visible[ret_type] &&
          reachable_type?(ret_type.graphql_name) &&
          (owner == field.owner || (!owner.kind.object?) || field_on_visible_interface?(field, owner))

        if !field.introspection?
          # The problem is that some introspection fields may have references
          # to non-custom introspection types.
          # If those were added here, they'd cause a DuplicateNamesError.
          # This is basically a bug -- those fields _should_ reference the custom types.
          add_type(ret_type, field)
        end
        true
      else
        false
      end
    end.compare_by_identity
  }.compare_by_identity

  @cached_visible_arguments = Hash.new do |h, arg|
    h[arg] = if @cached_visible[arg] && (arg_type = arg.type.unwrap) && @cached_visible[arg_type]
      add_type(arg_type, arg)
      true
    else
      false
    end
  end.compare_by_identity

  @unfiltered_pt = Hash.new do |hash, type|
    hash[type] = @schema.possible_types(type)
  end.compare_by_identity
end

Instance Method Details

#all_typesObject



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/graphql/schema/subset.rb', line 221

def all_types
  @all_types_filtered ||= begin
    load_all_types
    at = []
    @all_types.each do |_name, type_defn|
      if possible_types(type_defn).any? || referenced?(type_defn)
        at << type_defn
      end
    end
    at
  end
end

#argument(owner, arg_name) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/graphql/schema/subset.rb', line 150

def argument(owner, arg_name)
  # TODO this makes a Warden.visible_entry call down the stack
  # I need a non-Warden implementation
  arg = owner.get_argument(arg_name, @context)
  if arg.is_a?(Array)
    visible_arg = nil
    arg.each do |arg_defn|
      if @cached_visible_arguments[arg_defn]
        if arg_defn&.loads
          add_type(arg_defn.loads, arg_defn)
        end
        if visible_arg.nil?
          visible_arg = arg_defn
        else
          raise_duplicate_definition(visible_arg, arg_defn)
        end
      end
    end
    visible_arg
  else
    if arg && @cached_visible_arguments[arg]
      if arg&.loads
        add_type(arg.loads, arg)
      end
      arg
    else
      nil
    end
  end
end

#arguments(owner) ⇒ Object



146
147
148
# File 'lib/graphql/schema/subset.rb', line 146

def arguments(owner)
  non_duplicate_items(owner.all_argument_definitions, @cached_visible_arguments)
end

#directive_exists?(dir_name) ⇒ Boolean

Returns:

  • (Boolean)


242
243
244
245
# File 'lib/graphql/schema/subset.rb', line 242

def directive_exists?(dir_name)
  dir = @schema.directives[dir_name]
  dir && @cached_visible[dir]
end

#directivesObject



247
248
249
# File 'lib/graphql/schema/subset.rb', line 247

def directives
  @schema.directives.each_value.select { |d| @cached_visible[d] }
end

#enum_values(owner) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/graphql/schema/subset.rb', line 234

def enum_values(owner)
  values = non_duplicate_items(owner.all_enum_value_definitions, @cached_visible)
  if values.size == 0
    raise GraphQL::Schema::Enum::MissingValuesError.new(owner)
  end
  values
end

#field(owner, field_name) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/graphql/schema/subset.rb', line 110

def field(owner, field_name)
  f = if owner.kind.fields? && (field = owner.get_field(field_name, @context))
    field
  elsif owner == query_root && (entry_point_field = @schema.introspection_system.entry_point(name: field_name))
    entry_point_field
  elsif (dynamic_field = @schema.introspection_system.dynamic_field(name: field_name))
    dynamic_field
  else
    nil
  end
  if f.is_a?(Array)
    visible_f = nil
    f.each do |f_defn|
      if @cached_visible_fields[owner][f_defn]

        if visible_f.nil?
          visible_f = f_defn
        else
          raise_duplicate_definition(visible_f, f_defn)
        end
      end
    end
    visible_f
  else
    if f && @cached_visible_fields[owner][f]
      f
    else
      nil
    end
  end
end

#field_on_visible_interface?(field, owner) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/graphql/schema/subset.rb', line 55

def field_on_visible_interface?(field, owner)
  ints = owner.interface_type_memberships.map(&:abstract_type)
  field_name = field.graphql_name
  filtered_ints = interfaces(owner)
  any_interface_has_field = false
  any_interface_has_visible_field = false
  ints.each do |int_t|
    if (_int_f_defn = int_t.get_field(field_name, @context))
      any_interface_has_field = true

      if filtered_ints.include?(int_t) # TODO cycles, or maybe not necessary since previously checked? && @cached_visible_fields[owner][field]
        any_interface_has_visible_field = true
        break
      end
    end
  end

  if any_interface_has_field
    any_interface_has_visible_field
  else
    true
  end
end

#fields(owner) ⇒ Object



142
143
144
# File 'lib/graphql/schema/subset.rb', line 142

def fields(owner)
  non_duplicate_items(owner.all_field_definitions, @cached_visible_fields[owner])
end

#interfaces(obj_or_int_type) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/graphql/schema/subset.rb', line 201

def interfaces(obj_or_int_type)
  ints = obj_or_int_type.interface_type_memberships
    .select { |itm| @cached_visible[itm] && @cached_visible[itm.abstract_type] }
    .map!(&:abstract_type)
  ints.uniq! # Remove any duplicate interfaces implemented via other interfaces
  ints
end

#loadable?(t, _ctx) ⇒ Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/graphql/schema/subset.rb', line 251

def loadable?(t, _ctx)
  !@all_types[t.graphql_name] # TODO make sure t is not reachable but t is visible
end

#loaded_typesObject



261
262
263
# File 'lib/graphql/schema/subset.rb', line 261

def loaded_types
  @all_types.values
end

#mutation_rootObject



213
214
215
# File 'lib/graphql/schema/subset.rb', line 213

def mutation_root
  add_if_visible(@schema.mutation)
end

#possible_types(type) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/graphql/schema/subset.rb', line 181

def possible_types(type)
  @cached_possible_types ||= Hash.new do |h, type|
    pt = case type.kind.name
    when "INTERFACE"
      # TODO this requires the global map
      @unfiltered_pt[type]
    when "UNION"
      type.type_memberships.select { |tm| @cached_visible[tm] && @cached_visible[tm.object_type] }.map!(&:object_type)
    else
      [type]
    end

    # TODO use `select!` when possible, skip it for `[type]`
    h[type] = pt.select { |t|
      @cached_visible[t] && referenced?(t)
    }
  end.compare_by_identity
  @cached_possible_types[type]
end

#query_rootObject



209
210
211
# File 'lib/graphql/schema/subset.rb', line 209

def query_root
  add_if_visible(@schema.query)
end

#reachable_type?(type_name) ⇒ Boolean

TODO rename this to indicate that it is called with a typename

Returns:

  • (Boolean)


256
257
258
259
# File 'lib/graphql/schema/subset.rb', line 256

def reachable_type?(type_name)
  load_all_types
  !!((t = @all_types[type_name]) && referenced?(t))
end

#subscription_rootObject



217
218
219
# File 'lib/graphql/schema/subset.rb', line 217

def subscription_root
  add_if_visible(@schema.subscription)
end

#type(type_name) ⇒ Object



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
# File 'lib/graphql/schema/subset.rb', line 79

def type(type_name)
  t = if (loaded_t = @all_types[type_name])
    loaded_t
 elsif !@all_types_loaded
   load_all_types
    @all_types[type_name]
  end

  if t
    if t.is_a?(Array)
      vis_t = nil
      t.each do |t_defn|
        if @cached_visible[t_defn]
          if vis_t.nil?
            vis_t = t_defn
          else
            raise_duplicate_definition(vis_t, t_defn)
          end
        end
      end
      vis_t
    else
      if t && @cached_visible[t]
        t
      else
        nil
      end
    end
  end
end