Class: ElasticGraph::QueryRegistry::QueryValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/query_registry/query_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema, require_eg_latency_slo_directive:) ⇒ QueryValidator

Returns a new instance of QueryValidator.



18
19
20
21
22
23
24
# File 'lib/elastic_graph/query_registry/query_validator.rb', line 18

def initialize(schema, require_eg_latency_slo_directive:)
  @schema = schema
  @schema_element_names = schema.element_names
  @var_dumper = VariableDumper.new(schema)
  @var_incompat_detector = VariableBackwardIncompatibilityDetector.new
  @require_eg_latency_slo_directive = require_eg_latency_slo_directive
end

Instance Method Details

#validate(query_string, previously_dumped_variables:, client_name:, query_name:) ⇒ Object



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
51
52
53
54
# File 'lib/elastic_graph/query_registry/query_validator.rb', line 26

def validate(query_string, previously_dumped_variables:, client_name:, query_name:)
  # We pass `validate: false` since we do query validation on the operation level down below.
  query = @schema.new_graphql_query(query_string, validate: false)

  if query.document.nil?
    {nil => query.static_errors.map(&:to_h)}
  else
    # @type var fragments: ::Array[::GraphQL::Language::Nodes::FragmentDefinition]
    # @type var operations: ::Array[::GraphQL::Language::Nodes::OperationDefinition]
    fragments, operations = _ = query.document.definitions.partition do |definition|
      definition.is_a?(::GraphQL::Language::Nodes::FragmentDefinition)
    end

    newly_dumped_variables = @var_dumper.dump_variables_for_operations(operations)

    operations.to_h do |operation|
      errors = if operation.name.nil?
        [{"message" => "The query has no named operations. We require all registered queries to be named for more useful logging."}]
      else
        variables_errors = variables_errors_for(_ = operation.name, previously_dumped_variables, newly_dumped_variables, client_name, query_name)
        directive_errors = directive_errors_for(operation)

        static_validation_errors_for(query, operation, fragments) + variables_errors + directive_errors
      end

      [operation.name, errors]
    end
  end
end