public ConfigureRequestExecutorSetup(
     NameString schemaName,
     RequestExecutorSetup options)
 {
     SchemaName = schemaName.EnsureNotEmpty(nameof(schemaName));
     _configure = options.CopyTo;
 }
Beispiel #2
0
        private async Task CreateFactoryOptionsAsync(
            RemoteSchemaDefinition schemaDefinition,
            IList <IConfigureRequestExecutorSetup> factoryOptions,
            CancellationToken cancellationToken)
        {
            await using ServiceProvider services =
                            new ServiceCollection()
                            .AddGraphQL(_schemaName)
                            .AddRemoteSchema(
                                schemaDefinition.Name,
                                (sp, ct) => new ValueTask <RemoteSchemaDefinition>(schemaDefinition))
                            .Services
                            .BuildServiceProvider();

            IRequestExecutorOptionsMonitor optionsMonitor =
                services.GetRequiredService <IRequestExecutorOptionsMonitor>();

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

            factoryOptions.Add(new ConfigureRequestExecutorSetup(schemaDefinition.Name, options));

            options =
                await optionsMonitor.GetAsync(_schemaName, cancellationToken)
                .ConfigureAwait(false);

            factoryOptions.Add(new ConfigureRequestExecutorSetup(_schemaName, options));
        }
    public void Configure(RequestExecutorSetup options)
    {
        if (options is null)
        {
            throw new ArgumentNullException(nameof(options));
        }

        _configure(options);
    }
 public RegisteredExecutor(
     IRequestExecutor executor,
     IServiceProvider services,
     IDiagnosticEvents diagnosticEvents,
     RequestExecutorSetup setup)
 {
     Executor         = executor;
     Services         = services;
     DiagnosticEvents = diagnosticEvents;
     Setup            = setup;
 }
 public RegisteredExecutor(
     IRequestExecutor executor,
     IServiceProvider services,
     IExecutionDiagnosticEvents diagnosticEvents,
     RequestExecutorSetup setup,
     TypeModuleChangeMonitor typeModuleChangeMonitor)
 {
     Executor                = executor;
     Services                = services;
     DiagnosticEvents        = diagnosticEvents;
     Setup                   = setup;
     TypeModuleChangeMonitor = typeModuleChangeMonitor;
 }
        public async ValueTask <IRequestExecutor> GetRequestExecutorNoLockAsync(
            NameString schemaName = default,
            CancellationToken cancellationToken = default)
        {
            schemaName = schemaName.HasValue ? schemaName : Schema.DefaultName;

            if (!_executors.TryGetValue(schemaName, out RegisteredExecutor? re))
            {
                RequestExecutorSetup 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 ValueTask <RequestExecutorOptions> CreateExecutorOptionsAsync(
            RequestExecutorSetup options,
            CancellationToken cancellationToken)
        {
            var executorOptions = options.RequestExecutorOptions ?? new RequestExecutorOptions();

            foreach (RequestExecutorOptionsAction action in options.RequestExecutorOptionsActions)
            {
                if (action.Action is { } configure)
                {
                    configure(executorOptions);
                }

                if (action.AsyncAction is { } configureAsync)
                {
                    await configureAsync(executorOptions, cancellationToken).ConfigureAwait(false);
                }
            }

            return(executorOptions);
        }
        private async ValueTask <ISchema> CreateSchemaAsync(
            NameString schemaName,
            RequestExecutorSetup options,
            IServiceProvider serviceProvider,
            CancellationToken cancellationToken)
        {
            if (options.Schema is not null)
            {
                AssertSchemaNameValid(options.Schema, schemaName);
                return(options.Schema);
            }

            ISchemaBuilder schemaBuilder = options.SchemaBuilder ?? new SchemaBuilder();

            schemaBuilder.AddServices(serviceProvider);

            foreach (SchemaBuilderAction action in options.SchemaBuilderActions)
            {
                if (action.Action is { } configure)
                {
                    configure(serviceProvider, schemaBuilder);
                }

                if (action.AsyncAction is { } configureAsync)
                {
                    await configureAsync(serviceProvider, schemaBuilder, cancellationToken)
                    .ConfigureAwait(false);
                }
            }

            schemaBuilder.TryAddTypeInterceptor(new SetSchemaNameInterceptor(schemaName));

            ISchema schema = schemaBuilder.Create();

            AssertSchemaNameValid(schema, schemaName);
            return(schema);
        }
        private async Task <IServiceProvider> CreateSchemaServicesAsync(
            NameString schemaName,
            RequestExecutorSetup 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);
        }