Class: ElasticGraph::QueryRegistry::VariableDumper

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

Overview

Responsible for dumping structural information about query variables.

This is necessary for the query registry to be able to support object and enum variables. To understand why, consider what happens when a field is removed from an input object variable used by a client’s query. Whether or not it that will break the client depends on which fields of the input object the client populates when sending the query to ElasticGraph. Similarly, if an enum value is removed from an enum value variable used by a client, it could be a breaking change (but only if the client ever passes the removed enum value).

To detect this situation, we use this to dump the structural information about all variables. When the structure of variables changes, we can then tell the engineer that they need to verify that it won’t break the client.

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ VariableDumper

Returns a new instance of VariableDumper.



27
28
29
30
# File 'lib/elastic_graph/query_registry/variable_dumper.rb', line 27

def initialize(schema)
  @schema = schema
  @graphql_schema = schema.graphql_schema
end

Instance Method Details

#dump_variables_for_operations(operations) ⇒ Object

Returns a hash containing the variables for each operation.



48
49
50
51
52
# File 'lib/elastic_graph/query_registry/variable_dumper.rb', line 48

def dump_variables_for_operations(operations)
  operations.each_with_index.to_h do |operation, index|
    [operation.name || "(Anonymous operation #{index + 1})", variables_for_op(operation)]
  end
end

#dump_variables_for_query(query_string) ⇒ Object

Returns a hash of operations from the given query string. For each operation, the value is a hash of variables.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/elastic_graph/query_registry/variable_dumper.rb', line 34

def dump_variables_for_query(query_string)
  query = @schema.new_graphql_query(query_string, validate: false)

  if query.document.nil?
    # If the query was unparsable, we don't know anything about the variables and must just return an empty hash.
    {}
  else
    # @type var operations: ::Array[::GraphQL::Language::Nodes::OperationDefinition]
    operations = _ = query.document.definitions.grep(::GraphQL::Language::Nodes::OperationDefinition)
    dump_variables_for_operations(operations)
  end
end