예제 #1
0
        public override object WrapValue(GraphTypeInfo typeInfo, object value)
        {
            var input = value as Dictionary <string, object>;

            if (typeInfo.IsInputType && input != null)
            {
                var obj = Activator.CreateInstance(typeInfo.GetTypeRepresentation().AsType());
                foreach (var field in typeInfo.Fields.Where(field => !field.IsIgnored))
                {
                    object fieldValue;
                    if (!input.TryGetValue(field.Name, out fieldValue))
                    {
                        if (!field.Type.IsNullable && field.DefaultValue == null)
                        {
                            throw new Exception($"Value for non-nullable field '{field.Name}' not provided.");
                        }
                        fieldValue = field.DefaultValue;
                    }
                    var propertyInfo = field.AttributeProvider as PropertyInfo;
                    if (propertyInfo == null)
                    {
                        throw new Exception($"Invalid field '{field.Name}' on input object; must be a property.");
                    }
                    propertyInfo.SetValue(obj, _parent.Wrap(field.Type, fieldValue));
                }
                return(obj);
            }
            return(value);
        }
예제 #2
0
        public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool isSpecified)
        {
            if (value == null || !typeInfo.IsPrimitive || typeInfo.IsEnumerationType)
            {
                return(value);
            }

            try
            {
                var targetType = typeInfo.GetTypeRepresentation().AsType();

                var converter = TypeDescriptor.GetConverter(targetType);
                if (converter.CanConvertFrom(value.GetType()))
                {
                    return(converter.ConvertFrom(value));
                }

                if (value is IConvertible)
                {
                    return(Convert.ChangeType(value, targetType));
                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException($"Unable to cast {GetEntityDescription(entityInfo)} '{entityInfo.Name}' to '{typeInfo.Name}'.", ex);
            }
            return(value);
        }
예제 #3
0
 public override object WrapValue(GraphTypeInfo typeInfo, object value)
 {
     if (typeInfo.IsPrimitive &&
         !typeInfo.IsEnumerationType &&
         value is IConvertible)
     {
         return(Convert.ChangeType(value, typeInfo.GetTypeRepresentation().AsType()));
     }
     return(value);
 }
예제 #4
0
        private TReturnType ConstructType <TReturnType>(Type template, GraphTypeInfo typeInfo)
            where TReturnType : class, IGraphType
        {
            var typeParameter = typeInfo.GetTypeRepresentation();
            var type          = CreateTypeInstance <TReturnType>(template.MakeGenericType(typeParameter.AsType()));

            if (type == null)
            {
                return(default(TReturnType));
            }
            type.Name              = typeInfo.Name;
            type.Description       = typeInfo.Description;
            type.DeprecationReason = typeInfo.DeprecationReason;
            return(CacheType(typeInfo, type) as TReturnType);
        }
예제 #5
0
        private IGraphType ConstructOutputType(GraphTypeInfo typeInfo)
        {
            var graphType = ConstructType <ObjectGraphType>(typeof(Types.OutputObjectGraphType <>), typeInfo);

            foreach (var iface in typeInfo.Interfaces.Select(GetType))
            {
                graphType.Interface(iface);
            }
            graphType.IsTypeOf = obj =>
            {
                var objType = UnwrapObject(obj)?.GetType().GetTypeRepresentation();
                return(objType == typeInfo.GetTypeRepresentation());
            };
            DeriveFields(typeInfo, graphType);
            return(WrapNonNullableType(typeInfo, graphType));
        }
예제 #6
0
 public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool isSpecified)
 {
     if (typeInfo.IsPrimitive &&
         !typeInfo.IsEnumerationType &&
         value is IConvertible)
     {
         try
         {
             return(Convert.ChangeType(value, typeInfo.GetTypeRepresentation().AsType()));
         }
         catch (Exception ex)
         {
             throw new ArgumentException($"Unable to cast {GetEntityDescription(entityInfo)} '{entityInfo.Name}' to '{typeInfo.Name}'.", ex);
         }
     }
     return(value);
 }
예제 #7
0
        public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool _)
        {
            var isNull            = value == null;
            var shouldReturnValue = isNull;

            if (shouldReturnValue)
            {
                return(null);
            }

            var valueType = value.GetType();

            var isPrimitive = typeInfo.IsPrimitive;

            isPrimitive |= valueType.GetTypeInfo().IsPrimitive;
            isPrimitive |= value is string;

            shouldReturnValue |= isPrimitive;

            if (shouldReturnValue)
            {
                return(value);
            }

            var isNonNull = typeof(NonNull <string>).Name == typeInfo.TypeRepresentation.Name;

            shouldReturnValue |= isNonNull
                ? typeInfo.TypeRepresentation.GenericTypeArguments.First() == valueType
                : typeInfo.TypeRepresentation.AsType() == valueType;

            if (shouldReturnValue)
            {
                return(value);
            }

            if (typeInfo.IsScalarType)
            {
                return(Activator.CreateInstance(typeInfo.GetTypeRepresentation().AsType(), value));
            }

            if (!typeInfo.IsInputType || !(value is Dictionary <string, object> input))
            {
                return(value);
            }

            var obj = Activator.CreateInstance(typeInfo.GetTypeRepresentation().AsType());

            foreach (var field in typeInfo.Fields.Where(field => !field.IsIgnored))
            {
                var isSpecified = true;
                if (!input.TryGetValue(field.Name, out var fieldValue))
                {
                    if (!field.Type.IsNullable && field.DefaultValue == null)
                    {
                        throw new Exception($"Value for non-nullable field '{field.Name}' not provided.");
                    }
                    isSpecified = false;
                    fieldValue  = field.DefaultValue;
                }

                if (!(field.AttributeProvider is PropertyInfo propertyInfo))
                {
                    throw new Exception($"Invalid field '{field.Name}' on input object; must be a property.");
                }
                propertyInfo.SetValue(obj, _parent.Wrap(field, field.Type, fieldValue, isSpecified));
            }
            return(obj);
        }