示例#1
0
        public async Task Create_CoreMiddleware_InjectOptimizers()
        {
            // arrange
            RequestCoreMiddleware middleware = RequestClassMiddlewareFactory
                                               .Create <StubMiddleware <IEnumerable <ISelectionOptimizer> > >();
            var             applicationServices = new ServiceCollection().BuildServiceProvider();
            ServiceProvider schemaServices      = new ServiceCollection()
                                                  .AddSingleton <ISelectionOptimizer, StubOptimizer>()
                                                  .BuildServiceProvider();
            var schemaName = "_Default";
            IRequestExecutorOptionsAccessor optionsAccessor = new RequestExecutorOptions();
            var factoryContext = new RequestCoreMiddlewareContext(
                schemaName,
                applicationServices,
                schemaServices,
                optionsAccessor);
            var context = new RequestContext(
                new Mock <ISchema>().Object,
                new Mock <IServiceProvider>().Object,
                new Mock <IErrorHandler>().Object,
                new Mock <ITypeConverter>().Object,
                new Mock <IActivator>().Object,
                new Mock <IDiagnosticEvents>().Object,
                new Mock <IQueryRequest>().Object);

            // act
            RequestDelegate compiledMiddleware = middleware(factoryContext, context => default);

            await compiledMiddleware(context);


            // assert
            Assert.Single(
                (context.ContextData["result"] as IEnumerable <ISelectionOptimizer>) !);
        }
        public static IRequestExecutor MakeExecutable(
            this ISchema schema,
            RequestExecutorOptions options)
        {
            if (schema is null)
            {
                throw new ArgumentNullException(nameof(schema));
            }

            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(new ServiceCollection()
                   .AddGraphQL()
                   .Configure(o => o.Schema = schema)
                   .SetRequestOptions(() => options)
                   .Services
                   .BuildServiceProvider()
                   .GetRequiredService <IRequestExecutorResolver>()
                   .GetRequestExecutorAsync()
                   .Result);
        }
        private async Task <IServiceProvider> CreateSchemaServicesAsync(
            NameString schemaName,
            RequestExecutorFactoryOptions options,
            CancellationToken cancellationToken)
        {
            var lazy = new SchemaBuilder.LazySchema();

            RequestExecutorOptions executorOptions =
                await CreateExecutorOptionsAsync(options, cancellationToken)
                .ConfigureAwait(false);

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IApplicationServiceProvider>(
                s => new DefaultApplicationServiceProvider(_applicationServices));

            serviceCollection.AddSingleton(s => lazy.Schema);

            serviceCollection.AddSingleton(executorOptions);
            serviceCollection.AddSingleton <IRequestExecutorOptionsAccessor>(
                s => s.GetRequiredService <RequestExecutorOptions>());
            serviceCollection.AddSingleton <IInstrumentationOptionsAccessor>(
                s => s.GetRequiredService <RequestExecutorOptions>());
            serviceCollection.AddSingleton <IErrorHandlerOptionsAccessor>(
                s => s.GetRequiredService <RequestExecutorOptions>());
            serviceCollection.AddSingleton <IDocumentCacheSizeOptionsAccessor>(
                s => s.GetRequiredService <RequestExecutorOptions>());
            serviceCollection.AddSingleton <IRequestTimeoutOptionsAccessor>(
                s => s.GetRequiredService <RequestExecutorOptions>());

            serviceCollection.AddSingleton <IErrorHandler, DefaultErrorHandler>();

            serviceCollection.TryAddDiagnosticEvents();
            serviceCollection.TryAddOperationExecutors();
            serviceCollection.TryAddTimespanProvider();

            // register global error filters
            foreach (IErrorFilter errorFilter in _applicationServices.GetServices <IErrorFilter>())
            {
                serviceCollection.AddSingleton(errorFilter);
            }

            // register global diagnostic listener
            foreach (IDiagnosticEventListener diagnosticEventListener in
                     _applicationServices.GetServices <IDiagnosticEventListener>())
            {
                serviceCollection.AddSingleton(diagnosticEventListener);
            }

            serviceCollection.AddSingleton <IActivator>(
                sp => new DefaultActivator(_applicationServices));

            serviceCollection.AddSingleton(
                sp => CreatePipeline(
                    schemaName,
                    options.Pipeline,
                    sp,
                    sp.GetRequiredService <IRequestExecutorOptionsAccessor>()));

            serviceCollection.AddSingleton <IRequestExecutor>(
                sp => new RequestExecutor(
                    sp.GetRequiredService <ISchema>(),
                    _applicationServices.GetRequiredService <DefaultRequestContextAccessor>(),
                    _applicationServices,
                    sp,
                    sp.GetRequiredService <IErrorHandler>(),
                    _applicationServices.GetRequiredService <ITypeConverter>(),
                    sp.GetRequiredService <IActivator>(),
                    sp.GetRequiredService <IDiagnosticEvents>(),
                    sp.GetRequiredService <RequestDelegate>())
                );

            foreach (Action <IServiceCollection> configureServices in options.SchemaServices)
            {
                configureServices(serviceCollection);
            }

            var schemaServices   = serviceCollection.BuildServiceProvider();
            var combinedServices = schemaServices.Include(_applicationServices);

            lazy.Schema =
                await CreateSchemaAsync(schemaName, options, combinedServices, cancellationToken)
                .ConfigureAwait(false);

            return(schemaServices);
        }