Пример #1
0
 public override void OnConfigure(
     IDescriptorContext context,
     IObjectFieldDescriptor descriptor,
     MemberInfo member)
 {
     descriptor.Use(_ => _);
 }
Пример #2
0
        public static IObjectFieldDescriptor UseCustomSelection <TType>(this IObjectFieldDescriptor descriptor)
        {
            descriptor
            .Use <CustomSelectionMiddleware <TType> >();

            return(descriptor);
        }
Пример #3
0
        public static IObjectFieldDescriptor UsePaging(
            IObjectFieldDescriptor descriptor,
            Type?type,
            Type?entityType = null,
            GetPagingProvider?resolvePagingProvider = null,
            PagingOptions options = default)
        {
            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            FieldMiddleware placeholder = next => context => default;

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(definition =>
            {
                definition.Configurations.Add(
                    new TypeConfiguration <ObjectFieldDefinition>
                {
                    Definition = definition,
                    On         = ApplyConfigurationOn.Completion,
                    Configure  = (c, d) => ApplyConfiguration(
                        c, d, entityType, resolvePagingProvider, options, placeholder)
                });
            });

            return(descriptor);
        }
        private void ApplyConfigurationToField <TEntity, TType>(
            IObjectFieldDescriptor field,
            bool withPaging)
            where TEntity : class
            where TType : FilterInputType <TEntity>
        {
            field.Use(
                next => async context =>
            {
                await next(context);

                if (context.Result is IQueryable <TEntity> queryable)
                {
                    try
                    {
                        context.ContextData["sql"] = queryable.ToQueryString();
                    }
                    catch (Exception)
                    {
                        context.ContextData["sql"] =
                            "EF Core 3.1 does not support ToQueryString";
                    }
                }
            });

            if (withPaging)
            {
                field.UsePaging <ObjectType <TEntity> >();
            }

            field.UseFiltering <TType>();
        }
 public static IObjectFieldDescriptor UseBewitProtection <TPayload>(
     this IObjectFieldDescriptor descriptor)
 {
     return(descriptor
            .Use <BewitMiddleware <TPayload> >()
            .Type <NonNullType <StringType> >());
 }
 public static IObjectFieldDescriptor Use <TMiddleware>(
     this IObjectFieldDescriptor descriptor)
     where TMiddleware : class
 {
     return(descriptor.Use(
                FieldClassMiddlewareFactory.Create <TMiddleware>()));
 }
Пример #7
0
        public static IObjectFieldDescriptor UseDataloader(
            this IObjectFieldDescriptor descriptor,
            Type dataLoaderType)
        {
            FieldMiddleware placeholder = next => context => default;

            if (!TryGetDataLoaderTypes(dataLoaderType, out Type? keyType, out Type? valueType))
            {
                throw DataLoader_InvalidType(dataLoaderType);
            }

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(
                (c, definition) =>
            {
                IExtendedType schemaType;
                if (!valueType.IsArray)
                {
                    IExtendedType resolverType =
                        c.TypeInspector.GetType(definition.ResultType);

                    if (resolverType.IsArrayOrList)
                    {
                        schemaType = c.TypeInspector.GetType(
                            typeof(IEnumerable <>).MakeGenericType(valueType));
                    }
                    else
                    {
                        schemaType = c.TypeInspector.GetType(valueType);
                    }
                }
                else
                {
                    schemaType = c.TypeInspector.GetType(valueType);
                }

                definition.Type = TypeReference.Create(schemaType, TypeContext.Output);
                definition.Configurations.Add(
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure(
                        (context, def) =>
                {
                    CompileMiddleware(
                        def,
                        placeholder,
                        keyType,
                        valueType,
                        dataLoaderType);
                })
                    .On(ApplyConfigurationOn.Completion)
                    .Build());
            });

            return(descriptor);
        }
 /// <summary>
 /// Attach authorizator to the field.
 /// </summary>
 /// <returns>Field descriptor with middleware attached.</returns>
 public static IObjectFieldDescriptor UseAuthorization(this IObjectFieldDescriptor descriptor)
 {
     return(descriptor.Use((services, next)
                           => new Authorizator(next, services
                                               .CreateScope()
                                               .ServiceProvider
                                               .GetRequiredService <Tokenizer>())));
 }
Пример #9
0
        private static IObjectFieldDescriptor ApplyMiddleware(
            this IObjectFieldDescriptor descriptor,
            string optionName,
            Type middlewareDefinition)
        {
            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            FieldMiddleware placeholder = next => context => default;

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(
                (context, definition) =>
            {
                definition.ContextData[optionName] = null;

                if (definition.ResultType is null ||
                    !context.TypeInspector.TryCreateTypeInfo(
                        definition.ResultType,
                        out ITypeInfo? typeInfo))
                {
                    Type resultType = definition.ResolverType ?? typeof(object);
                    throw new ArgumentException(
                        $"Cannot handle the specified type `{resultType.FullName}`.",
                        nameof(descriptor));
                }

                Type selectionType    = typeInfo.NamedType;
                definition.ResultType = selectionType;
                definition.Type       = RewriteToNonNullableType(
                    context.TypeInspector,
                    definition.Type);

                ILazyTypeConfiguration lazyConfiguration =
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure(
                        (_, __) =>
                {
                    CompileMiddleware(
                        selectionType,
                        definition,
                        placeholder,
                        middlewareDefinition);
                })
                    .On(ApplyConfigurationOn.Completion)
                    .Build();

                definition.Configurations.Add(lazyConfiguration);
            });

            return(descriptor);
        }
Пример #10
0
        public static IObjectFieldDescriptor UseAutoMapperProjection(
            this IObjectFieldDescriptor descriptor,
            Type objectType)
        {
            FieldMiddleware placeholder = next => context => default;

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(
                (context, definition) =>
            {
                if (definition.ResultType is null ||
                    !context.TypeInspector.TryCreateTypeInfo(
                        definition.ResultType,
                        out ITypeInfo? typeInfo))
                {
                    Type resultType = definition.ResolverType ?? typeof(object);
                    throw new ArgumentException(
                        $"Cannot handle the specified type `{resultType.FullName}`.",
                        nameof(descriptor));
                }

                if (!typeof(IQueryable).IsAssignableFrom(definition.ResultType))
                {
                    throw new ArgumentException(
                        $"Cannot handle the specified type `{definition.ResultType.FullName}`.",
                        nameof(descriptor));
                }

                Type selectionType    = typeInfo.NamedType;
                definition.ResultType = typeof(IQueryable <>).MakeGenericType(objectType);
                definition.Type       = context.TypeInspector.GetTypeRef(definition.ResultType);

                ILazyTypeConfiguration lazyConfiguration =
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure(
                        (_, __) =>
                {
                    CompileMiddleware(
                        selectionType,
                        objectType,
                        definition,
                        placeholder,
                        _middlewareDefinition);
                })
                    .On(ApplyConfigurationOn.Completion)
                    .Build();

                definition.Configurations.Add(lazyConfiguration);
            });

            return(descriptor);
        }
Пример #11
0
        /// <summary>
        /// Projects the selection set of the request onto the field. Registers a middleware that
        /// uses the registered <see cref="ProjectionConvention"/> to apply the projections
        /// </summary>
        /// <param name="descriptor">The descriptor</param>
        /// <param name="scope">
        /// Specify which <see cref="ProjectionConvention"/> is used, based on the value passed in
        /// <see cref="ProjectionsSchemaBuilderExtensions.AddProjections{T}"/>
        /// </param>
        /// <param name="type">
        /// The <see cref="Type"/> of the resolved field
        /// </param>
        /// <returns>The descriptor passed in by <paramref name="descriptor"/></returns>
        /// <exception cref="ArgumentNullException">
        /// In case the descriptor is null
        /// </exception>
        public static IObjectFieldDescriptor UseProjection(
            this IObjectFieldDescriptor descriptor,
            Type?type,
            string?scope = null)
        {
            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            FieldMiddleware placeholder = next => context => default;

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(
                (context, definition) =>
            {
                Type?selectionType = type;

                if (selectionType is null)
                {
                    if (definition.ResultType is null ||
                        !context.TypeInspector.TryCreateTypeInfo(
                            definition.ResultType,
                            out ITypeInfo? typeInfo))
                    {
                        throw new ArgumentException(
                            "Cannot handle the specified type.",
                            nameof(descriptor));
                    }

                    selectionType = typeInfo.NamedType;
                }

                ILazyTypeConfiguration lazyConfiguration =
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure(
                        (context, definition) =>
                        CompileMiddleware(
                            selectionType,
                            definition,
                            placeholder,
                            context,
                            scope))
                    .On(ApplyConfigurationOn.Completion)
                    .Build();
                definition.Configurations.Add(lazyConfiguration);
            });

            return(descriptor);
        }
        /// <summary>
        /// Adds FluentValidation field middleware.
        /// It is going to validate all non-null field arguments and report errors if there is one or more failures.
        /// </summary>
        /// <param name="errorBuilderType">Custom error builder class type used to construct single validation error.</param>
        public static IObjectFieldDescriptor UseFluentValidation(
            this IObjectFieldDescriptor descriptor,
            Type errorBuilderType)
        {
            if (!(typeof(IValidationErrorBuilder).IsAssignableFrom(errorBuilderType) && errorBuilderType.IsClass))
            {
                throw new ArgumentException($"{errorBuilderType.Name} is not a class implementing {nameof(IValidationErrorBuilder)}!");
            }

            return(descriptor.Use((provider, next) =>
                                  new FluentValidationMiddleware(next, errorBuilderType)));
        }
        public static IObjectFieldDescriptor Use <TMiddleware>(
            this IObjectFieldDescriptor descriptor,
            Func <IServiceProvider, FieldDelegate, TMiddleware> factory)
            where TMiddleware : class
        {
            if (factory is null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            return(descriptor.Use(FieldClassMiddlewareFactory.Create(factory)));
        }
Пример #14
0
        /// <summary>
        /// Adds TransactionScope field middleware.
        /// </summary>
        /// <param name="factoryType">Custom TransactionScope factory class type implementing <see cref="ITransactionScopeFactory"/>.</param>
        public static IObjectFieldDescriptor UseTransactionScope(
            this IObjectFieldDescriptor descriptor,
            Type factoryType)
        {
            if (!(typeof(ITransactionScopeFactory).IsAssignableFrom(factoryType) && factoryType.IsClass))
            {
                throw new ArgumentException($"{factoryType.Name} is not a class implementing {nameof(ITransactionScopeFactory)}!");
            }

            return(descriptor.Use((provider, next) =>
                                  new TransactionScopeMiddleware(next, factoryType)));
        }
            public override void OnConfigure(
                IDescriptorContext context,
                IObjectFieldDescriptor descriptor,
                MemberInfo member)
            {
                descriptor.Use(next => context =>
                {
                    context.LocalContextData =
                        context.LocalContextData.SetItem(
                            QueryableProjectionProvider.ContextApplyProjectionKey,
                            CreateApplicatorAsync <Foo>());

                    return(next(context));
                });
            }
Пример #16
0
        public static IObjectFieldDescriptor UseSorting(
            this IObjectFieldDescriptor descriptor,
            Type sortType,
            ITypeSystemMember sortTypeInstance = null)
        {
            FieldMiddleware placeholder =
                next => context => Task.CompletedTask;

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(definition =>
            {
                Type argumentType = GetArgumentType(definition, sortType);

                ITypeReference argumentTypeReference =
                    sortTypeInstance is null
                            ? (ITypeReference) new ClrTypeReference(
                        argumentType, TypeContext.Input)
                            : new SchemaTypeReference(sortTypeInstance);

                var argumentDefinition = new ArgumentDefinition
                {
                    Name = OrderByArgumentName,
                    Type = new ClrTypeReference(
                        argumentType, TypeContext.Input)
                };
                definition.Arguments.Add(argumentDefinition);

                ILazyTypeConfiguration lazyConfiguration =
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure((context, defintion) =>
                               CompileMiddleware(
                                   context,
                                   definition,
                                   argumentTypeReference,
                                   placeholder))
                    .On(ApplyConfigurationOn.Completion)
                    .DependsOn(argumentTypeReference, true)
                    .Build();
                definition.Configurations.Add(lazyConfiguration);
            });

            return(descriptor);
        }
Пример #17
0
 public override void OnConfigure(
     IDescriptorContext context,
     IObjectFieldDescriptor descriptor,
     MemberInfo member)
 {
     descriptor.Use(next => async context =>
     {
         try
         {
             await next(context);
         }
         catch (Exception ex)
         {
             context.ContextData["ex"] = ex.Message;
         }
     });
 }
Пример #18
0
        private static IObjectFieldDescriptor UseSelection(
            IObjectFieldDescriptor descriptor,
            Type?objectType)
        {
            FieldMiddleware placeholder =
                next => context => Task.CompletedTask;

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(definition =>
            {
                Type?selectionType = objectType;

                if (selectionType == null)
                {
                    if (!TypeInspector.Default.TryCreate(
                            definition.ResultType, out TypeInfo typeInfo))
                    {
                        // TODO : resources
                        throw new ArgumentException(
                            "Cannot handle the specified type.",
                            nameof(descriptor));
                    }

                    selectionType = typeInfo.ClrType;
                }

                ILazyTypeConfiguration lazyConfiguration =
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure((context, defintion) =>
                               CompileMiddleware(
                                   selectionType,
                                   definition,
                                   placeholder,
                                   context))
                    .On(ApplyConfigurationOn.Completion)
                    .Build();
                definition.Configurations.Add(lazyConfiguration);
            });

            return(descriptor);
        }
        public static IObjectFieldDescriptor ID(
            this IObjectFieldDescriptor descriptor,
            NameString typeName = default)
        {
            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            FieldMiddleware placeholder = n => c => default;

            descriptor.Use(placeholder);
            descriptor.Extend().OnBeforeCreate(RewriteObjectFieldType);
            descriptor.Extend().OnBeforeCompletion(
                (c, d) => AddSerializerToObjectField(c, d, placeholder, typeName));

            return(descriptor);
        }
Пример #20
0
 public override void OnConfigure(
     IDescriptorContext context,
     IObjectFieldDescriptor descriptor,
     MemberInfo member)
 {
     descriptor.Use(next => context =>
     {
         if (!context.ContextData.ContainsKey("currentUserId"))
         {
             context.ReportError(
                 ErrorBuilder.New()
                 .SetMessage("Must be authorized to perform this action")
                 .SetCode("NOT_AUTHORIZED")
                 .Build());
             context.Result = new ForbidResult();
         }
         return(next(context));
     });
 }
Пример #21
0
 public static IObjectFieldDescriptor UseAutoSubscription(this IObjectFieldDescriptor descriptor, string subscriptionName)
 {
     descriptor
     .Extend()
     .OnBeforeNaming((configure, defn) =>
     {
         if (!configure.ContextData.ContainsKey(AutoSubscriptionTypeInterceptor.AutoSubscriptionContext))
         {
             configure.ContextData[AutoSubscriptionTypeInterceptor.AutoSubscriptionContext] = new List <ObjectFieldDefinition>();
         }
         ((List <ObjectFieldDefinition>)configure.ContextData[AutoSubscriptionTypeInterceptor.AutoSubscriptionContext]).Add(defn);
     });
     descriptor.Use(next => async context =>
     {
         await next(context);
         await context.SendSimpleSubscription(subscriptionName, context.Result);
     });
     return(descriptor);
 }
Пример #22
0
        public static IObjectFieldDescriptor UsePaging(
            IObjectFieldDescriptor descriptor,
            Type?type,
            Type?entityType = null,
            GetPagingProvider?resolvePagingProvider = null,
            PagingOptions options = default)
        {
            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            FieldMiddleware placeholder = next => context => default;

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate((c, d) =>
            {
                MemberInfo?member        = d.ResolverMember ?? d.Member;
                IExtendedType schemaType = GetSchemaType(c.TypeInspector, member, type);

                var configuration = new TypeConfiguration <ObjectFieldDefinition>
                {
                    Definition = d,
                    On         = ApplyConfigurationOn.Completion,
                    Configure  = (c, d) => ApplyConfiguration(
                        c, d, entityType, resolvePagingProvider, options, placeholder)
                };

                configuration.Dependencies.Add(
                    new TypeDependency(
                        TypeReference.Create(schemaType, TypeContext.Output),
                        TypeDependencyKind.Named));

                d.Configurations.Add(configuration);
            });

            return(descriptor);
        }
Пример #23
0
        private static IObjectFieldDescriptor UseFiltering(
            IObjectFieldDescriptor descriptor,
            Type?filterType,
            ITypeSystemMember?filterTypeInstance,
            string?scope)
        {
            FieldMiddleware placeholder         = next => context => default;
            string          argumentPlaceholder =
                "_" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(
                (c, definition) =>
            {
                IFilterConvention convention = c.GetFilterConvention(scope);
                ITypeReference argumentTypeReference;

                if (filterTypeInstance is not null)
                {
                    argumentTypeReference = TypeReference.Create(filterTypeInstance, scope);
                }
                else if (filterType is null)
                {
                    if (definition.ResultType is null ||
                        definition.ResultType == typeof(object) ||
                        !c.TypeInspector.TryCreateTypeInfo(
                            definition.ResultType,
                            out ITypeInfo? typeInfo))
                    {
                        throw new ArgumentException(
                            FilterObjectFieldDescriptorExtensions_UseFiltering_CannotHandleType,
                            nameof(descriptor));
                    }

                    argumentTypeReference = convention.GetFieldType(typeInfo.NamedType);
                }
                else
                {
                    argumentTypeReference = c.TypeInspector.GetTypeRef(
                        filterType,
                        TypeContext.Input,
                        scope);
                }

                var argumentDefinition = new ArgumentDefinition
                {
                    Name = argumentPlaceholder, Type = argumentTypeReference
                };

                definition.Arguments.Add(argumentDefinition);

                definition.Configurations.Add(
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure(
                        (context, definition) =>
                        CompileMiddleware(
                            context,
                            definition,
                            argumentTypeReference,
                            placeholder,
                            scope))
                    .On(ApplyConfigurationOn.Completion)
                    .DependsOn(argumentTypeReference, true)
                    .Build());

                argumentDefinition.Configurations.Add(
                    LazyTypeConfigurationBuilder
                    .New <ArgumentDefinition>()
                    .Definition(argumentDefinition)
                    .Configure(
                        (context, argumentDefinition) =>
                        argumentDefinition.Name =
                            context.GetFilterConvention(scope).GetArgumentName())
                    .On(ApplyConfigurationOn.Naming)
                    .Build());
            });

            return(descriptor);
        }
Пример #24
0
 public static IObjectFieldDescriptor AuthorizeBewit <T>(
     this IObjectFieldDescriptor descriptor)
 {
     return(descriptor
            .Use <BewitAuthorizationMiddleware <T> >());
 }
Пример #25
0
        private static IObjectFieldDescriptor UseFiltering(
            IObjectFieldDescriptor descriptor,
            Type?filterType,
            ITypeSystemMember?filterTypeInstance = null)
        {
            FieldMiddleware placeholder =
                _ => _ => Task.CompletedTask;

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(definition =>
            {
                Type?argumentType = filterType;

                if (argumentType == null)
                {
                    if (!TypeInspector.Default.TryCreate(
                            definition.ResultType,
                            out TypeInfo typeInfo))
                    {
                        throw new ArgumentException(
                            FilterResources.FilterObjectFieldDescriptor_InvalidType,
                            nameof(descriptor));
                    }

                    argumentType = typeof(FilterInputType <>).MakeGenericType(typeInfo.ClrType);
                }

                ITypeReference argumentTypeReference =
                    filterTypeInstance is null
                            ? (ITypeReference) new ClrTypeReference(argumentType, TypeContext.Input)
                            : new SchemaTypeReference(filterTypeInstance);

                if (argumentType == typeof(object))
                {
                    throw new SchemaException(
                        SchemaErrorBuilder.New()
                        .SetMessage(
                            FilterResources.FilterObjectFieldDescriptor_InvalidType_Msg)
                        .SetCode(ErrorCodes.Filtering.FilterObjectType)
                        .Build());
                }

                var argumentDefinition = new ArgumentDefinition
                {
                    Type = new ClrTypeReference(argumentType, TypeContext.Input)
                };

                argumentDefinition.ConfigureArgumentName();
                definition.Arguments.Add(argumentDefinition);

                ILazyTypeConfiguration lazyConfiguration =
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure((context, definition) =>
                               CompileMiddleware(
                                   context,
                                   definition,
                                   argumentTypeReference,
                                   placeholder))
                    .On(ApplyConfigurationOn.Completion)
                    .DependsOn(argumentTypeReference, true)
                    .Build();
                definition.Configurations.Add(lazyConfiguration);
            });

            return(descriptor);
        }
Пример #26
0
        private static IObjectFieldDescriptor UseFiltering(
            IObjectFieldDescriptor descriptor,
            Type filterType,
            ITypeSystemMember filterTypeInstance = null)
        {
            FieldMiddleware placeholder =
                next => context => Task.CompletedTask;

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(definition =>
            {
                Type argumentType = filterType;

                if (filterType == null)
                {
                    if (!TypeInspector.Default.TryCreate(
                            definition.ResultType, out TypeInfo typeInfo))
                    {
                        // TODO : resources
                        throw new ArgumentException(
                            "Cannot handle the specified type.",
                            nameof(descriptor));
                    }

                    argumentType =
                        typeof(FilterInputType <>).MakeGenericType(
                            typeInfo.ClrType);
                }

                var argumentTypeReference = filterTypeInstance is null
                        ? (ITypeReference) new ClrTypeReference(
                    argumentType, TypeContext.Input)
                        : new SchemaTypeReference(filterTypeInstance);

                if (argumentType == typeof(object))
                {
                    // TODO : resources
                    throw new SchemaException(
                        SchemaErrorBuilder.New()
                        .SetMessage(
                            "The filter type cannot be " +
                            "infered from `System.Object`.")
                        .SetCode(ErrorCodes.Filtering.FilterObjectType)
                        .Build());
                }

                var argumentDefinition  = new ArgumentDefinition();
                argumentDefinition.Name = _whereArgumentName;
                argumentDefinition.Type = new ClrTypeReference(
                    argumentType, TypeContext.Input);
                definition.Arguments.Add(argumentDefinition);

                ILazyTypeConfiguration lazyConfiguration =
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure((context, defintion) =>
                               CompileMiddleware(
                                   context,
                                   definition,
                                   argumentTypeReference,
                                   placeholder))
                    .On(ApplyConfigurationOn.Completion)
                    .DependsOn(argumentTypeReference, true)
                    .Build();
                definition.Configurations.Add(lazyConfiguration);
            });

            return(descriptor);
        }
Пример #27
0
        private static IObjectFieldDescriptor UseFiltering(
            IObjectFieldDescriptor descriptor,
            Type?filterType,
            ITypeSystemMember?filterTypeInstance = null)
        {
            FieldMiddleware placeholder         = next => context => default;
            string          argumentPlaceholder =
                "_" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate((c, definition) =>
            {
                Type?argumentType = filterType;

                if (argumentType is null)
                {
                    if (definition.ResultType is null ||
                        definition.ResultType == typeof(object) ||
                        !c.TypeInspector.TryCreateTypeInfo(
                            definition.ResultType, out ITypeInfo? typeInfo))
                    {
                        throw new ArgumentException(
                            FilterResources.FilterObjectFieldDescriptor_InvalidType,
                            nameof(descriptor));
                    }

                    argumentType = typeof(FilterInputType <>)
                                   .MakeGenericType(typeInfo.NamedType);
                }

                ITypeReference argumentTypeReference = filterTypeInstance is null
                        ? (ITypeReference)c.TypeInspector.GetTypeRef(
                    argumentType,
                    TypeContext.Input)
                        : TypeReference.Create(filterTypeInstance);

                if (argumentType == typeof(object))
                {
                    throw new SchemaException(
                        SchemaErrorBuilder.New()
                        .SetMessage(
                            FilterResources.FilterObjectFieldDescriptor_InvalidType_Msg)
                        .SetCode(ErrorCodes.Filtering.FilterObjectType)
                        .Build());
                }

                var argumentDefinition = new ArgumentDefinition
                {
                    Name = argumentPlaceholder,
                    Type = c.TypeInspector.GetTypeRef(argumentType, TypeContext.Input)
                };

                argumentDefinition.ConfigureArgumentName();
                definition.Arguments.Add(argumentDefinition);

                ILazyTypeConfiguration lazyConfiguration =
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure((context, definition) =>
                               CompileMiddleware(
                                   context,
                                   definition,
                                   argumentTypeReference,
                                   placeholder))
                    .On(ApplyConfigurationOn.Completion)
                    .DependsOn(argumentTypeReference, true)
                    .Build();
                definition.Configurations.Add(lazyConfiguration);
            });

            return(descriptor);
        }
 public static IObjectFieldDescriptor UseBewitUrlProtection(
     this IObjectFieldDescriptor descriptor)
 {
     return(descriptor.Use <BewitUrlMiddleware>()
            .Type <NonNullType <StringType> >());
 }
        public static IObjectFieldDescriptor UseSorting(
            this IObjectFieldDescriptor descriptor,
            Type?sortType,
            ITypeSystemMember?sortTypeInstance = null)
        {
            FieldMiddleware placeholder         = next => context => default;
            string          argumentPlaceholder =
                "_" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate((c, definition) =>
            {
                Type argumentType = GetArgumentType(definition, sortType, c.TypeInspector);

                ITypeReference argumentTypeReference = sortTypeInstance is null
                        ? (ITypeReference)c.TypeInspector.GetTypeRef(
                    argumentType,
                    TypeContext.Input)
                        : TypeReference.Create(sortTypeInstance);

                var argumentDefinition = new ArgumentDefinition
                {
                    Name = argumentPlaceholder,
                    Type = c.TypeInspector.GetTypeRef(argumentType, TypeContext.Input)
                };

                ILazyTypeConfiguration lazyArgumentConfiguration =
                    LazyTypeConfigurationBuilder
                    .New <ArgumentDefinition>()
                    .Definition(argumentDefinition)
                    .Configure((context, definition) =>
                {
                    ISortingNamingConvention convention =
                        context.DescriptorContext.GetSortingNamingConvention();
                    definition.Name = convention.ArgumentName;
                })
                    .On(ApplyConfigurationOn.Completion)
                    .Build();

                argumentDefinition.Configurations.Add(lazyArgumentConfiguration);
                definition.Arguments.Add(argumentDefinition);

                ILazyTypeConfiguration lazyConfiguration =
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure((context, definition) =>
                               CompileMiddleware(
                                   context,
                                   definition,
                                   argumentTypeReference,
                                   placeholder))
                    .On(ApplyConfigurationOn.Completion)
                    .DependsOn(argumentTypeReference, true)
                    .Build();
                definition.Configurations.Add(lazyConfiguration);
            });

            return(descriptor);
        }
Пример #30
0
        private static IObjectFieldDescriptor UseSorting(
            IObjectFieldDescriptor descriptor,
            Type?sortType,
            ITypeSystemMember?sortTypeInstance,
            string?scope)
        {
            FieldMiddleware placeholder         = next => context => default;
            string          argumentPlaceholder =
                "_" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(
                (c, definition) =>
            {
                Type?argumentType = sortType;

                if (argumentType is null)
                {
                    if (definition.ResultType is null ||
                        definition.ResultType == typeof(object) ||
                        !c.TypeInspector.TryCreateTypeInfo(
                            definition.ResultType,
                            out ITypeInfo? typeInfo))
                    {
                        throw new ArgumentException(
                            SortObjectFieldDescriptorExtensions_UseSorting_CannotHandleType,
                            nameof(descriptor));
                    }

                    argumentType = typeof(SortInputType <>)
                                   .MakeGenericType(typeInfo.NamedType);
                }

                ITypeReference argumentTypeReference = sortTypeInstance is null
                            ? (ITypeReference)c.TypeInspector.GetTypeRef(
                    argumentType,
                    TypeContext.Input,
                    scope)
                            : TypeReference.Create(sortTypeInstance, scope);

                if (argumentType == typeof(object))
                {
                    throw SortObjectFieldDescriptorExtensions_CannotInfer();
                }

                argumentType = typeof(ListType <>).MakeGenericType(argumentType);

                var argumentDefinition = new ArgumentDefinition
                {
                    Name = argumentPlaceholder,
                    Type = c.TypeInspector.GetTypeRef(
                        argumentType,
                        TypeContext.Input,
                        scope)
                };
                definition.Arguments.Add(argumentDefinition);

                definition.Configurations.Add(
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure(
                        (context, def) =>
                        CompileMiddleware(
                            context,
                            def,
                            argumentTypeReference,
                            placeholder,
                            scope))
                    .On(ApplyConfigurationOn.Completion)
                    .DependsOn(argumentTypeReference, true)
                    .Build());

                definition.Configurations.Add(
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure(
                        (context, _) =>
                        argumentDefinition.Name =
                            context.GetSortConvention(scope).GetArgumentName())
                    .On(ApplyConfigurationOn.Naming)
                    .Build());
            });

            return(descriptor);
        }