private async ValueTask <ISchema> CreateSchemaAsync(
     NameString schemaName,
     RequestExecutorFactoryOptions options,
     IServiceProvider serviceProvider,
     CancellationToken cancellationToken)
 {
     if (options.Schema is { })
示例#2
0
        public async ValueTask <IEnumerable <INamedRequestExecutorFactoryOptions> > GetOptionsAsync(
            CancellationToken cancellationToken)
        {
            IEnumerable <RemoteSchemaDefinition> schemaDefinitions =
                await GetSchemaDefinitionsAsync(cancellationToken)
                .ConfigureAwait(false);

            var list = new List <INamedRequestExecutorFactoryOptions>();
            var serviceCollection           = new ServiceCollection();
            IRequestExecutorBuilder builder = serviceCollection.AddGraphQL();

            foreach (RemoteSchemaDefinition schemaDefinition in schemaDefinitions)
            {
                builder.AddRemoteSchema(
                    schemaDefinition.Name,
                    (sp, ct) => new ValueTask <RemoteSchemaDefinition>(schemaDefinition));

                IServiceProvider services = serviceCollection.BuildServiceProvider();
                IRequestExecutorOptionsMonitor optionsMonitor =
                    services.GetRequiredService <IRequestExecutorOptionsMonitor>();

                RequestExecutorFactoryOptions options =
                    await optionsMonitor.GetAsync(schemaDefinition.Name, cancellationToken)
                    .ConfigureAwait(false);

                // list.Add(new NamedRequestExecutorFactoryOptions(schemaDefinition.Name, options));
            }

            return(list);
        }
        public async ValueTask <IRequestExecutor> GetRequestExecutorNoLockAsync(
            NameString schemaName = default,
            CancellationToken cancellationToken = default)
        {
            schemaName = schemaName.HasValue ? schemaName : Schema.DefaultName;

            if (!_executors.TryGetValue(schemaName, out RegisteredExecutor? re))
            {
                RequestExecutorFactoryOptions options =
                    await _optionsMonitor.GetAsync(schemaName, cancellationToken)
                    .ConfigureAwait(false);

                IServiceProvider schemaServices =
                    await CreateSchemaServicesAsync(schemaName, options, cancellationToken)
                    .ConfigureAwait(false);

                re = new RegisteredExecutor
                     (
                    schemaServices.GetRequiredService <IRequestExecutor>(),
                    schemaServices,
                    schemaServices.GetRequiredService <IDiagnosticEvents>(),
                    options
                     );

                foreach (OnRequestExecutorCreatedAction action in options.OnRequestExecutorCreated)
                {
                    action.Action?.Invoke(re.Executor);

                    if (action.AsyncAction is not null)
                    {
                        await action.AsyncAction.Invoke(re.Executor, cancellationToken)
                        .ConfigureAwait(false);
                    }
                }

                re.DiagnosticEvents.ExecutorCreated(schemaName, re.Executor);
                _executors.TryAdd(schemaName, re);
            }

            return(re.Executor);
        }
        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);
        }