angel-dart-archive / graphql

moved to angel-dart/angel/packages/graphql

Home Page:https://github.com/angel-dart/angel/tree/master/packages/graphql

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

graphql_parser lexer incorrectly tokenizes variables named "query", "fragment" or other non-reserved words

dustin-graham opened this issue · comments

I'm trying to parse an operation for use with the GitHub GraphQL API.

query searchRepos($queryString: String!, $repositoryOrder: RepositoryOrder, $first: Int!) {
  search(type: REPOSITORY, query: $queryString, first: $first) {
    ...SearchResultItemConnection
  }
}

I am parsing this document using the following function:

  DocumentContext parseDocument(String doc) {
    var tokens = scan(doc);
    var parser = new Parser(tokens);

    if (parser.errors.isNotEmpty) {
      throw "invalid document";
    }

    return parser.parseDocument();
  }

However, this returns 0 definitions. I found the issue while debugging. The lexer interprets any token named query as a TokenType.QUERY regardless of position.

Screen Shot 2019-03-30 at 8 10 37 AM

This classification breaks the parsing logic when it encounters my variables named "query". I believe this is an error because these words are not reserved in the GraphQL language. Unfortunately I don't believe this is something that can be worked around in this case as the GitHub API declares this variable name and is out of my control.

The document parser should be able to recognize a token named "query", "fragment", "mutation", "subscription", "on", and "null" as a TokenType.NAME when placed in a valid variable or alias location.

Thanks for filing this issue. Indeed, on a closer look, these are not reserved names:
https://graphql.github.io/graphql-spec/June2018/#sec-Reserved-Names

I'll have to patch this up soon.

It'll be easy-ish enough, though.

Resolved in package:graphql_parser@1.1.3: https://pub.dartlang.org/packages/graphql_parser

Also see the test file for this change; I used the snippet you provided:
https://github.com/angel-dart/graphql/blob/master/graphql_parser/test/next_name_test.dart