Exemplo n.º 1
0
        public static IObjectFieldDescriptor UsePagination <TSchemaType>(this IObjectFieldDescriptor descriptor) where TSchemaType : class, IOutputType
        {
            FieldMiddleware placeholder          = next => default(FieldDelegate);
            Type            middlewareDefinition = typeof(QueryableConnectionMiddleware <>);

            descriptor
            .AddPaginationArguments()
            .Type <PaginationType <TSchemaType> >()
            .Use(placeholder)
            .Extend()
            .OnBeforeCompletion((context, defintion) =>
            {
                var reference = typeof(TSchemaType)?.BaseType?.GenericTypeArguments?[0];
                if (reference != null)
                {
                    Type middlewareType        = middlewareDefinition.MakeGenericType(reference);
                    FieldMiddleware middleware = FieldClassMiddlewareFactory.Create(
                        middlewareType);
                    int index = defintion.MiddlewareComponents.IndexOf(placeholder);
                    defintion.MiddlewareComponents[index] = middleware;
                }
            })
            .DependsOn <TSchemaType>();

            return(descriptor);
        }
        private static void CompileMiddleware(
            ITypeCompletionContext context,
            ObjectFieldDefinition definition,
            ITypeReference argumentTypeReference,
            FieldMiddleware placeholder,
            string?scope)
        {
            IType resolvedType = context.GetType <IType>(argumentTypeReference);

            if (!(resolvedType.ElementType().NamedType() is ISortInputType type))
            {
                throw Sorting_TypeOfInvalidFormat(resolvedType);
            }

            ISortConvention convention = context.DescriptorContext.GetSortConvention(scope);

            var fieldDescriptor = ObjectFieldDescriptor.From(context.DescriptorContext, definition);

            convention.ConfigureField(fieldDescriptor);

            MethodInfo factory    = _factoryTemplate.MakeGenericMethod(type.EntityType.Source);
            var        middleware = (FieldMiddleware)factory.Invoke(null, new object[] { convention }) !;
            var        index      = definition.MiddlewareComponents.IndexOf(placeholder);

            definition.MiddlewareComponents[index] = middleware;
        }
        public static IObjectFieldDescriptor UsePaging <TSchemaType>(
            this IObjectFieldDescriptor descriptor)
            where TSchemaType : IOutputType, new()
        {
            FieldMiddleware placeholder =
                next => context => Task.CompletedTask;
            Type middlewareDefinition = typeof(QueryableConnectionMiddleware <>);

            descriptor
            .AddPagingArguments()
            .Type(ConnectionType <TSchemaType> .CreateWithTotalCount())
            .Use(placeholder)
            .Extend()
            .OnBeforeCompletion((context, defintion) =>
            {
                var reference = new ClrTypeReference(
                    typeof(TSchemaType),
                    TypeContext.Output);
                IOutputType type = context.GetType <IOutputType>(reference);
                if (type.NamedType() is IHasClrType hasClrType)
                {
                    Type middlewareType = middlewareDefinition
                                          .MakeGenericType(hasClrType.ClrType);
                    FieldMiddleware middleware =
                        FieldClassMiddlewareFactory.Create(middlewareType);
                    int index =
                        defintion.MiddlewareComponents.IndexOf(placeholder);
                    defintion.MiddlewareComponents[index] = middleware;
                }
            })
            .DependsOn <TSchemaType>();

            return(descriptor);
        }
Exemplo n.º 4
0
        public static IObjectFieldDescriptor UsePaging <TSchemaType>(
            this IObjectFieldDescriptor descriptor)
            where TSchemaType : class, IOutputType
        {
            FieldMiddleware placeholder          = next => context => default(ValueTask);
            Type            middlewareDefinition = typeof(QueryableConnectionMiddleware <>);

            descriptor
            .AddPagingArguments()
            .Type <ConnectionWithCountType <TSchemaType> >()
            .Use(placeholder)
            .Extend()
            .OnBeforeCompletion((context, defintion) =>
            {
                ITypeReference reference =
                    context.DescriptorContext.TypeInspector.GetTypeRef(typeof(TSchemaType));
                IOutputType type = context.GetType <IOutputType>(reference);
                if (type.NamedType() is IHasRuntimeType hasClrType)
                {
                    Type middlewareType = middlewareDefinition.MakeGenericType(
                        hasClrType.RuntimeType);
                    FieldMiddleware middleware = FieldClassMiddlewareFactory.Create(
                        middlewareType);
                    int index = defintion.MiddlewareComponents.IndexOf(placeholder);
                    defintion.MiddlewareComponents[index] = middleware;
                }
            })
            .DependsOn <TSchemaType>();

            return(descriptor);
        }
Exemplo n.º 5
0
        private static TDescriptor UseFilter <TDescriptor>(
            TDescriptor descriptor,
            Type filterType)
            where TDescriptor : IObjectFieldDescriptor
        {
            FieldMiddleware placeholder =
                next => context => Task.CompletedTask;
            Type middlewareDefinition = typeof(QueryableFilterMiddleware <>);

            descriptor
            .AddFilterArguments(filterType)
            .Use(placeholder)
            .Extend()
            .OnBeforeCompletion((context, defintion) =>
            {
                var reference = new ClrTypeReference(
                    filterType,
                    TypeContext.Input);
                IFilterInputType type =
                    context.GetType <IFilterInputType>(reference);
                Type middlewareType = middlewareDefinition
                                      .MakeGenericType(type.EntityType);
                FieldMiddleware middleware =
                    FieldClassMiddlewareFactory.Create(middlewareType);
                int index =
                    defintion.MiddlewareComponents.IndexOf(placeholder);
                defintion.MiddlewareComponents[index] = middleware;
            })
            .DependsOn(filterType, mustBeCompleted: true);

            return(descriptor);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        public QueryExecutor(
            ISchema schema,
            IServiceProvider applicationServices,
            QueryDelegate queryDelegate,
            FieldMiddleware fieldMiddleware)
        {
            Schema = schema
                     ?? throw new ArgumentNullException(nameof(schema));
            _applicationServices = applicationServices
                                   ?? throw new ArgumentNullException(nameof(applicationServices));
            _queryDelegate = queryDelegate
                             ?? throw new ArgumentNullException(nameof(queryDelegate));

            if (Schema.Services != null)
            {
                IEnumerable <IDiagnosticObserver> observers = Schema.Services
                                                              .GetService <IEnumerable <IDiagnosticObserver> >();

                if (observers != null)
                {
                    QueryExecutionDiagnostics diagnosticEvents = _applicationServices
                                                                 .GetService <QueryExecutionDiagnostics>();
                    diagnosticEvents.Subscribe(observers);
                }
            }

            _fieldMiddlewareCompiler = new FieldMiddlewareCompiler(
                schema, fieldMiddleware);
        }
Exemplo n.º 8
0
        public static IQueryExecutionBuilder MapField(
            this IQueryExecutionBuilder builder,
            FieldReference fieldReference,
            FieldMiddleware middleware)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (fieldReference == null)
            {
                throw new ArgumentNullException(nameof(fieldReference));
            }

            if (middleware == null)
            {
                throw new ArgumentNullException(nameof(middleware));
            }

            return(builder.UseField(
                       FieldClassMiddlewareFactory.Create <MapMiddleware>(
                           (s, n) => new MapMiddleware(
                               n, fieldReference, middleware(n)))));
        }
        private static void CompileMiddleware(
            ObjectFieldDefinition definition,
            FieldMiddleware placeholder,
            Type keyType,
            Type valueType,
            Type dataLoaderType)
        {
            Type middlewareType;

            if (valueType.IsArray)
            {
                middlewareType =
                    typeof(GroupedDataLoaderMiddleware <, ,>)
                    .MakeGenericType(dataLoaderType, keyType, valueType.GetElementType() !);
            }
            else
            {
                middlewareType =
                    typeof(DataLoaderMiddleware <, ,>)
                    .MakeGenericType(dataLoaderType, keyType, valueType);
            }

            FieldMiddleware middleware = FieldClassMiddlewareFactory.Create(middlewareType);
            var             index      = definition.MiddlewareComponents.IndexOf(placeholder);

            definition.MiddlewareComponents[index] = middleware;
        }
Exemplo n.º 10
0
        public static IQueryExecutionBuilder MapField <TMiddleware>(
            this IQueryExecutionBuilder builder,
            FieldReference fieldReference,
            Func <IServiceProvider, FieldDelegate, TMiddleware> factory)
            where TMiddleware : class
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (fieldReference == null)
            {
                throw new ArgumentNullException(nameof(fieldReference));
            }

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            return(builder.UseField(
                       FieldClassMiddlewareFactory.Create <MapMiddleware>(
                           (s, n) =>
            {
                FieldMiddleware classMiddleware =
                    FieldClassMiddlewareFactory.Create(factory);

                return new MapMiddleware(
                    n, fieldReference, classMiddleware(n));
            })));
        }
Exemplo n.º 11
0
        private static void AddSerializerToObjectField(
            ITypeCompletionContext completionContext,
            ObjectFieldDefinition definition,
            FieldMiddleware placeholder,
            NameString typeName)
        {
            ITypeInspector typeInspector = completionContext.TypeInspector;
            IExtendedType? resultType;

            if (definition.ResultType is not null)
            {
                resultType = typeInspector.GetType(definition.ResultType);
            }
            else if (definition.Type is ExtendedTypeReference typeReference)
            {
                resultType = typeReference.Type;
            }
            else
            {
                throw new SchemaException(SchemaErrorBuilder.New()
                                          .SetMessage("Unable to resolve type from field `{0}`.", definition.Name)
                                          .SetTypeSystemObject(completionContext.Type)
                                          .Build());
            }

            NameString schemaName = default;

            completionContext.DescriptorContext.SchemaCompleted += (sender, args) =>
                                                                   schemaName = args.Schema.Name;

            IIdSerializer serializer =
                completionContext.Services.GetService <IIdSerializer>() ??
                new IdSerializer();
            var index = definition.MiddlewareComponents.IndexOf(placeholder);

            definition.MiddlewareComponents[index] = next => async context =>
            {
                await next(context).ConfigureAwait(false);

                if (context.Result is not null)
                {
                    if (resultType.IsArrayOrList)
                    {
                        var list = new List <object?>();
                        foreach (object?element in (IEnumerable)context.Result)
                        {
                            list.Add(element is null
                                ? element
                                : serializer.Serialize(schemaName, typeName, element));
                        }
                        context.Result = list;
                    }
                    else
                    {
                        context.Result = serializer.Serialize(schemaName, typeName, context.Result);
                    }
                }
            };
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
        public StarWarsSchema(IServiceProvider provider)
            : base(provider)
        {
            Query    = (StarWarsQuery)provider.GetService(typeof(StarWarsQuery)) ?? throw new InvalidOperationException();
            Mutation = (StarWarsMutation)provider.GetService(typeof(StarWarsMutation)) ?? throw new InvalidOperationException();

            FieldMiddleware.Use(new InstrumentFieldsMiddleware());
        }
Exemplo n.º 14
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);
        }
Exemplo n.º 15
0
        public void RegisterMiddleware(FieldMiddleware middleware)
        {
            if (middleware == null)
            {
                throw new ArgumentNullException(nameof(middleware));
            }

            _middlewareComponents.Add(middleware);
        }
Exemplo n.º 16
0
 public FieldMiddlewareDefinition(
     FieldMiddleware middleware,
     bool isRepeatable = true,
     string?key        = null)
 {
     Middleware   = middleware;
     IsRepeatable = isRepeatable;
     Key          = key;
 }
Exemplo n.º 17
0
        protected void Use(FieldMiddleware middleware)
        {
            if (middleware == null)
            {
                throw new ArgumentNullException(nameof(middleware));
            }

            FieldDescription.MiddlewareComponents.Add(middleware);
        }
Exemplo n.º 18
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);
        }
 public static IMiddlewareConfiguration Map(
     this IMiddlewareConfiguration configuration,
     FieldReference fieldReference,
     FieldMiddleware middleware)
 {
     return(configuration.Use(
                ClassMiddlewareFactory.Create <MapMiddleware>(
                    (s, n) => new MapMiddleware(
                        n, fieldReference, middleware(n)))));
 }
Exemplo n.º 20
0
 public QueryExecutor(
     ISchema schema,
     IServiceProvider applicationServices,
     QueryDelegate queryDelegate,
     FieldMiddleware fieldMiddleware)
     : this(schema, queryDelegate, fieldMiddleware)
 {
     _applicationServices = applicationServices
                            ?? throw new ArgumentNullException(nameof(applicationServices));
 }
Exemplo n.º 21
0
        public IObjectFieldDescriptor Use(FieldMiddleware middleware)
        {
            if (middleware == null)
            {
                throw new ArgumentNullException(nameof(middleware));
            }

            Definition.MiddlewareComponents.Add(middleware);
            return(this);
        }
Exemplo n.º 22
0
 public static IRequestExecutorBuilder MapField(
     this IRequestExecutorBuilder builder,
     FieldReference fieldReference,
     FieldMiddleware middleware)
 {
     return(builder.UseField(
                FieldClassMiddlewareFactory.Create(
                    (s, n) => new MapMiddleware(
                        n, fieldReference, middleware(n)))));
 }
Exemplo n.º 23
0
        public ISchemaBuilder Use(FieldMiddleware middleware)
        {
            if (middleware == null)
            {
                throw new ArgumentNullException(nameof(middleware));
            }

            _globalComponents.Add(middleware);
            return(this);
        }
Exemplo n.º 24
0
 public static ISchemaBuilder Map(
     this ISchemaBuilder configuration,
     FieldReference fieldReference,
     FieldMiddleware middleware)
 {
     return(configuration.Use(
                FieldClassMiddlewareFactory.Create(
                    (s, n) => new MapMiddleware(
                        n, fieldReference, middleware(n)))));
 }
Exemplo n.º 25
0
        public IQueryExecutionBuilder UseField(FieldMiddleware middleware)
        {
            if (middleware == null)
            {
                throw new ArgumentNullException(nameof(middleware));
            }

            _fieldMiddlewareComponents.Add(middleware);
            return(this);
        }
Exemplo n.º 26
0
        public IMiddlewareConfiguration Use(FieldMiddleware middleware)
        {
            if (middleware == null)
            {
                throw new ArgumentNullException(nameof(middleware));
            }

            _resolverRegistry.RegisterMiddleware(middleware);
            return(this);
        }
Exemplo n.º 27
0
        public IMiddlewareConfiguration Use(FieldMiddleware middleware)
        {
            if (middleware == null)
            {
                throw new ArgumentNullException(nameof(middleware));
            }

            _builder.Use(middleware);
            return(this);
        }
Exemplo n.º 28
0
 public static IQueryExecutionBuilder Map(
     this IQueryExecutionBuilder builder,
     FieldReference fieldReference,
     FieldMiddleware middleware)
 {
     return(builder.UseField(
                FieldClassMiddlewareFactory.Create <MapMiddleware>(
                    (s, n) => new MapMiddleware(
                        n, fieldReference, middleware(n)))));
 }
        public static IObjectFieldDescriptor UseDbContext <TDbContext>(
            this IObjectFieldDescriptor descriptor)
            where TDbContext : DbContext
        {
            string          scopedServiceName = typeof(TDbContext).FullName ?? typeof(TDbContext).Name;
            FieldMiddleware placeholder       = next => context => throw new NotSupportedException();

            descriptor
            .Use(next => async context =>
            {
                await using TDbContext dbContext = context.Services
                                                   .GetRequiredService <IDbContextFactory <TDbContext> >()
                                                   .CreateDbContext();

                try
                {
                    context.SetLocalValue(scopedServiceName, dbContext);
                    await next(context).ConfigureAwait(false);
                }
                finally
                {
                    context.RemoveLocalValue(scopedServiceName);
                }
            })
            .Use(placeholder)
            .Extend()
            .OnBeforeNaming((c, d) =>
            {
                if (d.ResultType is null)
                {
                    d.MiddlewareComponents.Remove(placeholder);
                    return;
                }

                if (TryExtractEntityType(d.ResultType, out Type? entityType))
                {
                    Type middleware = typeof(ToListMiddleware <>).MakeGenericType(entityType);
                    var index       = d.MiddlewareComponents.IndexOf(placeholder);
                    d.MiddlewareComponents[index] = Create(middleware);
                    return;
                }

                if (IsExecutable(d.ResultType))
                {
                    Type middleware = typeof(ExecutableMiddleware);
                    var index       = d.MiddlewareComponents.IndexOf(placeholder);
                    d.MiddlewareComponents[index] = Create(middleware);
                }

                d.MiddlewareComponents.Remove(placeholder);
            });

            return(descriptor);
        }
Exemplo n.º 30
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);
        }