Class: GraphQL::Schema::Visibility::Subset
- Inherits:
-
Object
- Object
- GraphQL::Schema::Visibility::Subset
- Defined in:
- lib/graphql/schema/visibility/subset.rb
Overview
This class filters the types, fields, arguments, enum values, and directives in a schema
based on the given context
.
It’s like Warden, but has some differences:
- It doesn’t use GraphQL::Schema’s top-level caches (eg GraphQL::Schema.references_to, GraphQL::Schema.possible_types, GraphQL::Schema.types)
- It doesn’t hide Interface or Union types when all their possible types are hidden. (Instead, those types should implement
.visible?
to hide in that case.) - It checks
.visible?
on root introspection types
In the future, Subset will support lazy-loading types as needed during execution and multi-request caching of subsets.
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
-
#all_types ⇒ Object
-
#all_types_h ⇒ Object
-
#argument(owner, arg_name) ⇒ Object
-
#arguments(owner) ⇒ Object
-
#directive_exists?(dir_name) ⇒ Boolean
-
#directives ⇒ Object
-
#enum_values(owner) ⇒ Object
-
#field(owner, field_name) ⇒ Object
-
#field_on_visible_interface?(field, owner) ⇒ Boolean
-
#fields(owner) ⇒ Object
-
#initialize(context:, schema:) ⇒ Subset
constructor
A new instance of Subset.
-
#interfaces(obj_or_int_type) ⇒ Object
-
#loadable?(t, _ctx) ⇒ Boolean
-
#loaded_types ⇒ Object
-
#mutation_root ⇒ Object
-
#possible_types(type) ⇒ Object
-
#query_root ⇒ Object
-
#reachable_type?(name) ⇒ Boolean
-
#subscription_root ⇒ Object
-
#type(type_name) ⇒ Object
Constructor Details
#initialize(context:, schema:) ⇒ Subset
Returns a new instance of Subset.
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 129 130 131 132 133 134 135 136 137 |
# File 'lib/graphql/schema/visibility/subset.rb', line 33 def initialize(context:, schema:) @context = context @schema = schema @all_types = {} @all_types_loaded = false @unvisited_types = [] @referenced_types = Hash.new { |h, type_defn| h[type_defn] = [] }.compare_by_identity @cached_directives = {} @all_directives = 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] && (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 @cached_parent_fields = Hash.new do |h, type| h[type] = Hash.new do |h2, field_name| h2[field_name] = type.get_field(field_name, @context) end end.compare_by_identity @cached_parent_arguments = Hash.new do |h, arg_owner| h[arg_owner] = Hash.new do |h2, arg_name| h2[arg_name] = arg_owner.get_argument(arg_name, @context) end end.compare_by_identity @cached_possible_types = Hash.new do |h, type| h[type] = case type.kind.name when "INTERFACE" load_all_types pts = [] @unfiltered_interface_type_memberships[type].each { |itm| if @cached_visible[itm] && (ot = itm.object_type) && @cached_visible[ot] && referenced?(ot) pts << ot end } pts when "UNION" pts = [] type.type_memberships.each { |tm| if @cached_visible[tm] && (ot = tm.object_type) && @cached_visible[ot] && referenced?(ot) pts << ot end } pts when "OBJECT" load_all_types if @all_types[type.graphql_name] == type [type] else EmptyObjects::EMPTY_ARRAY end else GraphQL::EmptyObjects::EMPTY_ARRAY end end.compare_by_identity @cached_enum_values = Hash.new do |h, enum_t| values = non_duplicate_items(enum_t.all_enum_value_definitions, @cached_visible) if values.size == 0 raise GraphQL::Schema::Enum::MissingValuesError.new(enum_t) end h[enum_t] = values end.compare_by_identity @cached_fields = Hash.new do |h, owner| h[owner] = non_duplicate_items(owner.all_field_definitions.each(&:ensure_loaded), @cached_visible_fields[owner]) end.compare_by_identity @cached_arguments = Hash.new do |h, owner| h[owner] = non_duplicate_items(owner.all_argument_definitions, @cached_visible_arguments) end.compare_by_identity end |
Class Method Details
.from_context(ctx, schema) ⇒ Schema::Visibility::Subset
18 19 20 21 22 23 24 25 |
# File 'lib/graphql/schema/visibility/subset.rb', line 18 def self.from_context(ctx, schema) if ctx.respond_to?(:types) && (types = ctx.types).is_a?(self) types else # TODO use a cached instance from the schema self.new(context: ctx, schema: schema) end end |
.pass_thru(context:, schema:) ⇒ Object
27 28 29 30 31 |
# File 'lib/graphql/schema/visibility/subset.rb', line 27 def self.pass_thru(context:, schema:) subset = self.new(context: context, schema: schema) subset.instance_variable_set(:@cached_visible, Hash.new { |h,k| h[k] = true }) subset end |
Instance Method Details
#all_types ⇒ Object
278 279 280 281 |
# File 'lib/graphql/schema/visibility/subset.rb', line 278 def all_types load_all_types @all_types.values end |
#all_types_h ⇒ Object
283 284 285 286 |
# File 'lib/graphql/schema/visibility/subset.rb', line 283 def all_types_h load_all_types @all_types end |
#argument(owner, arg_name) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/graphql/schema/visibility/subset.rb', line 231 def argument(owner, arg_name) arg = @cached_parent_arguments[owner][arg_name] if arg.is_a?(Array) visible_arg = nil arg.each do |arg_defn| if @cached_visible_arguments[arg_defn] 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] arg else nil end end end |
#arguments(owner) ⇒ Object
227 228 229 |
# File 'lib/graphql/schema/visibility/subset.rb', line 227 def arguments(owner) @cached_arguments[owner] end |
#directive_exists?(dir_name) ⇒ Boolean
292 293 294 295 296 297 298 299 |
# File 'lib/graphql/schema/visibility/subset.rb', line 292 def directive_exists?(dir_name) if (dir = @schema.directives[dir_name]) && @cached_visible[dir] !!dir else load_all_types !!@cached_directives[dir_name] end end |
#directives ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/graphql/schema/visibility/subset.rb', line 301 def directives @all_directives ||= begin load_all_types dirs = [] @schema.directives.each do |name, dir_defn| if !@cached_directives[name] && @cached_visible[dir_defn] dirs << dir_defn end end dirs.concat(@cached_directives.values) end end |
#enum_values(owner) ⇒ Object
288 289 290 |
# File 'lib/graphql/schema/visibility/subset.rb', line 288 def enum_values(owner) @cached_enum_values[owner] end |
#field(owner, field_name) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/graphql/schema/visibility/subset.rb', line 193 def field(owner, field_name) f = if owner.kind.fields? && (field = @cached_parent_fields[owner][field_name]) 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.ensure_loaded elsif f && @cached_visible_fields[owner][f.ensure_loaded] f else nil end end |
#field_on_visible_interface?(field, owner) ⇒ Boolean
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/graphql/schema/visibility/subset.rb', line 139 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 = @cached_parent_fields[int_t][field_name]) 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
223 224 225 |
# File 'lib/graphql/schema/visibility/subset.rb', line 223 def fields(owner) @cached_fields[owner] end |
#interfaces(obj_or_int_type) ⇒ Object
258 259 260 261 262 263 264 |
# File 'lib/graphql/schema/visibility/subset.rb', line 258 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
314 315 316 |
# File 'lib/graphql/schema/visibility/subset.rb', line 314 def loadable?(t, _ctx) !@all_types[t.graphql_name] && @cached_visible[t] end |
#loaded_types ⇒ Object
318 319 320 |
# File 'lib/graphql/schema/visibility/subset.rb', line 318 def loaded_types @all_types.values end |
#mutation_root ⇒ Object
270 271 272 |
# File 'lib/graphql/schema/visibility/subset.rb', line 270 def mutation_root add_if_visible(@schema.mutation) end |
#possible_types(type) ⇒ Object
254 255 256 |
# File 'lib/graphql/schema/visibility/subset.rb', line 254 def possible_types(type) @cached_possible_types[type] end |
#query_root ⇒ Object
266 267 268 |
# File 'lib/graphql/schema/visibility/subset.rb', line 266 def query_root add_if_visible(@schema.query) end |
#reachable_type?(name) ⇒ Boolean
322 323 324 325 |
# File 'lib/graphql/schema/visibility/subset.rb', line 322 def reachable_type?(name) load_all_types !!@all_types[name] end |
#subscription_root ⇒ Object
274 275 276 |
# File 'lib/graphql/schema/visibility/subset.rb', line 274 def subscription_root add_if_visible(@schema.subscription) end |
#type(type_name) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/graphql/schema/visibility/subset.rb', line 163 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 |