Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept, Allow conditional introspection #4551

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/graphql/execution/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class << self
# @param max_complexity [Integer, nil]
# @return [Array<Hash>] One result per query
def run_all(schema, query_options, context: {}, max_complexity: schema.max_complexity)
# Remove 'enable_introspection_entry_points' from the options hash
# before passing it to the query.
# This is a hack to avoid breaking the public API.
query_options = query_options.map do |opts|
opts = opts.dup
if opts.key?(:enable_introspection_entry_points)
opts.delete(:enable_introspection_entry_points)
end
opts
end
queries = query_options.map do |opts|
case opts
when Hash
Expand Down
3 changes: 2 additions & 1 deletion lib/graphql/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def selected_operation_name
# @param max_complexity [Numeric] the maximum field complexity for this query (falls back to schema-level value)
# @param except [<#call(schema_member, context)>] If provided, objects will be hidden from the schema when `.call(schema_member, context)` returns truthy
# @param only [<#call(schema_member, context)>] If provided, objects will be hidden from the schema when `.call(schema_member, context)` returns false
def initialize(schema, query_string = nil, query: nil, document: nil, context: nil, variables: nil, validate: true, static_validator: nil, subscription_topic: nil, operation_name: nil, root_value: nil, max_depth: schema.max_depth, max_complexity: schema.max_complexity, except: nil, only: nil, warden: nil)
def initialize(schema, query_string = nil, query: nil, document: nil, context: nil, variables: nil, validate: true, static_validator: nil, subscription_topic: nil, operation_name: nil, root_value: nil, max_depth: schema.max_depth, max_complexity: schema.max_complexity, except: nil, only: nil, warden: nil, disable_introspection_entry_points: true)
# Even if `variables: nil` is passed, use an empty hash for simpler logic
variables ||= {}
@schema = schema
Expand All @@ -114,6 +114,7 @@ def initialize(schema, query_string = nil, query: nil, document: nil, context: n
self.static_validator = static_validator if static_validator
context_tracers = (context ? context.fetch(:tracers, []) : [])
@tracers = schema.tracers + context_tracers
@disable_introspection_entry_points = disable_introspection_entry_points

# Support `ctx[:backtrace] = true` for wrapping backtraces
if context && context[:backtrace] && [email protected]?(GraphQL::Backtrace::Tracer)
Expand Down
6 changes: 6 additions & 0 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,11 @@ def disable_introspection_entry_points
@introspection_system = nil
end

def enable_introspection_entry_points
@disable_introspection_entry_points = false
introspection_system
end

def disable_schema_introspection_entry_point
@disable_schema_introspection_entry_point = true
# TODO: this clears the cache made in `def types`. But this is not a great solution.
Expand Down Expand Up @@ -1045,6 +1050,7 @@ def execute(query_str = nil, **kwargs)
if query_str
kwargs[:query] = query_str
end
enable_introspection_entry_points if kwargs[:enable_introspection_entry_points]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this won't play nice with a Fiber- or Thread-based application server (like Puma) because in that case, multiple queries may be running at the same time, using the same schema. If one query disabled entry points on the schema, then another query enabled them again, the first query would use the setting from the second query. (It's a memory conflict: the schema is shared between queries, so it the schema is changed, all currently-running queries would use the modified schema.)

Technically, it would still work in forking servers (like Unicorn), but modifying the Schema class while the application is running would cause it to be copied into fork-specific memory which is slow and a poor use of space. (Although I wouldn't be surprised if some other element of Schema setup causes it to be written into the fork's own heap, so that may not be a new problem...)

# Some of the query context _should_ be passed to the multiplex, too
multiplex_context = if (ctx = kwargs[:context])
{
Expand Down
Loading