Exemplo n.º 1
0
        public override void OnBeforeRegisterDependencies(
            ITypeDiscoveryContext discoveryContext,
            DefinitionBase?definition,
            IDictionary <string, object?> contextData)
        {
            if (definition is FilterInputTypeDefinition def)
            {
                IFilterConvention convention = GetConvention(
                    discoveryContext.DescriptorContext,
                    def.Scope);

                var descriptor = FilterInputTypeDescriptor.From(
                    discoveryContext.DescriptorContext,
                    def,
                    def.Scope);

                var typeReference = TypeReference.Create(
                    discoveryContext.Type,
                    def.Scope);

                convention.ApplyConfigurations(typeReference, descriptor);

                foreach (InputFieldDefinition field in def.Fields)
                {
                    if (field is FilterFieldDefinition filterFieldDefinition)
                    {
                        if (discoveryContext.TryPredictTypeKind(
                                filterFieldDefinition.Type,
                                out TypeKind kind) &&
                            kind != TypeKind.Scalar && kind != TypeKind.Enum)
                        {
                            field.Type = field.Type.With(scope: discoveryContext.Scope);
                        }

                        if (filterFieldDefinition.Handler is null)
                        {
                            if (convention.TryGetHandler(
                                    discoveryContext,
                                    def,
                                    filterFieldDefinition,
                                    out IFilterFieldHandler? handler))
                            {
                                filterFieldDefinition.Handler = handler;
                            }
                            else
                            {
                                throw ThrowHelper.FilterInterceptor_NoHandlerFoundForField(
                                          def,
                                          filterFieldDefinition);
                            }
                        }
                    }
                }
            }
        }
        public override void OnBeforeCompleteType(
            ITypeCompletionContext completionContext,
            DefinitionBase?definition,
            IDictionary <string, object?> contextData)
        {
            if (definition is FilterInputTypeDefinition def)
            {
                IFilterConvention convention = GetConvention(
                    completionContext.DescriptorContext,
                    def.Scope);

                foreach (InputFieldDefinition field in def.Fields)
                {
                    if (field is FilterFieldDefinition filterFieldDefinition)
                    {
                        if (filterFieldDefinition.Type is null)
                        {
                            throw ThrowHelper.FilterInterceptor_OperationHasNoTypeSpecified(
                                      def,
                                      filterFieldDefinition);
                        }

                        if (completionContext.TryPredictTypeKind(
                                filterFieldDefinition.Type,
                                out TypeKind kind) &&
                            kind != TypeKind.Scalar && kind != TypeKind.Enum)
                        {
                            field.Type = field.Type.With(scope: completionContext.Scope);
                        }

                        if (filterFieldDefinition.Handler is null)
                        {
                            if (convention.TryGetHandler(
                                    completionContext,
                                    def,
                                    filterFieldDefinition,
                                    out IFilterFieldHandler? handler))
                            {
                                filterFieldDefinition.Handler = handler;
                            }
                            else
                            {
                                throw ThrowHelper.FilterInterceptor_NoHandlerFoundForField(
                                          def,
                                          filterFieldDefinition);
                            }
                        }
                    }
                }
            }
        }