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

Use new analysis engine when using Interpreter #1918

Closed
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
4 changes: 2 additions & 2 deletions guides/queries/ast_analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ end

### Using Analyzers

The new query analyzers are added to the schema the same one as before with `query_analyzer`. However, to use the new analysis engine, you must opt in by using `use GraphQL::Analysis::AST`, for example:
The new query analyzers are added to the schema the same one as before with `query_analyzer`. However, to use the new analysis engine, you must opt in by using the new runtime module, [GraphQL::Execution::Interpreter](guides/queries/interpreter.md), for example:

```ruby
class MySchema < GraphQL::Schema
use GraphQL::Analysis::AST
use GraphQL::Execution::Interpreter
query_analyzer MyQueryAnalyzer
end
```
Expand Down
3 changes: 3 additions & 0 deletions guides/queries/interpreter.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ The new runtime works with class-based schemas only. Several features are no lon

These depend on the now-removed `Rewrite` step, which wasted a lot of time making often-unneeded preparation. Most of the attributes you might need from an `irep_node` are available with `extras: [...]`. Query analyzers can be refactored to be static checks (custom validation rules) or dynamic checks, made at runtime. The built-in analyzers have been refactored to run as validators.

A new style of analyzers has been added, which rely only on the query AST. By using the interpreter, you automatically opt-in
into the new analysis runtime, which is [described here](guides/queries/ast_analysis.md).

`irep_node`-based lookahead is not supported. Stay tuned for a replacement.

- `rescue_from`
Expand Down
5 changes: 0 additions & 5 deletions lib/graphql/analysis/ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ module Analysis
module AST
module_function

def use(schema_defn)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to use Interpreter instead.

schema = schema_defn.target
schema.analysis_engine = GraphQL::Analysis::AST
end

# Analyze a multiplex, and all queries within.
# Multiplex analyzers are ran for all queries, keeping state.
# Query analyzers are ran per query, without carrying state between queries.
Expand Down
5 changes: 5 additions & 0 deletions lib/graphql/analysis/ast/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def initialize(query:, analyzers:)
# @return [Array<GraphQL::ObjectType>] Types whose scope we've entered
attr_reader :object_types

def visit
return if @document.nil?
super
end

# Visit Helpers

# @return [GraphQL::Query::Arguments] Arguments for this node, merging default values, literal values and query variables
Expand Down
2 changes: 2 additions & 0 deletions lib/graphql/execution/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def self.use(schema_defn)
schema_defn.query_execution_strategy(GraphQL::Execution::Interpreter)
schema_defn.mutation_execution_strategy(GraphQL::Execution::Interpreter)
schema_defn.subscription_execution_strategy(GraphQL::Execution::Interpreter)
schema = schema_defn.target
schema.analysis_engine = GraphQL::Analysis::AST
end

def self.begin_multiplex(multiplex)
Expand Down
6 changes: 5 additions & 1 deletion lib/graphql/execution/multiplex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ def instrument_and_analyze(multiplex)
schema = multiplex.schema
multiplex_analyzers = schema.multiplex_analyzers
if multiplex.max_complexity
multiplex_analyzers += [GraphQL::Analysis::MaxQueryComplexity.new(multiplex.max_complexity)]
if schema.using_ast_analysis?
multiplex_analyzers += [GraphQL::Analysis::AST::MaxQueryComplexity]
else
multiplex_analyzers += [GraphQL::Analysis::MaxQueryComplexity.new(multiplex.max_complexity)]
end
end

schema.analysis_engine.analyze_multiplex(multiplex, multiplex_analyzers)
Expand Down
47 changes: 31 additions & 16 deletions spec/graphql/analysis/analyze_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
require "spec_helper"

describe GraphQL::Analysis do
class ClassicAnalysisSchema < GraphQL::Schema
query Dummy::DairyAppQuery
mutation Dummy::DairyAppMutation
subscription Dummy::Subscription
max_depth 5
# TODO why is `.graphql_definition` required here?
orphan_types Dummy::Honey, Dummy::Beverage.graphql_definition

rescue_from(Dummy::NoSuchDairyError) { |err| err.message }

def self.resolve_type(type, obj, ctx)
ClassicAnalysisSchema.types[obj.class.name.split("::").last]
end
end

class TypeCollector
def initial_value(query)
[]
Expand Down Expand Up @@ -43,7 +58,7 @@ def call(memo, visit_type, irep_node)
let(:analyzers) { [type_collector, node_counter] }
let(:reduce_result) { GraphQL::Analysis.analyze_query(query, analyzers) }
let(:variables) { {} }
let(:query) { GraphQL::Query.new(Dummy::Schema, query_string, variables: variables) }
let(:query) { GraphQL::Query.new(ClassicAnalysisSchema, query_string, variables: variables) }
let(:query_string) {%|
{
cheese(id: 1) {
Expand All @@ -58,7 +73,7 @@ def call(memo, visit_type, irep_node)
let(:analyzers) { [type_collector, conditional_analyzer] }

describe "when analyze? returns false" do
let(:query) { GraphQL::Query.new(Dummy::Schema, query_string, variables: variables, context: { analyze: false }) }
let(:query) { GraphQL::Query.new(ClassicAnalysisSchema, query_string, variables: variables, context: { analyze: false }) }

it "does not run the analyzer" do
# Only type_collector ran
Expand All @@ -67,7 +82,7 @@ def call(memo, visit_type, irep_node)
end

describe "when analyze? returns true" do
let(:query) { GraphQL::Query.new(Dummy::Schema, query_string, variables: variables, context: { analyze: true }) }
let(:query) { GraphQL::Query.new(ClassicAnalysisSchema, query_string, variables: variables, context: { analyze: true }) }

it "it runs the analyzer" do
# Both analyzers ran
Expand All @@ -93,7 +108,7 @@ def call(memo, visit_type, irep_node)
it "emits traces" do
traces = TestTracing.with_trace do
ctx = { tracers: [TestTracing] }
Dummy::Schema.execute(query_string, context: ctx)
ClassicAnalysisSchema.execute(query_string, context: ctx)
end

# The query_trace is on the list _first_ because it finished first
Expand All @@ -119,14 +134,14 @@ def call(memo, visit_type, irep_node)
let(:variable_accessor) { ->(memo, visit_type, irep_node) { query.variables["cheeseId"] } }

before do
@previous_query_analyzers = Dummy::Schema.query_analyzers.dup
Dummy::Schema.query_analyzers.clear
Dummy::Schema.query_analyzers << variable_accessor
@previous_query_analyzers = ClassicAnalysisSchema.query_analyzers.dup
ClassicAnalysisSchema.query_analyzers.clear
ClassicAnalysisSchema.query_analyzers << variable_accessor
end

after do
Dummy::Schema.query_analyzers.clear
Dummy::Schema.query_analyzers.push(*@previous_query_analyzers)
ClassicAnalysisSchema.query_analyzers.clear
ClassicAnalysisSchema.query_analyzers.push(*@previous_query_analyzers)
end

it "returns an error" do
Expand Down Expand Up @@ -213,7 +228,7 @@ def final_value(memo)
let(:flavor_catcher) { FlavorCatcher.new }
let(:analyzers) { [id_catcher, flavor_catcher] }
let(:reduce_result) { GraphQL::Analysis.analyze_query(query, analyzers) }
let(:query) { GraphQL::Query.new(Dummy::Schema, query_string) }
let(:query) { GraphQL::Query.new(ClassicAnalysisSchema, query_string) }
let(:query_string) {%|
{
cheese(id: 1) {
Expand All @@ -222,7 +237,7 @@ def final_value(memo)
}
}
|}
let(:schema) { Dummy::Schema }
let(:schema) { ClassicAnalysisSchema }
let(:result) { schema.execute(query_string) }
let(:query_string) {%|
{
Expand All @@ -234,14 +249,14 @@ def final_value(memo)
|}

before do
@previous_query_analyzers = Dummy::Schema.query_analyzers.dup
Dummy::Schema.query_analyzers.clear
Dummy::Schema.query_analyzers << id_catcher << flavor_catcher
@previous_query_analyzers = ClassicAnalysisSchema.query_analyzers.dup
ClassicAnalysisSchema.query_analyzers.clear
ClassicAnalysisSchema.query_analyzers << id_catcher << flavor_catcher
end

after do
Dummy::Schema.query_analyzers.clear
Dummy::Schema.query_analyzers.push(*@previous_query_analyzers)
ClassicAnalysisSchema.query_analyzers.clear
ClassicAnalysisSchema.query_analyzers.push(*@previous_query_analyzers)
end

it "groups all errors together" do
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/analysis/ast_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def foobar

Class.new(GraphQL::Schema) do
query query_type
use GraphQL::Analysis::AST
use GraphQL::Execution::Interpreter
query_analyzer AstErrorAnalyzer
end
end
Expand Down
Loading