예제 #1
0
        public static IGraphType GetQueryItemType(IGraphQueryHandler handler, Type modelType, bool isInput)
        {
            if (!isInput)
            {
                lock (OutputDeclaredTypes)
                {
                    if (OutputDeclaredTypes.ContainsKey(modelType))
                    {
                        return(OutputDeclaredTypes[modelType]);
                    }
                    var result = GetGraphTypeEx(modelType, handler, false);
                    OutputDeclaredTypes.Add(modelType, result);
                    return(result);
                }
            }

            lock (InputDeclaredTypes)
            {
                if (InputDeclaredTypes.ContainsKey(modelType))
                {
                    return(InputDeclaredTypes[modelType]);
                }
                var result = GetGraphTypeEx(modelType, handler, true);
                InputDeclaredTypes.Add(modelType, result);
                return(result);
            }
        }
예제 #2
0
        public static QueryArguments GetArguments(Type parametersType, IGraphQueryHandler graphQuery, Boolean isInput)
        {
            var qas = new List <QueryArgument>();

            foreach (var prop in parametersType.GetProperties().Where(i => i.CanWrite))
            {
                var type = GetGraphType(prop.PropertyType, isInput);

                var allowNulls = prop.GetCustomAttribute <AllowNullAttribute>() != null;

                var description = prop.GetCustomAttribute <DescriptionAttribute>()?.Description;

                if (type == null)
                {
                    var gType = GetQueryItemType(graphQuery, prop.PropertyType, true);
                    if (!allowNulls && isInput)
                    {
                        gType = new NonNullGraphType(gType);
                    }

                    qas.Add(new QueryArgument(gType)
                    {
                        Name = prop.Name, Description = description
                    });
                }
                else
                {
                    if (!allowNulls && isInput)
                    {
                        if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(NonNullGraphType <>))
                        {
                            type = typeof(NonNullGraphType <>).MakeGenericType(type);
                        }
                    }
                    else
                    {
                        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(NonNullGraphType <>))
                        {
                            type = type.GetGenericArguments()[0];
                        }
                    }

                    qas.Add(new QueryArgument(type)
                    {
                        Name = prop.Name, Description = description
                    });
                }
            }
            return(new QueryArguments(qas));
        }
예제 #3
0
        public static IEnumerable <FieldType> GetFields(Type modelType, IGraphQueryHandler graphQuery, Boolean isInput)
        {
            var qas = new List <FieldType>();

            foreach (var prop in modelType.GetProperties())
            {
                var description = prop.GetCustomAttribute <DescriptionAttribute>()?.Description;

                var allowNulls = prop.GetCustomAttribute <AllowNullAttribute>() != null;

                var type = GetGraphType(prop.PropertyType, isInput);

                if (type == null)
                {
                    var gType = GetQueryItemType(graphQuery, prop.PropertyType, isInput);

                    if (!allowNulls && isInput)
                    {
                        gType = new NonNullGraphType(gType);
                    }

                    qas.Add(new FieldType {
                        ResolvedType = gType, Name = prop.Name, Description = description
                    });
                }
                else
                {
                    if (!allowNulls && isInput)
                    {
                        if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(NonNullGraphType <>))
                        {
                            type = typeof(NonNullGraphType <>).MakeGenericType(type);
                        }
                    }
                    else
                    {
                        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(NonNullGraphType <>))
                        {
                            type = type.GetGenericArguments()[0];
                        }
                    }

                    qas.Add(new FieldType {
                        Type = type, Name = prop.Name, Description = description
                    });
                }
            }
            return(qas);
        }
예제 #4
0
        public InputObjectGraphTypeFromModel(Type modelType, IGraphQueryHandler graphQueryHandler)
        {
            var modelType1 = modelType;

            //IsTypeOf = type => type.GetType().IsAssignableFrom(modelType1);

            Name        = "Input" + (!modelType1.Name.EndsWith("Model") ? modelType1.Name : modelType1.Name.Substring(0, modelType1.Name.Length - "Model".Length));
            Description = modelType1.GetCustomAttribute <DescriptionAttribute>() != null?modelType1.GetCustomAttribute <DescriptionAttribute>()?.Description : null;

            var fields = QueryParametersHelper.GetFields(modelType1, graphQueryHandler, true);

            foreach (var field in fields)
            {
                AddField(field);
            }
        }
예제 #5
0
        private static IGraphType GetGraphTypeEx(Type propType, IGraphQueryHandler graphQuery, Boolean isInput)
        {
            if (propType.IsGenericType && propType.GetGenericArguments().Length == 1 && typeof(IEnumerable).IsAssignableFrom(propType))
            {
                var innerType = propType.GetGenericArguments().First();
                if (innerType != null)
                {
                    return(new ListGraphType(graphQuery.GetQueryItemType(innerType, isInput)));
                }

                return(null);
            }

            return(isInput ? new InputObjectGraphTypeFromModel(propType, graphQuery) :
                   (IGraphType) new ObjectGraphTypeFromModel(propType, graphQuery, false));
        }