Class: GraphQL::Hive::Analyzer

Inherits:
Analysis::AST::Analyzer
  • Object
show all
Defined in:
lib/graphql-hive/analyzer.rb

Overview

Fetch all users fields, input objects and enums

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_or_multiplex) ⇒ Analyzer

Returns a new instance of Analyzer.



7
8
9
10
# File 'lib/graphql-hive/analyzer.rb', line 7

def initialize(query_or_multiplex)
  super
  @used_fields = Set.new
end

Instance Attribute Details

#used_fieldsObject (readonly)

Returns the value of attribute used_fields.



38
39
40
# File 'lib/graphql-hive/analyzer.rb', line 38

def used_fields
  @used_fields
end

Instance Method Details

#on_enter_argument(node, parent, visitor) ⇒ Object

Visitor also calls ‘on_enter_argument’ when visiting input object fields in arguments



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/graphql-hive/analyzer.rb', line 21

def on_enter_argument(node, parent, visitor)
  arg_type = visitor.argument_definition.type.unwrap
  @used_fields.add(arg_type.graphql_name)

  # collect field argument path
  # input object fields won't have a "parent.name" method available
  if parent.respond_to?(:name)
    @used_fields.add(make_id(visitor.parent_type_definition.graphql_name, parent.name, node.name))
  end

  if arg_type.kind.input_object?
    collect_input_object_fields(node, arg_type)
  elsif arg_type.kind.enum?
    collect_enum_values(node, arg_type)
  end
end

#on_enter_field(node, _parent, visitor) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/graphql-hive/analyzer.rb', line 12

def on_enter_field(node, _parent, visitor)
  parent_type = visitor.parent_type_definition
  if parent_type&.respond_to?(:graphql_name) && node&.respond_to?(:name)
    @used_fields.add(parent_type.graphql_name)
    @used_fields.add(make_id(parent_type.graphql_name, node.name))
  end
end

#resultObject



40
41
42
# File 'lib/graphql-hive/analyzer.rb', line 40

def result
  @used_fields
end