예제 #1
0
        public object CoerceValue(ISchema schema, GraphType type, object input, Variables variables = null)
        {
            if (type is NonNullGraphType)
            {
                var nonNull = type as NonNullGraphType;
                return(CoerceValue(schema, schema.FindType(nonNull.Type), input, variables));
            }

            if (input == null)
            {
                return(null);
            }

            var variable = input as Variable;

            if (variable != null)
            {
                return(variables != null
                    ? variables.ValueFor(variable.Name)
                    : null);
            }

            if (type is ListGraphType)
            {
                var listType     = type as ListGraphType;
                var listItemType = schema.FindType(listType.Type);
                var list         = input as IEnumerable;
                return(list != null && !(input is string)
                    ? list.Map(item => CoerceValue(schema, listItemType, item, variables)).ToArray()
                    : new[] { CoerceValue(schema, listItemType, input, variables) });
            }

            if (type is ObjectGraphType || type is InputObjectGraphType)
            {
                var obj = new Dictionary <string, object>();

                if (input is KeyValuePair <string, object> )
                {
                    var kvp = (KeyValuePair <string, object>)input;
                    input = new Dictionary <string, object> {
                        { kvp.Key, kvp.Value }
                    };
                }

                var kvps = input as IEnumerable <KeyValuePair <string, object> >;
                if (kvps != null)
                {
                    input = kvps.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
                }

                var dict = input as Dictionary <string, object>;
                if (dict == null)
                {
                    return(null);
                }

                type.Fields.Apply(field =>
                {
                    object inputValue;
                    if (dict.TryGetValue(field.Name, out inputValue))
                    {
                        var fieldValue  = CoerceValue(schema, schema.FindType(field.Type), inputValue, variables);
                        obj[field.Name] = fieldValue ?? field.DefaultValue;
                    }
                });

                return(obj);
            }

            if (type is ScalarGraphType)
            {
                var scalarType = type as ScalarGraphType;
                return(scalarType.Coerce(input));
            }

            return(input);
        }
예제 #2
0
        public object CoerceValue(ISchema schema, GraphType type, IValue input, Variables variables = null)
        {
            if (type is NonNullGraphType)
            {
                var nonNull = type as NonNullGraphType;
                return(CoerceValue(schema, schema.FindType(nonNull.Type), input, variables));
            }

            if (input == null)
            {
                return(null);
            }

            var variable = input as VariableReference;

            if (variable != null)
            {
                return(variables != null
                    ? variables.ValueFor(variable.Name)
                    : null);
            }

            if (type is ListGraphType)
            {
                var listType     = type as ListGraphType;
                var listItemType = schema.FindType(listType.Type);
                var list         = input as ListValue;
                return(list != null
                    ? list.Values.Map(item => CoerceValue(schema, listItemType, item, variables)).ToArray()
                    : new[] { CoerceValue(schema, listItemType, input, variables) });
            }

            if (type is ObjectGraphType || type is InputObjectGraphType)
            {
                var obj = new Dictionary <string, object>();

                var objectValue = input as ObjectValue;
                if (objectValue == null)
                {
                    return(null);
                }

                type.Fields.Apply(field =>
                {
                    var objectField = objectValue.Field(field.Name);
                    if (objectField != null)
                    {
                        var fieldValue = CoerceValue(schema, schema.FindType(field.Type), objectField.Value, variables);
                        fieldValue     = fieldValue ?? field.DefaultValue;

                        obj[field.Name] = fieldValue;
                    }
                });

                return(obj);
            }

            if (type is ScalarGraphType)
            {
                var scalarType = type as ScalarGraphType;
                return(scalarType.ParseLiteral(input));
            }

            return(null);
        }
예제 #3
0
        public Dictionary <string, object> GetArgumentValues(ISchema schema, QueryArguments definitionArguments, Arguments astArguments, Variables variables)
        {
            if (definitionArguments == null || !definitionArguments.Any())
            {
                return(null);
            }

            return(definitionArguments.Aggregate(new Dictionary <string, object>(), (acc, arg) =>
            {
                var value = astArguments != null ? astArguments.ValueFor(arg.Name) : null;
                var type = schema.FindType(arg.Type);

                var coercedValue = CoerceValue(schema, type, value, variables);
                acc[arg.Name] = IsValidValue(schema, type, coercedValue)
                    ? coercedValue ?? arg.DefaultValue
                    : arg.DefaultValue;
                return acc;
            }));
        }
예제 #4
0
        public Dictionary <string, object> GetArgumentValues(ISchema schema, QueryArguments definitionArguments, Arguments astArguments, Variables variables)
        {
            if (definitionArguments == null || !definitionArguments.Any())
            {
                return(null);
            }

            return(definitionArguments.Aggregate(new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase), (acc, arg) =>
            {
                var value = astArguments?.ValueFor(arg.Name);
                var type = arg.ResolvedType;

                var coercedValue = CoerceValue(schema, type, value, variables);
                coercedValue = coercedValue ?? arg.DefaultValue;
                acc[arg.Name] = coercedValue;

                return acc;
            }));
        }