示例#1
0
        public IGraphType GetInputType(Type type)
        {
            // Check if the type is an scalar type
            IGraphType result = null;

            // Check if it is an scalar type
            if (ScalarTypeMap.TryGetValue(type, out result))
            {
                return(result);
            }

            // Check if it is a list
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                var enumItemType = type.GetInterfaces().First(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable <>)).GetGenericArguments()[0];
                return(new ListGraphType(GetInputType(enumItemType)));
            }

            // Check if it is a enum
            if (type.IsEnum)
            {
                if (!EnumTypeMap.TryGetValue(type, out result))
                {
                    result = new DynamicEnumerationGraphType(type);
                    EnumTypeMap.Add(type, result);
                }
                return(result);
            }

            // if it is value type and not included in the scalars like Guid
            // use it as string
            if (type.IsValueType)
            {
                return(new StringGraphType());
            }

            // Check if it is object
            if (type.IsClass)
            {
                if (!InputObjectTypeMap.TryGetValue(type, out result))
                {
                    result = new DynamicInputGraphObject(this, type);
                    InputObjectTypeMap.Add(type, result);
                }
            }

            return(result);
        }
示例#2
0
        public IGraphType GetGraphType(Type type)
        {
            IGraphType result = null;

            // Check if it is an scalar type
            if (ScalarTypeMap.TryGetValue(type, out result))
            {
                return(result);
            }

            // Check if it is a list
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                var enumItemType = type.GetInterfacesIncludingType()
                                   .First(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable <>)).GetGenericArguments()[0];
                return(new ListGraphType(GetGraphType(enumItemType)));
            }

            // Check if it is a enum
            if (type.IsEnum)
            {
                if (!EnumTypeMap.TryGetValue(type, out result))
                {
                    result = new DynamicEnumerationGraphType(type);
                    EnumTypeMap.Add(type, result);
                }

                return(result);
            }

            // if it is value type and not included in the scalars like Guid
            // use it as string
            if (type.IsValueType)
            {
                return(new StringGraphType());
            }

            // Check if it is an interface
            if (type.IsInterface)
            {
                if (!InterfaceTypeMap.TryGetValue(type, out result))
                {
                    result = new DynamicInterfaceType(this, type);
                    InterfaceTypeMap.Add(type, result);
                    RegisterInterfaceTypes(type, result.Name);
                }

                return(result);
            }

            // Check if type is a union
            if (typeof(IUnionGraphType).IsAssignableFrom(type))
            {
                if (!UnionTypeMap.TryGetValue(type, out result))
                {
                    var possibleResult = new DynamicUnionGraphType(this, type);

                    // Check again in case it has been added recursevely
                    if (!UnionTypeMap.TryGetValue(type, out result))
                    {
                        result = possibleResult;
                        UnionTypeMap.Add(type, result);
                    }
                }

                return(result);
            }

            // Check if it is object
            if (type.IsClass)
            {
                if (!ObjectTypeMap.TryGetValue(type, out result))
                {
                    var possibleResult = new DynamicGraphType(this, type);

                    // Check again in case it has been added recursevely
                    if (!ObjectTypeMap.TryGetValue(type, out result))
                    {
                        result = possibleResult;
                        ObjectTypeMap.Add(type, result);
                    }
                }
            }

            return(result);
        }