Class: GraphQL::Libgraphqlparser::Builder

Inherits:
Object
  • Object
show all
Includes:
GraphQL::Language
Defined in:
lib/graphql/libgraphqlparser/builder.rb

Overview

Keeps a stack of parse results, exposing the latest one. The C parser can call methods on this object, assuming they'll be applied to the right object.

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.


9
10
11
# File 'lib/graphql/libgraphqlparser/builder.rb', line 9

def initialize
  @ast_stack = []
end

Instance Method Details

#add_value(string_value) ⇒ Object

This is for convenience from C


66
67
68
# File 'lib/graphql/libgraphqlparser/builder.rb', line 66

def add_value(string_value)
  current.value = string_value
end

#begin_visit(node_class_name) ⇒ Object


21
22
23
24
25
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
55
56
57
# File 'lib/graphql/libgraphqlparser/builder.rb', line 21

def begin_visit(node_class_name)
  # p "--> BEGIN #{node_class_name}"
  node_class = Nodes.const_get(node_class_name)
  node = node_class.new
  case node
  when Nodes::OperationDefinition, Nodes::FragmentDefinition
    current.definitions << node
  when Nodes::VariableDefinition
    current.variables << node
  when Nodes::VariableIdentifier
    if current.is_a?(Nodes::VariableDefinition)
      node = current
    else
      current.value = node
    end
  when Nodes::Directive
    current.directives << node
  when Nodes::Argument
    current.arguments << node
  when Nodes::InlineFragment, Nodes::FragmentSpread, Nodes::Field
    current.selections << node
  when Nodes::TypeName, Nodes::ListType, Nodes::NonNullType
    # Using ||= because FragmentDefinition has already assigned
    # this as a plain string :(
    current.type ||= node
  when Nodes::ListLiteral
    # mutability! 🎉
    current.value = node.values
  when Nodes::InputObject
    current.value = node
  when Nodes::Enum
    current.value = node
  end

  @ast_stack.push(node)
  node
end

#currentObject


17
18
19
# File 'lib/graphql/libgraphqlparser/builder.rb', line 17

def current
  @ast_stack.last
end

#documentObject


13
14
15
# File 'lib/graphql/libgraphqlparser/builder.rb', line 13

def document
  @ast_stack.first
end

#end_visitObject


59
60
61
62
63
# File 'lib/graphql/libgraphqlparser/builder.rb', line 59

def end_visit
  removed = @ast_stack.pop
  # p "--> END   #{removed}"
  removed
end