FieldDescriptorUtilities

We found 10 examples in language CSharp for this search. You will see 58 fragments of code.

        protected override void OnCreateDefinition(
            InputObjectTypeDefinition definition)
        {
            if (Definition.ClrType is { })
            {
                Context.Inspector.ApplyAttributes(
                    Context,
                    this,
                    Definition.ClrType);
            }

            var fields = new Dictionary<NameString, InputFieldDefinition>();
            var handledProperties = new HashSet<PropertyInfo>();

            FieldDescriptorUtilities.AddExplicitFields(
                Fields.Select(t => t.CreateDefinition()),
                f => f.Property,
                fields,
                handledProperties);

            OnCompleteFields(fields, handledProperties);

            Definition.Fields.AddRange(fields.Values);

            base.OnCreateDefinition(definition);
        }

        protected virtual void OnCompleteFields(
            IDictionary<NameString, InputFieldDefinition> fields,
            ISet<PropertyInfo> handledProperties)
        {
        }

        public IInputObjectTypeDescriptor SyntaxNode(
            InputObjectTypeDefinitionNode inputObjectTypeDefinitionNode)
        {
            Definition.SyntaxNode = inputObjectTypeDefinitionNode;
            return this;
        }

        public IInputObjectTypeDescriptor Name(NameString value)
        {
            Definition.Name = value.EnsureNotEmpty(nameof(value));
            return this;
        }

        public IInputObjectTypeDescriptor Description(string value)
        {
            Definition.Description = value;
            return this;
        }

        public IInputFieldDescriptor Field(NameString name)
        {
            InputFieldDescriptor fieldDescriptor =
                Fields.FirstOrDefault(t => t.Definition.Name.Equals(name));
            if (fieldDescriptor is { })
            {
                return fieldDescriptor;
            }

            fieldDescriptor = new InputFieldDescriptor(
                Context,
                name.EnsureNotEmpty(nameof(name)));
            Fields.Add(fieldDescriptor);
            return fieldDescriptor;
        }


        protected override void OnCreateDefinition(
            InterfaceTypeDefinition definition)
        {
            if (Definition.ClrType is { })
            {
                Context.Inspector.ApplyAttributes(
                    Context,
                    this,
                    Definition.ClrType);
            }

            var fields = new Dictionary<NameString, InterfaceFieldDefinition>();
            var handledMembers = new HashSet<MemberInfo>();

            FieldDescriptorUtilities.AddExplicitFields(
                Fields.Select(t => t.CreateDefinition()),
                f => f.Member,
                fields,
                handledMembers);

            OnCompleteFields(fields, handledMembers);

            Definition.Fields.AddRange(fields.Values);

            base.OnCreateDefinition(definition);
        }

        protected virtual void OnCompleteFields(
            IDictionary<NameString, InterfaceFieldDefinition> fields,
            ISet<MemberInfo> handledMembers)
        {
        }

        public IInterfaceTypeDescriptor SyntaxNode(
            InterfaceTypeDefinitionNode interfaceTypeDefinition)
        {
            Definition.SyntaxNode = interfaceTypeDefinition;
            return this;
        }

        public IInterfaceTypeDescriptor Name(NameString value)
        {
            Definition.Name = value.EnsureNotEmpty(nameof(value));
            return this;
        }

        public IInterfaceTypeDescriptor Description(string value)
        {
            Definition.Description = value;
            return this;
        }

        public IInterfaceFieldDescriptor Field(NameString name)
        {
            InterfaceFieldDescriptor fieldDescriptor =
                Fields.FirstOrDefault(t => t.Definition.Name.Equals(name));
            if (fieldDescriptor is { })
            {
                return fieldDescriptor;
            }

            fieldDescriptor = InterfaceFieldDescriptor.New(
                Context,
                name.EnsureNotEmpty(nameof(name)));
            Fields.Add(fieldDescriptor);
            return fieldDescriptor;
        }


        protected override void OnCreateDefinition(
            InterfaceFieldDefinition definition)
        {
            if (Definition.Member is { })
            {
                Context.Inspector.ApplyAttributes(
                    Context,
                    this,
                    Definition.Member);
            }

            base.OnCreateDefinition(definition);

            CompleteArguments(definition);
        }

        private void CompleteArguments(InterfaceFieldDefinition definition)
        {
            if (!_argumentsInitialized)
            {
                FieldDescriptorUtilities.DiscoverArguments(
                    Context,
                    definition.Arguments,
                    definition.Member);
                _argumentsInitialized = true;
            }
        }

        public new IInterfaceFieldDescriptor SyntaxNode(
            FieldDefinitionNode fieldDefinitionNode)
        {
            base.SyntaxNode(fieldDefinitionNode);
            return this;
        }

        public new IInterfaceFieldDescriptor Name(
            NameString name)
        {
            base.Name(name);
            return this;
        }

        public new IInterfaceFieldDescriptor Description(
            string description)
        {
            base.Description(description);
            return this;
        }

        [Obsolete("Use `Deprecated`.")]
        public IInterfaceFieldDescriptor DeprecationReason(string reason) =>
            Deprecated(reason);


        protected override void OnCreateDefinition(
            DirectiveTypeDefinition definition)
        {
            if (Definition.ClrType is { })
            {
                Context.Inspector.ApplyAttributes(
                    Context,
                    this,
                    Definition.ClrType);
            }

            var arguments =
                new Dictionary<NameString, DirectiveArgumentDefinition>();
            var handledMembers = new HashSet<PropertyInfo>();

            FieldDescriptorUtilities.AddExplicitFields(
                Arguments.Select(t => t.CreateDefinition()),
                f => f.Property,
                arguments,
                handledMembers);

            OnCompleteArguments(arguments, handledMembers);

            definition.Arguments.AddRange(arguments.Values);

            base.OnCreateDefinition(definition);
        }

        protected virtual void OnCompleteArguments(
            IDictionary<NameString, DirectiveArgumentDefinition> arguments,
            ISet<PropertyInfo> handledProperties)
        {
        }

        public IDirectiveTypeDescriptor SyntaxNode(
            DirectiveDefinitionNode directiveDefinitionNode)
        {
            Definition.SyntaxNode = directiveDefinitionNode;
            return this;
        }

        public IDirectiveTypeDescriptor Name(NameString value)
        {
            Definition.Name = value.EnsureNotEmpty(nameof(value));
            return this;
        }

        public IDirectiveTypeDescriptor Description(string value)
        {
            Definition.Description = value;
            return this;
        }

        public IDirectiveArgumentDescriptor Argument(NameString name)
        {
            DirectiveArgumentDescriptor descriptor =
                Arguments.FirstOrDefault(t => t.Definition.Name.Equals(name));
            if (descriptor is { })
            {
                return descriptor;
            }

            descriptor = new DirectiveArgumentDescriptor(
                Context,
                name.EnsureNotEmpty(nameof(name)));
            Arguments.Add(descriptor);
            return descriptor;
        }



        public ISortInputTypeDescriptor<T> Name(NameString value)
        {
            Definition.Name = value.EnsureNotEmpty(nameof(value));
            return this;
        }

        public ISortInputTypeDescriptor<T> Description(
            string value)
        {
            Definition.Description = value;
            return this;
        }

        public ISortInputTypeDescriptor<T> Directive<TDirective>(
            TDirective directiveInstance)
            where TDirective : class
        {
            Definition.AddDirective(directiveInstance);
            return this;
        }

        public ISortInputTypeDescriptor<T> Directive<TDirective>()
            where TDirective : class, new()
        {
            Definition.AddDirective(new TDirective());
            return this;
        }

        public ISortInputTypeDescriptor<T> Directive(
            NameString name,
            params ArgumentNode[] arguments)
        {
            Definition.AddDirective(name, arguments);
            return this;
        }


        public ISortInputTypeDescriptor<T> BindFields(
            BindingBehavior behavior)
        {
            Definition.Fields.BindingBehavior = behavior;
            return this;
        }


        protected override void OnCreateDefinition(
            ObjectFieldDefinition definition)
        {

            if (Definition.Member is { })
            {
                Context.Inspector.ApplyAttributes(
                    Context,
                    this,
                    Definition.Member);
            }

            base.OnCreateDefinition(definition);

            CompleteArguments(definition);
        }

        private void CompleteArguments(ObjectFieldDefinition definition)
        {
            if (!_argumentsInitialized)
            {
                FieldDescriptorUtilities.DiscoverArguments(
                    Context,
                    definition.Arguments,
                    definition.Member);
                _argumentsInitialized = true;
            }
        }

        public new IObjectFieldDescriptor SyntaxNode(
            FieldDefinitionNode? fieldDefinition)
        {
            base.SyntaxNode(fieldDefinition);
            return this;
        }

        public new IObjectFieldDescriptor Name(NameString value)
        {
            base.Name(value);
            return this;
        }

        public new IObjectFieldDescriptor Description(
            string? value)
        {
            base.Description(value);
            return this;
        }

        [Obsolete("Use `Deprecated`.")]
        public IObjectFieldDescriptor DeprecationReason(string? reason) =>
           Deprecated(reason);


        protected override void OnCreateDefinition(
            ObjectTypeDefinition definition)
        {
            if (Definition.ClrType is { })
            {
                Context.Inspector.ApplyAttributes(
                    Context,
                    this,
                    Definition.ClrType);
            }

            var fields = new Dictionary<NameString, ObjectFieldDefinition>();
            var handledMembers = new HashSet<MemberInfo>();

            FieldDescriptorUtilities.AddExplicitFields(
                Fields.Select(t => t.CreateDefinition()),
                f => f.Member,
                fields,
                handledMembers);

            OnCompleteFields(fields, handledMembers);

            Definition.Fields.AddRange(fields.Values);

            base.OnCreateDefinition(definition);
        }

        protected virtual void OnCompleteFields(
            IDictionary<NameString, ObjectFieldDefinition> fields,
            ISet<MemberInfo> handledMembers)
        {
            DiscoverResolvers(fields);
        }

        protected void DiscoverResolvers(
            IDictionary<NameString, ObjectFieldDefinition> fields)
        {
            var processed = new HashSet<string>();

            if (Definition.ClrType != typeof(object))
            {
                foreach (Type resolverType in Context.Inspector
                    .GetResolverTypes(Definition.ClrType))
                {
                    ResolverTypes.Add(resolverType);
                }
            }

            foreach (Type resolverType in ResolverTypes)
            {
                AddResolvers(
                    fields,
                    processed,
                    Definition.ClrType ?? typeof(object),
                    resolverType);
            }
        }

        private void AddResolvers(
            IDictionary<NameString, ObjectFieldDefinition> fields,
            ISet<string> processed,
            Type sourceType,
            Type resolverType)
        {
            foreach (MemberInfo member in Context.Inspector.GetMembers(resolverType))
            {
                if (IsResolverRelevant(sourceType, member))
                {
                    ObjectFieldDefinition fieldDefinition =
                        ObjectFieldDescriptor
                            .New(Context, member, resolverType)
                            .CreateDefinition();

                    if (processed.Add(fieldDefinition.Name))
                    {
                        fields[fieldDefinition.Name] = fieldDefinition;
                    }
                }
            }
        }

        private static bool IsResolverRelevant(
            Type sourceType,
            MemberInfo resolver)
        {
            if (resolver is PropertyInfo)
            {
                return true;
            }

            if (resolver is MethodInfo m)
            {
                ParameterInfo parent = m.GetParameters()
                    .FirstOrDefault(t => t.IsDefined(typeof(ParentAttribute)));
                return parent == null
                    || parent.ParameterType.IsAssignableFrom(sourceType);
            }

            return false;
        }

        public IObjectTypeDescriptor SyntaxNode(
            ObjectTypeDefinitionNode objectTypeDefinition)
        {
            Definition.SyntaxNode = objectTypeDefinition;
            return this;
        }


    protected override void OnCompleteFields(
        IDictionary<NameString, InputFieldDefinition> fields,
        ISet<PropertyInfo> handledProperties)
    {
        if (Definition.Fields.IsImplicitBinding())
        {
            FieldDescriptorUtilities.AddImplicitFields(
                this,
                p => InputFieldDescriptor
                    .New(Context, p)
                    .CreateDefinition(),
                fields,
                handledProperties);
        }

        base.OnCompleteFields(fields, handledProperties);
    }

    public new IInputObjectTypeDescriptor<T> SyntaxNode(
        InputObjectTypeDefinitionNode inputObjectTypeDefinitionNode)
    {
        base.SyntaxNode(inputObjectTypeDefinitionNode);
        return this;
    }

    public new IInputObjectTypeDescriptor<T> Name(NameString value)
    {
        base.Name(value);
        return this;
    }

    public new IInputObjectTypeDescriptor<T> Description(string value)
    {
        base.Description(value);
        return this;
    }

    public IInputObjectTypeDescriptor<T> BindFields(
        BindingBehavior behavior)
    {
        Definition.Fields.BindingBehavior = behavior;
        return this;
    }

    public IInputObjectTypeDescriptor<T> BindFieldsExplicitly() =>
        BindFields(BindingBehavior.Explicit);


    public static void AddExplicitFields<TMember, TField>(
        IEnumerable<TField> fieldDefinitions,
        Func<TField, TMember?> resolveMember,
        IDictionary<NameString, TField> fields,
        ISet<TMember> handledMembers)
        where TMember : MemberInfo
        where TField : FieldDefinitionBase
    {
        foreach (TField fieldDefinition in fieldDefinitions)
        {
            if (!fieldDefinition.Ignore)
            {
                fields[fieldDefinition.Name] = fieldDefinition;
            }

            TMember? member = resolveMember(fieldDefinition);
            if (member != null)
            {
                handledMembers.Add(member);
            }
        }
    }

    public static void AddImplicitFields<TDescriptor, TMember, TField>(
        TDescriptor descriptor,
        Func<TMember, TField> createdFieldDefinition,
        IDictionary<NameString, TField> fields,
        ISet<TMember> handledMembers)
        where TDescriptor : IHasRuntimeType, IHasDescriptorContext
        where TMember : MemberInfo
        where TField : FieldDefinitionBase
    {
        AddImplicitFields(
            descriptor,
            descriptor.RuntimeType,
            createdFieldDefinition,
            fields,
            handledMembers);
    }

    public static void AddImplicitFields<TDescriptor, TMember, TField>(
        TDescriptor descriptor,
        Type fieldBindingType,
        Func<TMember, TField> createdFieldDefinition,
        IDictionary<NameString, TField> fields,
        ISet<TMember> handledMembers,
        Func<IReadOnlyList<TMember>, TMember, bool>? include = null,
        bool includeIgnoredMembers = false)
        where TDescriptor : IHasDescriptorContext
        where TMember : MemberInfo
        where TField : FieldDefinitionBase
    {
        if (fieldBindingType != typeof(object))
        {
            var members = descriptor.Context.TypeInspector
                .GetMembers(fieldBindingType, includeIgnoredMembers)
                .OfType<TMember>()
                .ToList();

            foreach (TMember member in members)
            {
                if (include?.Invoke(members, member) ?? true)
                {
                    TField fieldDefinition = createdFieldDefinition(member);

                    if (!handledMembers.Contains(member) &&
                        !fields.ContainsKey(fieldDefinition.Name) &&
                        (includeIgnoredMembers || !fieldDefinition.Ignore))
                    {
                        handledMembers.Add(member);
                        fields[fieldDefinition.Name] = fieldDefinition;
                    }
                }
            }
        }
    }

    public static void DiscoverArguments(
        IDescriptorContext context,
        ICollection<ArgumentDefinition> arguments,
        MemberInfo? member)
    {
        if (arguments is null)
        {
            throw new ArgumentNullException(nameof(arguments));
        }

        if (member is MethodInfo method)
        {
            var processedNames = Interlocked.Exchange(ref _names, null) ?? new();

            try
            {
                foreach (ArgumentDefinition argument in arguments)
                {
                    processedNames.Add(argument.Name);
                }

                foreach (ParameterInfo parameter in
                    context.ResolverCompiler.GetArgumentParameters(method.GetParameters()))
                {
                    ArgumentDefinition argumentDefinition =
                        ArgumentDescriptor
                            .New(context, parameter)
                            .CreateDefinition();

                    if (processedNames.Add(argumentDefinition.Name))
                    {
                        arguments.Add(argumentDefinition);
                    }
                }
            }
            finally
            {
                processedNames.Clear();

                Interlocked.CompareExchange(ref _names, processedNames, null);
            }
        }
    }


    protected override void OnCompleteArguments(
        IDictionary<NameString, DirectiveArgumentDefinition> arguments,
        ISet<PropertyInfo> handledProperties)
    {
        if (Definition.Arguments.IsImplicitBinding())
        {
            FieldDescriptorUtilities.AddImplicitFields(
                this,
                p => DirectiveArgumentDescriptor
                    .New(Context, p)
                    .CreateDefinition(),
                arguments,
                handledProperties);
        }

        base.OnCompleteArguments(arguments, handledProperties);
    }

    public new IDirectiveTypeDescriptor<T> SyntaxNode(
        DirectiveDefinitionNode directiveDefinitionNode)
    {
        base.SyntaxNode(directiveDefinitionNode);
        return this;
    }

    public new IDirectiveTypeDescriptor<T> Name(NameString value)
    {
        base.Name(value);
        return this;
    }

    public new IDirectiveTypeDescriptor<T> Description(string value)
    {
        base.Description(value);
        return this;
    }

    public IDirectiveTypeDescriptor<T> BindArguments(
        BindingBehavior behavior)
    {
        Definition.Arguments.BindingBehavior = behavior;
        return this;
    }

    public IDirectiveTypeDescriptor<T> BindArgumentsExplicitly() =>
        BindArguments(BindingBehavior.Explicit);

HotChocolate.Types.Descriptors.FieldDescriptorUtilities : Object

Methods :

public static Void AddExplicitFields(IEnumerable<TField> fieldDefinitions = , Func<TField, TMember> resolveMember = , IDictionary<NameString, TField> fields = , ISet<TMember> handledMembers = )
public static Void AddImplicitFields(TDescriptor descriptor = , Func<TMember, TField> createdFieldDefinition = , IDictionary<NameString, TField> fields = , ISet<TMember> handledMembers = )
public static Void AddImplicitFields(TDescriptor descriptor = , Type fieldBindingType = , Func<TMember, TField> createdFieldDefinition = , IDictionary<NameString, TField> fields = , ISet<TMember> handledMembers = , Func<IReadOnlyList<TMember>, TMember, Boolean> include = null)
public static Void DiscoverArguments(IDescriptorContext context = , ICollection<ArgumentDefinition> arguments = , MemberInfo member = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()