Ejemplo n.º 1
0
        public override IGraphQLBaseNode VisitAliasExp(EntityGraphQLParser.AliasExpContext context)
        {
            var name  = context.alias.name.GetText();
            var query = context.entity.GetText();

            if (selectContext == null)
            {
                // top level are queries on the context
                var exp  = EqlCompiler.Compile(query, schemaProvider, methodProvider, variables);
                var node = new GraphQLNode(schemaProvider, fragments, name, exp, null);
                return(node);
            }
            else
            {
                var result = EqlCompiler.CompileWith(query, selectContext, schemaProvider, methodProvider, variables);
                var node   = new GraphQLNode(schemaProvider, fragments, name, result, null);
                return(node);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// We compile each entityQuery with EqlCompiler and build a Select call from the fields
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override IGraphQLBaseNode 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
            {
                CompiledQueryResult 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.ExpressionResult;

                IGraphQLNode graphQLNode = null;
                if (exp.Type.IsEnumerableOrArray())
                {
                    graphQLNode = BuildDynamicSelectOnCollection(result, name, context);
                }
                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.ExpressionResult);
                    if (listExp.Item1 != null)
                    {
                        // yes we can
                        // rebuild the ExpressionResult so we keep any ConstantParameters
                        var item1 = (ExpressionResult)listExp.Item1;
                        item1.AddConstantParameters(result.ExpressionResult.ConstantParameters);
                        graphQLNode = BuildDynamicSelectOnCollection(new CompiledQueryResult(item1, result.ContextParams), name, context);
                        graphQLNode.SetNodeExpression((ExpressionResult)Compiler.Util.ExpressionUtil.CombineExpressions(graphQLNode.GetNodeExpression(), 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);
            }
        }