Example #1
0
        private static ExpressionResult CompileQuery(string query, Expression context, ISchemaProvider schemaProvider, IMethodProvider methodProvider, QueryVariables variables)
        {
            AntlrInputStream stream = new AntlrInputStream(query);
            var lexer = new EntityGraphQLLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new EntityGraphQLParser(tokens);
            parser.BuildParseTree = true;
            var tree = parser.startRule();

            var visitor = new QueryGrammerNodeVisitor(context, schemaProvider, methodProvider, variables);
            var expression = visitor.Visit(tree);
            return expression;
        }
        private static ExpressionResult CompileQuery(string query, Expression context, ISchemaProvider schemaProvider, ClaimsIdentity claims, IMethodProvider methodProvider, QueryVariables variables)
        {
            AntlrInputStream stream = new AntlrInputStream(query);
            var lexer  = new EntityGraphQLLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new EntityGraphQLParser(tokens)
            {
                BuildParseTree = true
            };
            var tree = parser.eqlStart();

            var visitor    = new EntityQueryNodeVisitor(context, schemaProvider, methodProvider, variables, claims);
            var expression = visitor.Visit(tree);

            return(expression);
        }
Example #3
0
        public IGraphQLNode Compile(QueryRequest request)
        {
            // Setup our Antlr parser
            var stream = new AntlrInputStream(request.Query);
            var lexer  = new EntityGraphQLLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new EntityGraphQLParser(tokens);

            parser.BuildParseTree = true;
            parser.ErrorHandler   = new BailErrorStrategy();
            try
            {
                var tree    = parser.graphQL();
                var visitor = new GraphQLVisitor(_schemaProvider, _methodProvider, request.Variables);
                // visit each node. it will return a linq expression for each entity requested
                var node = visitor.Visit(tree);
                return(node);
            }
            catch (ParseCanceledException pce)
            {
                if (pce.InnerException != null)
                {
                    if (pce.InnerException is NoViableAltException)
                    {
                        var nve = (NoViableAltException)pce.InnerException;
                        throw new EntityGraphQLCompilerException($"Error: line {nve.OffendingToken.Line}:{nve.OffendingToken.Column} no viable alternative at input '{nve.OffendingToken.Text}'");
                    }
                    else if (pce.InnerException is InputMismatchException)
                    {
                        var ime       = (InputMismatchException)pce.InnerException;
                        var expecting = string.Join(", ", ime.GetExpectedTokens());
                        throw new EntityGraphQLCompilerException($"Error: line {ime.OffendingToken.Line}:{ime.OffendingToken.Column} extraneous input '{ime.OffendingToken.Text}' expecting {expecting}");
                    }
                    System.Console.WriteLine(pce.InnerException.GetType());
                    throw new EntityGraphQLCompilerException(pce.InnerException.Message);
                }
                throw new EntityGraphQLCompilerException(pce.Message);
            }
        }