Module: GraphQL::Libgraphqlparser

Defined in:
lib/graphql/libgraphqlparser.rb,
lib/graphql/libgraphqlparser/builder.rb,
lib/graphql/libgraphqlparser/version.rb,
ext/graphql_libgraphqlparser_ext/graphql_libgraphqlparser_ext.c

Defined Under Namespace

Classes: Builder, ParseError

Constant Summary collapse

VERSION =
'0.5.1'

Class Method Summary collapse

Class Method Details

.builder_parse(query_string) ⇒ Object

GraphQL::Nodes::Document instance.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'ext/graphql_libgraphqlparser_ext/graphql_libgraphqlparser_ext.c', line 15

VALUE GraphQL_Libgraphqlparser_parse(VALUE self, VALUE query_string) {
  const char* c_query_string = StringValueCStr(query_string);
  const char* parse_error_message;
  VALUE exception;
  VALUE builder = Qnil;

  struct GraphQLAstNode *parse_result = graphql_parse_string(c_query_string, &parse_error_message);

  if (parse_result == NULL) {
    exception = rb_exc_new_cstr(Libgraphqlparser_ParseError, parse_error_message);
    graphql_error_free(parse_error_message);
    rb_exc_raise(exception);
  } else {
    builder = rb_class_new_instance(0, NULL, Libgraphqlparser_Builder);
    graphql_node_visit(parse_result, &Libgraphqlparser_Callbacks, (void*)builder);
  }

  return builder;
}

.parse(string) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/graphql/libgraphqlparser.rb', line 9

def self.parse(string)
  begin
    builder = builder_parse(string)
    builder.document
  rescue ArgumentError
    if index = string.index("\x00")
      string_before_null = string.slice(0, index)
      line = string_before_null.count("\n") + 1
      col = index - (string_before_null.rindex("\n") || 0)
      raise GraphQL::ParseError.new("Invalid null byte in query", line, col, string)
    end
    raise
  rescue ParseError => e
    error_message = e.message.match(/(\d+)\.(\d+): (.*)/)
    if error_message
      line = error_message[1].to_i
      col = error_message[2].to_i
      raise GraphQL::ParseError.new(error_message[3], line, col, string)
    else
      raise GraphQL::ParseError.new(e.message, nil, nil, string)
    end
  end
end