Class: GraphQL::RemoteLoader::QueryMerger::QueryAST

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/remote_loader/query_merger.rb

Defined Under Namespace

Classes: ParseException

Class Method Summary collapse

Class Method Details

.build(tokenizer) ⇒ Object



94
95
96
# File 'lib/graphql/remote_loader/query_merger.rb', line 94

def build(tokenizer)
  result = build_node(tokenizer)
end

.build_body(tokenizer) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/graphql/remote_loader/query_merger.rb', line 168

def build_body(tokenizer)
  body = []
  return body unless (tokenizer.peek[:type] == :left_brace)

  # remove "{"
  tokenizer.pop

  while true
    body << build_node(tokenizer)
    break if (tokenizer.peek[:type] == :right_brace)
  end

  # remove "}"
  tokenizer.pop

  body
end

.build_node(tokenizer) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/graphql/remote_loader/query_merger.rb', line 98

def build_node(tokenizer)
  {
    field: get_field(tokenizer),
    arguments: get_args(tokenizer),
    body: build_body(tokenizer)
  }
end

.get_args(tokenizer) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/graphql/remote_loader/query_merger.rb', line 123

def get_args(tokenizer)
  args = {}
  return args unless (tokenizer.peek[:type] == :left_paren)

  # remove "("
  tokenizer.pop

  while true
    token = tokenizer.pop
    validate_token_type(token, :key)

    # Remove :
    key = token[:string].sub(":", "")

    token = tokenizer.pop
    value = case token[:type]
            when :string
              token[:string][1..-2] # remove ""
            when :int
              Integer(token[:string])
            when :float
              Float(token[:string])
            when :true
              true
            when :false
              false
            else
              raise ParseException, "Expected string, int, float, or boolean, got #{token[:string]}"
            end

    args[key.to_sym] = value

    if tokenizer.peek[:type] == :comma
      tokenizer.pop
    elsif tokenizer.peek[:type] == :right_paren
      tokenizer.pop
      break
    else
      raise ParseException, "Expected comma or right paren, got #{tokenizer[:string]}"
    end
  end

  args
end

.get_field(tokenizer) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/graphql/remote_loader/query_merger.rb', line 106

def get_field(tokenizer)
  token = tokenizer.pop

  if token[:type] == :spread
    validate_token_type(tokenizer.pop, :on)
    spread_type = tokenizer.pop[:string]

    return "... on #{spread_type}"
  end

  validate_token_type(token, :field)

  throw_parse_exception(token, "field") unless (token[:type] == :field)

  token[:string]
end

.validate_token_type(token, expected_type) ⇒ Object



186
187
188
189
190
# File 'lib/graphql/remote_loader/query_merger.rb', line 186

def validate_token_type(token, expected_type)
  unless token[:type] == expected_type
    raise ParseException, "Expected #{expected_type.to_s} token, got #{token[:string]}"
  end
end