Class: GraphQL::Language::Parser

Inherits:
Object
  • Object
show all
Includes:
EmptyObjects, Nodes
Defined in:
lib/graphql/language/parser.rb

Direct Known Subclasses

SchemaParser

Defined Under Namespace

Classes: SchemaParser

Constant Summary

Constants included from EmptyObjects

EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH

Constants included from Nodes

Nodes::NONE

Class Attribute Summary collapse

Attributes included from Nodes

#name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graphql_str, filename: nil, trace: Tracing::NullTrace, max_tokens: nil) ⇒ Parser

Returns a new instance of Parser.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/graphql/language/parser.rb', line 30

def initialize(graphql_str, filename: nil, trace: Tracing::NullTrace, max_tokens: nil)
  if graphql_str.nil?
    raise GraphQL::ParseError.new("No query string was present", nil, nil, nil)
  end
  @lexer = Lexer.new(graphql_str, filename: filename, max_tokens: max_tokens)
  @graphql_str = graphql_str
  @filename = filename
  @trace = trace
  @dedup_identifiers = false
  @lines_at = nil
end

Class Attribute Details

.cacheObject

Returns the value of attribute cache.



13
14
15
# File 'lib/graphql/language/parser.rb', line 13

def cache
  @cache
end

Class Method Details

.parse(graphql_str, filename: nil, trace: Tracing::NullTrace, max_tokens: nil) ⇒ Object



15
16
17
# File 'lib/graphql/language/parser.rb', line 15

def parse(graphql_str, filename: nil, trace: Tracing::NullTrace, max_tokens: nil)
  self.new(graphql_str, filename: filename, trace: trace, max_tokens: max_tokens).parse
end

.parse_file(filename, trace: Tracing::NullTrace) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/graphql/language/parser.rb', line 19

def parse_file(filename, trace: Tracing::NullTrace)
  if cache
    cache.fetch(filename) do
      parse(File.read(filename), filename: filename, trace: trace)
    end
  else
    parse(File.read(filename), filename: filename, trace: trace)
  end
end

Instance Method Details

#column_at(pos) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/graphql/language/parser.rb', line 61

def column_at(pos)
  next_line_idx = lines_at.bsearch_index { |l| l >= pos } || 0
  if next_line_idx > 0
    line_pos = @lines_at[next_line_idx - 1]
    pos - line_pos
  else
    pos + 1
  end
end

#line_at(pos) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/graphql/language/parser.rb', line 52

def line_at(pos)
  line = lines_at.bsearch_index { |l| l >= pos }
  if line.nil?
    @lines_at.size + 1
  else
    line + 1
  end
end

#parseObject



42
43
44
45
46
47
48
49
50
# File 'lib/graphql/language/parser.rb', line 42

def parse
  @document ||= begin
    @trace.parse(query_string: @graphql_str) do
      document
    end
  rescue SystemStackError
    raise GraphQL::ParseError.new("This query is too large to execute.", nil, nil, @query_str, filename: @filename)
  end
end