Example #1
0
        /// We compile each entityQuery with EqlCompiler and build a Select call from the fields
        public override IGraphQLNode VisitEntityQuery(EntityGraphQLParser.EntityQueryContext context)
        {
            string name;
            string query;

            if (context.alias != null)
            {
                name  = context.alias.name.GetText();
                query = context.entity.GetText();
            }
            else
            {
                query = context.entity.GetText();
                name  = query;
                if (name.IndexOf(".") > -1)
                {
                    name = name.Substring(0, name.IndexOf("."));
                }
                if (name.IndexOf("(") > -1)
                {
                    name = name.Substring(0, name.IndexOf("("));
                }
            }

            try
            {
                QueryResult result = null;
                if (selectContext == null)
                {
                    // top level are queries on the context
                    result = EqlCompiler.Compile(query, schemaProvider, methodProvider, variables);
                }
                else
                {
                    result = EqlCompiler.CompileWith(query, selectContext, schemaProvider, methodProvider, variables);
                }
                var exp = result.LambdaExpression.Body;

                IGraphQLNode graphQLNode = null;
                if (exp.Type.IsEnumerable())
                {
                    graphQLNode = BuildDynamicSelectOnCollection(result, name, context, true);
                }
                else
                {
                    // Could be a list.First() that we need to turn into a select, or
                    // other levels are object selection. e.g. from the top level people query I am selecting all their children { field1, etc. }
                    // Can we turn a list.First() into and list.Select().First()
                    var listExp = Compiler.Util.ExpressionUtil.FindIEnumerable(result.LambdaExpression.Body);
                    if (listExp.Item1 != null)
                    {
                        // yes we can
                        graphQLNode = BuildDynamicSelectOnCollection(new QueryResult((ExpressionResult)listExp.Item1, result.ContextParams, result.ConstantParameterValues), name, context, true);
                        graphQLNode.NodeExpression = (ExpressionResult)Compiler.Util.ExpressionUtil.CombineExpressions(graphQLNode.NodeExpression, listExp.Item2);
                    }
                    else
                    {
                        graphQLNode = BuildDynamicSelectForObjectGraph(query, name, context, result);
                    }
                }
                // the query result may be a mutation
                if (result.IsMutation)
                {
                    return(new GraphQLMutationNode(result, graphQLNode));
                }
                return(graphQLNode);
            }
            catch (EntityGraphQLCompilerException ex)
            {
                throw SchemaException.MakeFieldCompileError(query, ex.Message);
            }
        }