예제 #1
0
        public AutoInputGraphType(IGraphQLConfiguration configuration)
        {
            _configuration     = configuration;
            _typeConfiguration = _configuration.GetModelConfiguration <TSourceType>();

            Name = GetTypeName(typeof(TSourceType)) + "Input";

            Metadata["Type"] = typeof(TSourceType);

            var properties = GetRegisteredProperties().ToList();

            foreach (var propertyInfo in properties)
            {
                var fieldConfiguration = _typeConfiguration.GetFieldConfiguration(propertyInfo.Name);

                var field = Field(
                    type: propertyInfo.PropertyType.ToGraphType(),
                    name: fieldConfiguration?.FieldName ?? GetFieldName(propertyInfo),
                    description: propertyInfo.Description(),
                    deprecationReason: propertyInfo.ObsoleteMessage()
                    );

                field.DefaultValue             = (propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault() as DefaultValueAttribute)?.Value;
                field.Metadata["PropertyInfo"] = propertyInfo;
            }
        }
예제 #2
0
        protected virtual bool IsEnabledForRegister(PropertyInfo propertyInfo, Type propertyType, bool firstCall)
        {
            if (propertyInfo.GetCustomAttribute <IgnoreAttribute>() != null)
            {
                return(false);
            }

            if (propertyType == typeof(string))
            {
                return(true);
            }

            if (propertyType.IsValueType)
            {
                return(true);                          // TODO: requires discussion: Nullable<T>, enums, any struct
            }
            if (GraphTypeTypeRegistry.Contains(propertyType))
            {
                return(true);
            }

            if (propertyType == typeof(ResolveFieldContext))
            {
                return(false);
            }

            if (firstCall)
            {
                var realType = GetRealType(propertyType);
                if (realType != propertyType)
                {
                    return(IsEnabledForRegister(propertyInfo, realType, false));
                }
            }

            var fieldConfiguration = _typeConfiguration.GetFieldConfiguration(propertyInfo.Name);

            if (fieldConfiguration?.Ignored == true || fieldConfiguration?.Output == false)
            {
                return(false);
            }

            if (!propertyType.IsValueType && propertyType.IsClass)
            {
                var type = typeof(AutoObjectGraphType <>).MakeGenericType(propertyType);
                GraphTypeTypeRegistry.Register(propertyType, type);

                return(true);
            }

            return(false);
        }
예제 #3
0
        protected virtual bool IsEnabledForRegister(PropertyInfo propertyInfo, Type propertyType, bool firstCall)
        {
            if (propertyInfo.GetCustomAttribute <IgnoreAttribute>() != null)
            {
                return(false);
            }

            if (propertyType == typeof(string))
            {
                return(true);
            }

            if (propertyType.IsValueType)
            {
                return(true);                          // TODO: requires discussion: Nullable<T>, enums, any struct
            }
            if (_schema.TypeMappings.Any(x => x.clrType == propertyType))
            {
                return(true);
            }

            if (typeof(IResolveFieldContext).IsAssignableFrom(propertyType))
            {
                return(false);
            }

            if (firstCall)
            {
                var realType = GetRealType(propertyType);
                if (realType != propertyType)
                {
                    return(IsEnabledForRegister(propertyInfo, realType, false));
                }
            }

            var fieldConfiguration = _typeConfiguration.GetFieldConfiguration(propertyInfo.Name);

            if (fieldConfiguration?.Ignored == true || fieldConfiguration?.Output == false)
            {
                return(false);
            }

            if (!propertyType.IsValueType && propertyType.IsClass)
            {
                var type = GetType().GetGenericTypeDefinition().MakeGenericType(propertyType);
                _schema.RegisterTypeMapping(propertyType, type);

                return(true);
            }

            return(false);
        }
예제 #4
0
        /// <summary>
        /// Creates a GraphQL type by specifying fields to exclude from registration.
        /// </summary>
        public AutoObjectGraphType(IGraphQLConfiguration configuration)
        {
            _configuration     = configuration;
            _typeConfiguration = _configuration.GetModelConfiguration <TSourceType>();

            Name             = GetTypeName(typeof(TSourceType));
            Metadata["Type"] = typeof(TSourceType);

            var properties = GetRegisteredProperties().ToList();

            foreach (var propertyInfo in properties)
            {
                var fieldConfiguration = _typeConfiguration.GetFieldConfiguration(propertyInfo.Name);

                if (propertyInfo.PropertyType != typeof(string) &&
                    typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var realType      = propertyInfo.PropertyType.GetGenericArguments()[0];
                    var realGraphType = typeof(AutoObjectGraphType <>).MakeGenericType(realType);
                    var listGqlType   = typeof(ListGraphType <>).MakeGenericType(realGraphType);

                    var field = Field(
                        type: listGqlType,
                        name: fieldConfiguration?.FieldName ?? GetFieldName(propertyInfo),
                        description: propertyInfo.Description(),
                        deprecationReason: propertyInfo.ObsoleteMessage()
                        );

                    field.DefaultValue             = (propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault() as DefaultValueAttribute)?.Value;
                    field.Metadata["PropertyInfo"] = propertyInfo;
                }
                else
                {
                    var field = Field(
                        type: propertyInfo.PropertyType.GetGraphTypeFromType(IsNullableProperty(propertyInfo)),
                        name: fieldConfiguration?.FieldName ?? GetFieldName(propertyInfo),
                        description: propertyInfo.Description(),
                        deprecationReason: propertyInfo.ObsoleteMessage()
                        );

                    field.DefaultValue             = (propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault() as DefaultValueAttribute)?.Value;
                    field.Metadata["PropertyInfo"] = propertyInfo;

                    // Synthetic properties

                    /*if (propertyInfo.PropertyType.IsAssignableToGenericType(typeof(IEntity<>)) &&
                     *  !propertyInfo.PropertyType.DeclaringType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Any(x => x.Name == propertyInfo.Name + "Id"))
                     * {
                     *  var genericType = propertyInfo.PropertyType.GetInterfaces()
                     *      .Single(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntity<>))
                     *      .GetGenericArguments()[0];
                     *
                     *  var syntheticPropertyField = new FieldType
                     *  {
                     *      Type = genericType.GetGraphTypeFromType(IsNullableProperty(propertyInfo)),
                     *      Resolver = new SyntheticPropertyResolver(propertyInfo),
                     *      Name = (fieldConfiguration?.FieldName ?? GetFieldName(propertyInfo)) + "Id",
                     *      Description = propertyInfo.Description(),
                     *      DeprecationReason = propertyInfo.ObsoleteMessage()
                     *  };
                     *  syntheticPropertyField.Metadata["PropertyInfo"] = propertyInfo;
                     *
                     *  AddField(syntheticPropertyField);
                     * }*/
                }
            }

            var interfaces = typeof(TSourceType).GetInterfaces().Where(x => x.IsPublic).ToList();

            foreach (var @interface in interfaces)
            {
                var interfaceType = GraphTypeTypeRegistry.Get(@interface);
                if (interfaceType == null)
                {
                    interfaceType = typeof(AutoInterfaceGraphType <>).MakeGenericType(@interface);
                    GraphTypeTypeRegistry.Register(@interface, interfaceType);
                }

                Interface(interfaceType);
            }
        }
예제 #5
0
        /// <summary>
        /// Creates a GraphQL type by specifying fields to exclude from registration.
        /// </summary>
        public AutoGraphType(ISchema schema, IGraphQLConfiguration configuration, IFieldResolver fieldResolver)
        {
            _schema            = schema;
            _configuration     = configuration;
            _typeConfiguration = _configuration.GetModelConfiguration <TSourceType>();

            var type = typeof(TSourceType);

            Name             = GetTypeName(type);
            Metadata["Type"] = type;
            var typePermissions = type.GetCustomAttribute <AuthorizeAttribute>()?.Permissions;

            Metadata[GraphQLExtensions.PermissionsKey] = typePermissions;

            var properties = GetRegisteredProperties().ToList();

            foreach (var propertyInfo in properties)
            {
                var fieldConfiguration  = _typeConfiguration.GetFieldConfiguration(propertyInfo.Name);
                var propertyPermissions = propertyInfo.GetCustomAttribute <AuthorizeAttribute>()?.Permissions;

                if (propertyInfo.PropertyType != typeof(string) &&
                    typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var realType        = propertyInfo.PropertyType.GetGenericArguments()[0];
                    var nonNullableType =
                        realType.IsGenericType && realType.GetGenericTypeDefinition() == typeof(Nullable <>)
                            ? Nullable.GetUnderlyingType(realType)
                            : realType;
                    var realGraphType =
                        _schema.BuiltInTypeMappings.Where(x => x.clrType == nonNullableType).Select(x => x.graphType).SingleOrDefault() ??
                        _schema.TypeMappings.Where(x => x.clrType == nonNullableType).Select(x => x.graphType).SingleOrDefault() ??
                        (nonNullableType.IsEnum ? typeof(EnumerationGraphType <>).MakeGenericType(nonNullableType) : GetType().GetGenericTypeDefinition().MakeGenericType(nonNullableType));
                    var listGqlType = typeof(ListGraphType <>).MakeGenericType(realGraphType);

                    var field = Field(
                        type: listGqlType,
                        name: fieldConfiguration?.FieldName ?? GetFieldName(propertyInfo),
                        description: propertyInfo.Description(),
                        deprecationReason: propertyInfo.ObsoleteMessage()
                        );
                    field.Resolver = fieldResolver;

                    field.DefaultValue             = (propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault() as DefaultValueAttribute)?.Value;
                    field.Metadata["PropertyInfo"] = propertyInfo;

                    if (propertyPermissions != null)
                    {
                        foreach (var permission in propertyPermissions)
                        {
                            field.RequirePermission(permission);
                        }
                    }
                }
                else
                {
                    var propertyType    = propertyInfo.PropertyType;
                    var nonNullableType =
                        propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)
                            ? Nullable.GetUnderlyingType(propertyType)
                            : propertyType;
                    var isNullableProperty = IsNullableProperty(propertyInfo);
                    var gqlType            =
                        _schema.BuiltInTypeMappings.Where(x => x.clrType == nonNullableType).Select(x => x.graphType).SingleOrDefault() ??
                        _schema.TypeMappings.Where(x => x.clrType == nonNullableType).Select(x => x.graphType).SingleOrDefault() ??
                        (propertyType.IsEnum ? typeof(EnumerationGraphType <>).MakeGenericType(propertyType) : GetType().GetGenericTypeDefinition().MakeGenericType(propertyType));
                    if (!isNullableProperty)
                    {
                        gqlType = typeof(NonNullGraphType <>).MakeGenericType(gqlType);
                    }

                    var field = Field(
                        type: gqlType,
                        name: fieldConfiguration?.FieldName ?? GetFieldName(propertyInfo),
                        description: propertyInfo.Description(),
                        deprecationReason: propertyInfo.ObsoleteMessage()
                        );
                    field.Resolver = fieldResolver;

                    field.DefaultValue             = (propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault() as DefaultValueAttribute)?.Value;
                    field.Metadata["PropertyInfo"] = propertyInfo;

                    if (propertyPermissions != null)
                    {
                        foreach (var permission in propertyPermissions)
                        {
                            field.RequirePermission(permission);
                        }
                    }

                    // Synthetic properties

                    /*if (propertyInfo.PropertyType.IsAssignableToGenericType(typeof(IEntity<>)) &&
                     *  !propertyInfo.PropertyType.DeclaringType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Any(x => x.Name == propertyInfo.Name + "Id"))
                     * {
                     *  var genericType = propertyInfo.PropertyType.GetInterfaces()
                     *      .Single(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntity<>))
                     *      .GetGenericArguments()[0];
                     *
                     *  var syntheticPropertyField = new FieldType
                     *  {
                     *      Type = genericType.GetGraphTypeFromType(IsNullableProperty(propertyInfo)),
                     *      Resolver = new SyntheticPropertyResolver(propertyInfo),
                     *      Name = (fieldConfiguration?.FieldName ?? GetFieldName(propertyInfo)) + "Id",
                     *      Description = propertyInfo.Description(),
                     *      DeprecationReason = propertyInfo.ObsoleteMessage()
                     *  };
                     *  syntheticPropertyField.Metadata["PropertyInfo"] = propertyInfo;
                     *
                     *  AddField(syntheticPropertyField);
                     * }*/
                }
            }

            if (_configuration.GenerateInterfaces)
            {
                var interfaces = type.GetInterfaces().Where(x => x.IsPublic).ToList();

                foreach (var @interface in interfaces)
                {
                    if (_configuration.ImplementInterface != null && !configuration.ImplementInterface(@interface, type))
                    {
                        continue;
                    }

                    if (_typeConfiguration.ImplementInterface != null && !_typeConfiguration.ImplementInterface(@interface))
                    {
                        continue;
                    }

                    var interfaceType = _schema.TypeMappings.Where(x => x.clrType == @interface).Select(x => x.graphType).SingleOrDefault();
                    if (interfaceType == null)
                    {
                        interfaceType = typeof(AutoInterfaceGraphType <>).MakeGenericType(@interface);
                        _schema.RegisterTypeMapping(@interface, interfaceType);
                    }

                    AddInterface(interfaceType);
                }
            }
        }