Esempio n. 1
0
        public override ExpressionResult VisitGqlarg(EntityGraphQLParser.GqlargContext context)
        {
            if (context.gqlVar() != null)
            {
                string varKey = context.gqlVar().GetText().TrimStart('$');
                object value  = variables.GetValueFor(varKey);
                var    exp    = (ExpressionResult)Expression.Constant(value);
                if (value != null && value.GetType() == typeof(string) && guidRegex.IsMatch((string)value))
                {
                    exp = ConvertToGuid(exp);
                }
                return(exp);
            }
            var enumName = context.gqlvalue.GetText();
            var argType  = fieldArgumentContext.GetArgumentType(context.gqlfield.GetText());

            if (!argType.GetTypeInfo().IsEnum)
            {
                // could be a constant or some other compilable expression
                return(Visit(context.gqlvalue));
            }
            var valueIndex = Enum.GetNames(argType).ToList().FindIndex(n => n.ToLower() == enumName.ToLower());

            if (valueIndex == -1)
            {
                throw new EntityGraphQLCompilerException($"Value {enumName} is not valid for argument {context.gqlfield}");
            }
            var enumValue = Enum.GetValues(argType).GetValue(valueIndex);

            return((ExpressionResult)Expression.Constant(enumValue));
        }
        public ExpressionResult ParseGqlarg(IMethodType fieldArgumentContext, EntityGraphQLParser.GqlargContext context)
        {
            ExpressionResult gqlVarValue = null;

            if (context.gqlVar() != null)
            {
                string varKey = context.gqlVar().GetText().TrimStart('$');
                object value  = variables.GetValueFor(varKey);
                gqlVarValue = (ExpressionResult)Expression.Constant(value);
            }
            else
            {
                // this is an expression
                gqlVarValue = constantVisitor.Visit(context.gqlvalue);
            }

            string argName = context.gqlfield.GetText();

            if (fieldArgumentContext != null && fieldArgumentContext.HasArgumentByName(argName))
            {
                var argType = fieldArgumentContext.GetArgumentType(argName);

                if (gqlVarValue != null && gqlVarValue.Type == typeof(string) && gqlVarValue.NodeType == ExpressionType.Constant)
                {
                    string strValue = (string)((ConstantExpression)gqlVarValue).Value;
                    if (
                        (argType.Type == typeof(Guid) || argType.Type == typeof(Guid?) ||
                         argType.Type == typeof(RequiredField <Guid>) || argType.Type == typeof(RequiredField <Guid?>)) && ConstantVisitor.GuidRegex.IsMatch(strValue))
                    {
                        return((ExpressionResult)Expression.Constant(Guid.Parse(strValue)));
                    }
                    if (argType.Type.IsConstructedGenericType && argType.Type.GetGenericTypeDefinition() == typeof(EntityQueryType <>))
                    {
                        string query = strValue;
                        if (query.StartsWith("\""))
                        {
                            query = query.Substring(1, context.gqlvalue.GetText().Length - 2);
                        }
                        return(BuildEntityQueryExpression(fieldArgumentContext, fieldArgumentContext.Name, argName, query));
                    }

                    var argumentNonNullType = argType.Type.IsNullableType() ? Nullable.GetUnderlyingType(argType.Type) : argType.Type;
                    if (argumentNonNullType.GetTypeInfo().IsEnum)
                    {
                        var enumName   = strValue;
                        var valueIndex = Enum.GetNames(argumentNonNullType).ToList().FindIndex(n => n == enumName);
                        if (valueIndex == -1)
                        {
                            throw new EntityGraphQLCompilerException($"Value {enumName} is not valid for argument {context.gqlfield.GetText()}");
                        }
                        var enumValue = Enum.GetValues(argumentNonNullType).GetValue(valueIndex);
                        return((ExpressionResult)Expression.Constant(enumValue));
                    }
                }
            }
            return(gqlVarValue);
        }