Class: GraphQL::Language::StaticVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/language/static_visitor.rb

Overview

Like GraphQL::Language::Visitor except it doesn't support making changes to the document -- only visiting it as-is.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ StaticVisitor

Returns a new instance of StaticVisitor.



9
10
11
# File 'lib/graphql/language/static_visitor.rb', line 9

def initialize(document)
  @document = document
end

Class Method Details

.make_visit_methods(ast_node_class) ⇒ Object

We don't use alias here because it breaks super



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/graphql/language/static_visitor.rb', line 104

def self.make_visit_methods(ast_node_class)
  node_method = ast_node_class.visit_method
  children_of_type = ast_node_class.children_of_type
  child_visit_method = :"#{node_method}_children"

  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
    # The default implementation for visiting an AST node.
    # It doesn't _do_ anything, but it continues to visiting the node's children.
    # To customize this hook, override one of its make_visit_methods (or the base method?)
    # in your subclasses.
    #
    # **Parameters**
    #
    # - `node` (`GraphQL::Language::Nodes::AbstractNode`) — the node being visited
    # - `parent` (`GraphQL::Language::Nodes::AbstractNode, nil`) — the previously-visited node, or `nil` if this is the root node.
    #
    # **Returns**
    #
    # - `void`
    #
    # :call-seq:
    #   #{node_method}(GraphQL::Language::Nodes::AbstractNode node, GraphQL::Language::Nodes::AbstractNode | nil parent) -> void
    def #{node_method}(node, parent)
      #{
        if method_defined?(child_visit_method)
          "#{child_visit_method}(node)"
        elsif children_of_type
          children_of_type.map do |child_accessor, child_class|
            "node.#{child_accessor}.each do |child_node|
              #{child_class.visit_method}(child_node, node)
            end"
          end.join("\n")
        else
          ""
        end
      }
    end
  RUBY
end

Instance Method Details

#on_argument_children(new_node) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/graphql/language/static_visitor.rb', line 84

def on_argument_children(new_node)
  new_node.children.each do |value_node|
    case value_node
    when Language::Nodes::VariableIdentifier
      on_variable_identifier(value_node, new_node)
    when Language::Nodes::InputObject
      on_input_object(value_node, new_node)
    when Language::Nodes::Enum
      on_enum(value_node, new_node)
    when Language::Nodes::NullValue
      on_null_value(value_node, new_node)
    else
      raise ArgumentError, "Invariant: unexpected argument value node #{value_node.class} (#{value_node.inspect})"
    end
  end
end

#on_document_children(document_node) ⇒ Object



33
34
35
36
37
38
# File 'lib/graphql/language/static_visitor.rb', line 33

def on_document_children(document_node)
  document_node.children.each do |child_node|
    visit_method = child_node.visit_method
    public_send(visit_method, child_node, document_node)
  end
end

#on_field_children(new_node) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/graphql/language/static_visitor.rb', line 40

def on_field_children(new_node)
  new_node.arguments.each do |arg_node| # rubocop:disable Development/ContextIsPassedCop
    on_argument(arg_node, new_node)
  end
  visit_directives(new_node)
  visit_selections(new_node)
end

#on_fragment_definition_children(new_node) ⇒ Object Also known as: on_inline_fragment_children



69
70
71
72
# File 'lib/graphql/language/static_visitor.rb', line 69

def on_fragment_definition_children(new_node)
  visit_directives(new_node)
  visit_selections(new_node)
end

#on_operation_definition_children(new_node) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/graphql/language/static_visitor.rb', line 76

def on_operation_definition_children(new_node)
  new_node.variables.each do |arg_node|
    on_variable_definition(arg_node, new_node)
  end
  visit_directives(new_node)
  visit_selections(new_node)
end

#visit ⇒ Object

Visit document and all children

Returns

  • void

:call-seq:

visit() -> void


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/graphql/language/static_visitor.rb', line 21

def visit
  # `@document` may be any kind of node:
  visit_method = @document.visit_method
  result = public_send(visit_method, @document, nil)
  @result = if result.is_a?(Array)
    result.first
  else
    # The node wasn't modified
    @document
  end
end

#visit_directives(new_node) ⇒ Object



48
49
50
51
52
# File 'lib/graphql/language/static_visitor.rb', line 48

def visit_directives(new_node)
  new_node.directives.each do |dir_node|
    on_directive(dir_node, new_node)
  end
end

#visit_selections(new_node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/graphql/language/static_visitor.rb', line 54

def visit_selections(new_node)
  new_node.selections.each do |selection|
    case selection
    when GraphQL::Language::Nodes::Field
      on_field(selection, new_node)
    when GraphQL::Language::Nodes::InlineFragment
      on_inline_fragment(selection, new_node)
    when GraphQL::Language::Nodes::FragmentSpread
      on_fragment_spread(selection, new_node)
    else
      raise ArgumentError, "Invariant: unexpected field selection #{selection.class} (#{selection.inspect})"
    end
  end
end