コード例 #1
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);
                }
            }
        }