private static object? CoerceValue()

in src/Epam.GraphQL/Schema/SchemaExecuter.cs [99:145]


        private static object? CoerceValue(IGraphType? type, object? input)
        {
            if (type is NonNullGraphType nonNull)
            {
                return CoerceValue(nonNull.ResolvedType, input);
            }

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

            if (type is ListGraphType listType)
            {
                var listItemType = listType.ResolvedType;

                if (input is IEnumerable list)
                {
                    return list
                        .Cast<object>()
                        .Select(item => CoerceValue(listItemType, item))
                        .ToList();
                }
                else
                {
                    return new[] { CoerceValue(listItemType, input) };
                }
            }

            if (type is IObjectGraphType or IInputObjectGraphType)
            {
                var complexType = (IComplexGraphType)type; // both IObjectGraphType and IInputObjectGraphType inherit from IComplexGraphType
                if (input is IDictionary<string, object?> dictionary)
                {
                    return CoerceValue(complexType, dictionary);
                }

                return new Dictionary<string, object?>();
            }

            if (type is ScalarGraphType scalarType)
            {
                return scalarType.ParseValue(input) ?? throw new ArgumentException($"Unable to convert '{input}' to '{type.Name}'");
            }

            return null;
        }