public IValue Value(GraphQLValue source)
        {
            switch (source.Kind)
            {
            case ASTNodeKind.StringValue:
            {
                var str = source as GraphQLScalarValue;
                Debug.Assert(str != null, nameof(str) + " != null");

                return(new StringValue(str.Value).WithLocation(str, _body));
            }

            case ASTNodeKind.IntValue:
            {
                var str = source as GraphQLScalarValue;
                Debug.Assert(str != null, nameof(str) + " != null");

                if (int.TryParse(str.Value, out var intResult))
                {
                    var val = new IntValue(intResult).WithLocation(str, _body);
                    return(val);
                }

                // If the value doesn't fit in an integer, revert to using long...
                if (long.TryParse(str.Value, out var longResult))
                {
                    var val = new LongValue(longResult).WithLocation(str, _body);
                    return(val);
                }

                throw new ExecutionError($"Invalid number {str.Value}");
            }

            case ASTNodeKind.FloatValue:
            {
                var str = source as GraphQLScalarValue;
                Debug.Assert(str != null, nameof(str) + " != null");
                return(new FloatValue(ValueConverter.ConvertTo <double>(str.Value)).WithLocation(str, _body));
            }

            case ASTNodeKind.BooleanValue:
            {
                var str = source as GraphQLScalarValue;
                Debug.Assert(str != null, nameof(str) + " != null");
                return(new BooleanValue(ValueConverter.ConvertTo <bool>(str.Value)).WithLocation(str, _body));
            }

            case ASTNodeKind.EnumValue:
            {
                var str = source as GraphQLScalarValue;
                Debug.Assert(str != null, nameof(str) + " != null");
                return(new EnumValue(str.Value).WithLocation(str, _body));
            }

            case ASTNodeKind.Variable:
            {
                var vari = source as GraphQLVariable;
                Debug.Assert(vari != null, nameof(vari) + " != null");
                return(new VariableReference(Name(vari.Name)).WithLocation(vari, _body));
            }

            case ASTNodeKind.ObjectValue:
            {
                var obj = source as GraphQLObjectValue;
                Debug.Assert(obj != null, nameof(obj) + " != null");
                var fields = obj.Fields.Select(ObjectField);
                return(new ObjectValue(fields).WithLocation(obj, _body));
            }

            case ASTNodeKind.ListValue:
            {
                var list = source as GraphQLListValue;
                Debug.Assert(list != null, nameof(list) + " != null");
                var values = list.Values.Select(Value);
                return(new ListValue(values).WithLocation(list, _body));
            }

            case ASTNodeKind.NullValue:
            {
                var str = source as GraphQLScalarValue;
                return(new NullValue().WithLocation(str, _body));
            }
            }

            throw new ExecutionError($"Unmapped value type {source.Kind}");
        }
Example #2
0
        public override object VisitValueWithVariable(GraphQLParser.ValueWithVariableContext context)
        {
            if (context.StringValue() != null)
            {
                var val = new StringValue(context.StringValue().GetText());
                NewNode(val, context);
                return(val);
            }

            if (context.IntValue() != null)
            {
                var value = context.IntValue().GetText();
                int intResult;
                if (int.TryParse(value, out intResult))
                {
                    var val = new IntValue(intResult);
                    NewNode(val, context);
                    return(val);
                }

                // If the value doesn't fit in an integer, revert to using long...
                long longResult;
                if (long.TryParse(value, out longResult))
                {
                    var val = new LongValue(intResult);
                    NewNode(val, context);
                    return(val);
                }
            }

            if (context.FloatValue() != null)
            {
                var val = new FloatValue(float.Parse(context.FloatValue().GetText()));
                NewNode(val, context);
                return(val);
            }

            if (context.BooleanValue() != null)
            {
                var val = new BooleanValue(bool.Parse(context.BooleanValue().GetText()));
                NewNode(val, context);
                return(val);
            }

            if (context.enumValue() != null)
            {
                var val = new EnumValue(context.enumValue().GetText());
                NewNode(val, context);
                return(val);
            }

            if (context.variable() != null)
            {
                return(Visit(context.variable()));
            }

            if (context.arrayValueWithVariable() != null)
            {
                return(Visit(context.arrayValueWithVariable()));
            }

            if (context.objectValueWithVariable() != null)
            {
                return(Visit(context.objectValueWithVariable()));
            }

            return(null);
        }
Example #3
0
 protected bool Equals(IntValue other)
 {
     return(Value == other.Value);
 }
Example #4
0
        public IValue Value(GraphQLValue source)
        {
            switch (source.Kind)
            {
            case ASTNodeKind.StringValue:
            {
                var str = source as GraphQLScalarValue;
                return(new StringValue($"{str.Value}").WithLocation(str, _body));
            }

            case ASTNodeKind.IntValue:
            {
                var str = source as GraphQLScalarValue;

                int intResult;
                if (int.TryParse(str.Value, out intResult))
                {
                    var val = new IntValue(intResult).WithLocation(str, _body);
                    return(val);
                }

                // If the value doesn't fit in an integer, revert to using long...
                long longResult;
                if (long.TryParse(str.Value, out longResult))
                {
                    var val = new LongValue(longResult).WithLocation(str, _body);
                    return(val);
                }

                throw new ExecutionError($"Invalid number {str.Value}");
            }

            case ASTNodeKind.FloatValue:
            {
                var str = source as GraphQLScalarValue;
                return(new FloatValue(double.Parse(str.Value)).WithLocation(str, _body));
            }

            case ASTNodeKind.BooleanValue:
            {
                var str = source as GraphQLScalarValue;
                return(new BooleanValue(bool.Parse(str.Value)).WithLocation(str, _body));
            }

            case ASTNodeKind.EnumValue:
            {
                var str = source as GraphQLScalarValue;
                return(new EnumValue(str.Value).WithLocation(str, _body));
            }

            case ASTNodeKind.Variable:
            {
                var vari = source as GraphQLVariable;
                return(new VariableReference(Name(vari.Name)).WithLocation(vari, _body));
            }

            case ASTNodeKind.ObjectValue:
            {
                var obj    = source as GraphQLObjectValue;
                var fields = obj.Fields.Select(ObjectField);
                return(new ObjectValue(fields).WithLocation(obj, _body));
            }

            case ASTNodeKind.ListValue:
            {
                var list   = source as GraphQLListValue;
                var values = list.Values.Select(Value);
                return(new ListValue(values).WithLocation(list, _body));
            }
            }

            throw new ExecutionError($"Unmapped value type {source.Kind}");
        }