Class: GraphQL::Schema::Visibility

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/schema/visibility.rb,
lib/graphql/schema/visibility/visit.rb,
lib/graphql/schema/visibility/profile.rb,
lib/graphql/schema/visibility/migration.rb

Overview

Use this plugin to make some parts of your schema hidden from some viewers.

Defined Under Namespace

Classes: Migration, Profile, Visit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, dynamic:, preload:, profiles:, migration_errors:) ⇒ Visibility

Returns a new instance of Visibility.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/graphql/schema/visibility.rb', line 26

def initialize(schema, dynamic:, preload:, profiles:, migration_errors:)
  @schema = schema
  schema.use_visibility_profile = true
  schema.visibility_profile_class = if migration_errors
    Visibility::Migration
  else
    Visibility::Profile
  end
  @preload = preload
  @profiles = profiles
  @cached_profiles = {}
  @dynamic = dynamic
  @migration_errors = migration_errors
  # Top-level type caches:
  @visit = nil
  @interface_type_memberships = nil
  @directives = nil
  @types = nil
  @all_references = nil
  @loaded_all = false
end

Instance Attribute Details

#cached_profilesObject (readonly)

Returns the value of attribute cached_profiles.



149
150
151
# File 'lib/graphql/schema/visibility.rb', line 149

def cached_profiles
  @cached_profiles
end

#top_levelObject (readonly)

Returns the value of attribute top_level.



176
177
178
# File 'lib/graphql/schema/visibility.rb', line 176

def top_level
  @top_level
end

#unfiltered_interface_type_membershipsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



179
180
181
# File 'lib/graphql/schema/visibility.rb', line 179

def unfiltered_interface_type_memberships
  @unfiltered_interface_type_memberships
end

Class Method Details

.use(schema, dynamic: false, profiles: EmptyObjects::EMPTY_HASH, preload: (Rails) ? Rails.env.production? : nil), migration_errors: false) ⇒ Object

Parameters:

  • schema (Class<GraphQL::Schema>)
  • profiles (Hash<Symbol => Hash>) (defaults to: EmptyObjects::EMPTY_HASH)

    A hash of name => context pairs for preloading visibility profiles

  • preload (Boolean) (defaults to: (Rails) ? Rails.env.production? : nil))

    if true, load the default schema profile and all named profiles immediately (defaults to true for Rails.env.production?)

  • migration_errors (Boolean) (defaults to: false)

    if true, raise an error when Visibility and Warden return different results



15
16
17
18
19
20
21
22
23
24
# File 'lib/graphql/schema/visibility.rb', line 15

def self.use(schema, dynamic: false, profiles: EmptyObjects::EMPTY_HASH, preload: (defined?(Rails) ? Rails.env.production? : nil), migration_errors: false)
  profiles&.each { |name, ctx|
    ctx[:visibility_profile] = name
    ctx.freeze
  }
  schema.visibility = self.new(schema, dynamic: dynamic, preload: preload, profiles: profiles, migration_errors: migration_errors)
  if preload
    schema.visibility.preload
  end
end

Instance Method Details

#all_directivesObject



48
49
50
51
# File 'lib/graphql/schema/visibility.rb', line 48

def all_directives
  load_all
  @directives
end

#all_interface_type_membershipsObject



53
54
55
56
# File 'lib/graphql/schema/visibility.rb', line 53

def all_interface_type_memberships
  load_all
  @interface_type_memberships
end

#all_referencesObject



58
59
60
61
# File 'lib/graphql/schema/visibility.rb', line 58

def all_references
  load_all
  @all_references
end

#dup_for(other_schema) ⇒ Visibility

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Make another Visibility for schema based on this one

Returns:



135
136
137
138
139
140
141
142
143
# File 'lib/graphql/schema/visibility.rb', line 135

def dup_for(other_schema)
  self.class.new(
    other_schema,
    dynamic: @dynamic,
    preload: @preload,
    profiles: @profiles,
    migration_errors: @migration_errors
  )
end

#get_type(type_name) ⇒ Object



63
64
65
66
# File 'lib/graphql/schema/visibility.rb', line 63

def get_type(type_name)
  load_all
  @types[type_name]
end

#introspection_system_configured(introspection_system) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



122
123
124
125
126
127
128
129
130
# File 'lib/graphql/schema/visibility.rb', line 122

def introspection_system_configured(introspection_system)
  if @preload
    introspection_types = [
      *@schema.introspection_system.types.values,
      *@schema.introspection_system.entry_points.map { |ep| ep.type.unwrap },
    ]
    ensure_all_loaded(introspection_types)
  end
end

#migration_errors?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/graphql/schema/visibility.rb', line 145

def migration_errors?
  @migration_errors
end

#mutation_configured(mutation_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



101
102
103
104
105
# File 'lib/graphql/schema/visibility.rb', line 101

def mutation_configured(mutation_type)
  if @preload
    ensure_all_loaded([mutation_type])
  end
end

#orphan_types_configured(orphan_types) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



115
116
117
118
119
# File 'lib/graphql/schema/visibility.rb', line 115

def orphan_types_configured(orphan_types)
  if @preload
    ensure_all_loaded(orphan_types)
  end
end

#preloadObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/graphql/schema/visibility.rb', line 72

def preload
  # Traverse the schema now (and in the *_configured hooks below)
  # To make sure things are loaded during boot
  @preloaded_types = Set.new
  types_to_visit = [
    @schema.query,
    @schema.mutation,
    @schema.subscription,
    *@schema.introspection_system.types.values,
    *@schema.introspection_system.entry_points.map { |ep| ep.type.unwrap },
    *@schema.orphan_types,
  ]
  # Root types may have been nil:
  types_to_visit.compact!
  ensure_all_loaded(types_to_visit)
  @profiles.each do |profile_name, example_ctx|
    prof = profile_for(example_ctx)
    prof.all_types # force loading
  end
end

#preload?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/graphql/schema/visibility.rb', line 68

def preload?
  @preload
end

#profile_for(context, visibility_profile = ) ⇒ Object



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

def profile_for(context, visibility_profile = context[:visibility_profile])
  if !@profiles.empty?
    if visibility_profile.nil?
      if @dynamic
        if context.is_a?(Query::NullContext)
          top_level_profile
        else
          @schema.visibility_profile_class.new(context: context, schema: @schema)
        end
      elsif !@profiles.empty?
        raise ArgumentError, "#{@schema} expects a visibility profile, but `visibility_profile:` wasn't passed. Provide a `visibility_profile:` value or add `dynamic: true` to your visibility configuration."
      end
    elsif !@profiles.include?(visibility_profile)
      raise ArgumentError, "`#{visibility_profile.inspect}` isn't allowed for `visibility_profile:` (must be one of #{@profiles.keys.map(&:inspect).join(", ")}). Or, add `#{visibility_profile.inspect}` to the list of profiles in the schema definition."
    else
      profile_ctx = @profiles[visibility_profile]
      @cached_profiles[visibility_profile] ||= @schema.visibility_profile_class.new(name: visibility_profile, context: profile_ctx, schema: @schema)
    end
  elsif context.is_a?(Query::NullContext)
    top_level_profile
  else
    @schema.visibility_profile_class.new(context: context, schema: @schema)
  end
end

#query_configured(query_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



94
95
96
97
98
# File 'lib/graphql/schema/visibility.rb', line 94

def query_configured(query_type)
  if @preload
    ensure_all_loaded([query_type])
  end
end

#subscription_configured(subscription_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



108
109
110
111
112
# File 'lib/graphql/schema/visibility.rb', line 108

def subscription_configured(subscription_type)
  if @preload
    ensure_all_loaded([subscription_type])
  end
end

#top_level_profile(refresh: false) ⇒ Object



181
182
183
184
185
186
# File 'lib/graphql/schema/visibility.rb', line 181

def top_level_profile(refresh: false)
  if refresh
    @top_level_profile = nil
  end
  @top_level_profile ||= @schema.visibility_profile_class.new(context: Query::NullContext.instance, schema: @schema)
end