Module: GraphQL::Analysis::AST

Defined in:
lib/graphql/analysis/ast/query_depth.rb,
lib/graphql/analysis/ast.rb,
lib/graphql/analysis/ast/visitor.rb,
lib/graphql/analysis/ast/analyzer.rb,
lib/graphql/analysis/ast/field_usage.rb,
lib/graphql/analysis/ast/max_query_depth.rb,
lib/graphql/analysis/ast/query_complexity.rb,
lib/graphql/analysis/ast/max_query_complexity.rb

Overview

Calculate the complexity of a query, using Field#complexity values.

Defined Under Namespace

Classes: Analyzer, FieldUsage, MaxQueryComplexity, MaxQueryDepth, QueryComplexity, QueryDepth, Visitor

Class Method Summary collapse

Class Method Details

.analysis_errors(results) ⇒ Object



77
78
79
# File 'lib/graphql/analysis/ast.rb', line 77

def analysis_errors(results)
  results.flatten.select { |r| r.is_a?(GraphQL::AnalysisError) }
end

.analyze_multiplex(multiplex, analyzers) ⇒ Array<Any>

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.

Parameters:

Returns:

  • (Array<Any>)

    Results from multiplex analyzers



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
# File 'lib/graphql/analysis/ast.rb', line 26

def analyze_multiplex(multiplex, analyzers)
  multiplex_analyzers = analyzers.map { |analyzer| analyzer.new(multiplex) }

  multiplex.trace("analyze_multiplex", { multiplex: multiplex }) do
    query_results = multiplex.queries.map do |query|
      if query.valid?
        analyze_query(
          query,
          query.analyzers,
          multiplex_analyzers: multiplex_analyzers
        )
      else
        []
      end
    end

    multiplex_results = multiplex_analyzers.map(&:result)
    multiplex_errors = analysis_errors(multiplex_results)

    multiplex.queries.each_with_index do |query, idx|
      query.analysis_errors = multiplex_errors + analysis_errors(query_results[idx])
    end
    multiplex_results
  end
end

.analyze_query(query, analyzers, multiplex_analyzers: []) ⇒ Array<Any>

Returns Results from those analyzers.

Parameters:

Returns:

  • (Array<Any>)

    Results from those analyzers



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

def analyze_query(query, analyzers, multiplex_analyzers: [])
  query.trace("analyze_query", { query: query }) do
    query_analyzers = analyzers
      .map { |analyzer| analyzer.new(query) }
      .select { |analyzer| analyzer.analyze? }

    analyzers_to_run = query_analyzers + multiplex_analyzers
    if analyzers_to_run.any?
      visitor = GraphQL::Analysis::AST::Visitor.new(
        query: query,
        analyzers: analyzers_to_run
      )

      visitor.visit

      query_analyzers.map(&:result)
    else
      []
    end
  end
end

.use(schema_class) ⇒ Object



15
16
17
# File 'lib/graphql/analysis/ast.rb', line 15

def use(schema_class)
  schema_class.analysis_engine = GraphQL::Analysis::AST
end