Class: PlatformosCheck::Tags::Graphql

Inherits:
Base
  • Object
show all
Defined in:
lib/platformos_check/tags/graphql.rb

Defined Under Namespace

Classes: ParseTreeVisitor

Constant Summary collapse

QUERY_NAME_SYNTAX =
/(#{Liquid::VariableSignature}+)\s*=\s*(.*)\s*/om
INLINE_SYNTAX =
/(#{Liquid::QuotedFragment}+)(\s*(#{Liquid::QuotedFragment}+))?/o
INLINE_SYNTAX_WITHOUT_RESULT_VARIABLE =
/\A([\w\-\.\[\]])+\s*:\s*/om
CLOSE_TAG_SYNTAX =

based on Liquid::Raw::FullTokenPossiblyInvalid

/\A(.*)(?-mix:\{%-?)\s*(\w+)\s*(.*)?(?-mix:%\})\z/m

Constants included from BaseTagMethods

BaseTagMethods::BACKWARDS_COMPATIBILITY_KEYS, BaseTagMethods::SYNTAX, BaseTagMethods::TAG_ATTRIBUTES

Instance Attribute Summary collapse

Attributes included from BaseTagMethods

#duplicated_attrs, #main_value

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ Graphql

Returns a new instance of Graphql.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/platformos_check/tags/graphql.rb', line 13

def initialize(tag_name, markup, options)
  super
  if markup =~ QUERY_NAME_SYNTAX
    @to = Regexp.last_match(1)
    @inline_query = false

    # inline query looks like this:
    # {% graph res = "my_query", id: "1" | dig: 'my_query' %}
    # we want to first process "my_query, id: "1" , store it in "res" and then process
    # it with filters like this:
    # res | dig: 'my_query'
    after_assign_markup = Regexp.last_match(2).split('|')
    parse_markup(tag_name, after_assign_markup.shift)
    @attributes = attributes_expr.keys

    after_assign_markup.unshift(@to)
    @partial_name = value_expr
    @from = Liquid::Variable.new(after_assign_markup.join('|'), options)
  elsif INLINE_SYNTAX.match?(markup)
    raise Liquid::SyntaxError, 'Invalid syntax for inline graphql tag - missing result name. Valid syntax: graphql result, arg1: var1, ...' if markup.match?(INLINE_SYNTAX_WITHOUT_RESULT_VARIABLE)

    @inline_query = true
    parse_markup(tag_name, markup)
    @attributes = attributes_expr.keys
    @to = @value_expr.is_a?(String) ? @value_expr : @value_expr.name
  else
    raise Liquid::SyntaxError, 'Invalid syntax for graphql tag'
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



11
12
13
# File 'lib/platformos_check/tags/graphql.rb', line 11

def attributes
  @attributes
end

#attributes_exprObject (readonly)

Returns the value of attribute attributes_expr.



11
12
13
# File 'lib/platformos_check/tags/graphql.rb', line 11

def attributes_expr
  @attributes_expr
end

#fromObject (readonly)

Returns the value of attribute from.



11
12
13
# File 'lib/platformos_check/tags/graphql.rb', line 11

def from
  @from
end

#inline_queryObject (readonly)

Returns the value of attribute inline_query.



11
12
13
# File 'lib/platformos_check/tags/graphql.rb', line 11

def inline_query
  @inline_query
end

#partial_nameObject (readonly)

Returns the value of attribute partial_name.



11
12
13
# File 'lib/platformos_check/tags/graphql.rb', line 11

def partial_name
  @partial_name
end

#toObject (readonly)

Returns the value of attribute to.



11
12
13
# File 'lib/platformos_check/tags/graphql.rb', line 11

def to
  @to
end

#value_exprObject (readonly)

Returns the value of attribute value_expr.



11
12
13
# File 'lib/platformos_check/tags/graphql.rb', line 11

def value_expr
  @value_expr
end

Instance Method Details

#block_delimiterObject



62
63
64
# File 'lib/platformos_check/tags/graphql.rb', line 62

def block_delimiter
  @block_delimiter = "end#{block_name}"
end

#block_nameObject



58
59
60
# File 'lib/platformos_check/tags/graphql.rb', line 58

def block_name
  @tag_name
end

#parse(tokens) ⇒ Object

Raises:

  • (Liquid::SyntaxError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/platformos_check/tags/graphql.rb', line 43

def parse(tokens)
  return super unless @inline_query

  @body = +''
  while (token = tokens.send(:shift))
    if token =~ CLOSE_TAG_SYNTAX && block_delimiter == Regexp.last_match(2)
      @body << Regexp.last_match(1) if Regexp.last_match(1) != ''
      return
    end
    @body << token unless token.empty?
  end

  raise Liquid::SyntaxError, parse_context.locale.t('errors.syntax.tag_never_closed', block_name:)
end