Module: GQLParser

Defined in:
ext/gql_parser.c

Defined Under Namespace

Classes: ParserError

Constant Summary collapse

VERSION =
rb_str_new2("October 2021")

Class Method Summary collapse

Class Method Details

.parse_execution(document) ⇒ Object

EXECUTION DOCUMENT [OPERATION*, FRAGMENT*]



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/gql_parser.c', line 71

VALUE gql_parse_execution(VALUE self, VALUE document)
{
  if (!RB_TYPE_P(document, T_STRING))
    rb_raise(rb_eArgError, "%+" PRIsVALUE " is not a string", document);

  // Initialize its pieces
  VALUE pieces[] = {Qnil, Qnil};
  struct gql_scanner scanner = gql_new_scanner(document);
  gql_next_lexeme_no_comments(&scanner);

  // Go over all the operations and fragments
  while (scanner.lexeme != gql_i_eof)
  {
    // Try to upgrade if the token is a name
    if (scanner.lexeme == gql_i_name)
      scanner.lexeme = GQL_SAFE_NAME_TO_KEYWORD(&scanner, GQL_EXECUTION_KEYWORDS);

    // It can contain either operations or fragments, anything else is unknown and an error
    if (QGL_I_OPERATION(scanner.lexeme) || scanner.lexeme == gql_is_op_curly)
      GQL_SAFE_PUSH(pieces[0], gql_parse_operation(&scanner));
    else if (scanner.lexeme == gql_ie_fragment)
      GQL_SAFE_PUSH(pieces[1], gql_parse_fragment(&scanner));
    else if (scanner.lexeme != gql_i_comment)
      scanner.lexeme = gql_i_unknown;

    // If anything made the scanner fall into an unknown, throw an error
    if (scanner.lexeme == gql_i_unknown)
      gql_throw_parser_error(&scanner);
  }

  // Return the plain array, no need to turn into a token
  return rb_ary_new4(2, pieces);
}