GraphQLBuilderExtensions

We found 10 examples in language CSharp for this search. You will see 16 fragments of code.
        /// <summary>
        /// Registers <see cref="MemoryDocumentCache"/> as a singleton of type <see cref="IDocumentCache"/> within the
        /// dependency injection framework, and configures it with the specified configuration delegate.
        /// </summary>
        public static IGraphQLBuilder AddMemoryCache(this IGraphQLBuilder builder, Action<MemoryDocumentCacheOptions>? action = null)
            => builder.AddDocumentCache<MemoryDocumentCache>().Configure(action);

        /// <inheritdoc cref="AddMemoryCache(IGraphQLBuilder, Action{MemoryDocumentCacheOptions})"/>
        public static IGraphQLBuilder AddMemoryCache(this IGraphQLBuilder builder, Action<MemoryDocumentCacheOptions, IServiceProvider>? action)
            => builder.AddDocumentCache<MemoryDocumentCache>().Configure(action);

        /// <summary>
        /// Registers the Newtonsoft.Json <see cref="DocumentWriter"/> as a singleton of type
        /// <see cref="IDocumentWriter"/> within the dependency injection framework and configures
        /// it with the specified configuration delegate.
        /// </summary>
        public static IGraphQLBuilder AddNewtonsoftJson(this IGraphQLBuilder builder, Action<JsonSerializerSettings>? action = null)
            => builder.AddDocumentWriter<DocumentWriter>().Configure(action);

        /// <inheritdoc cref="AddNewtonsoftJson(IGraphQLBuilder, Action{JsonSerializerSettings})"/>
        public static IGraphQLBuilder AddNewtonsoftJson(this IGraphQLBuilder builder, Action<JsonSerializerSettings, IServiceProvider>? action)
            => builder.AddDocumentWriter<DocumentWriter>().Configure(action);

using GraphQL;
using GraphQL.Server;
using GraphQL.Server.Ui.Altair;
using GraphQLDemo;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCors(options =>
{
    options.AddPolicy("default", p =>
    {
        p.AllowAnyOrigin();
        p.AllowAnyMethod();
    });
});

GraphQL.MicrosoftDI.GraphQLBuilderExtensions
    .AddGraphQL(builder.Services)
    .AddServer(false)
    .AddSystemTextJson()
    .AddWebSockets()
    .AddDocumentExecuter<SubscriptionDocumentExecuter>()
    .AddGraphTypes()
    .AddSchema<DemoSchema>();

var app = builder.Build();

const string subscriptionsEndpoint = "/subscriptions";
const string graphQLEndPoint = "/graphql";

app.UseCors("default");
app.UseWebSockets();
app.UseGraphQLWebSockets<DemoSchema>(subscriptionsEndpoint);
app.UseGraphQL<DemoSchema>(graphQLEndPoint);
app.UseGraphQLAltair(new AltairOptions { SubscriptionsEndPoint = subscriptionsEndpoint, GraphQLEndPoint = graphQLEndPoint });

app.Run();

        /// <summary>
        /// Registers the System.Text.Json <see cref="DocumentWriter"/> as a singleton of type
        /// <see cref="IDocumentWriter"/> within the dependency injection framework and configures
        /// it with the specified configuration delegate.
        /// </summary>
        public static IGraphQLBuilder AddSystemTextJson(this IGraphQLBuilder builder, Action<JsonSerializerOptions>? action = null)
            => builder.AddDocumentWriter<DocumentWriter>().Configure(action);

        /// <inheritdoc cref="AddSystemTextJson(IGraphQLBuilder, Action{JsonSerializerOptions})"/>
        public static IGraphQLBuilder AddSystemTextJson(this IGraphQLBuilder builder, Action<JsonSerializerOptions, IServiceProvider>? action)
            => builder.AddDocumentWriter<DocumentWriter>().Configure(action);

        public static void AddWeatherSchema(this IServiceCollection services)
        {
            services.AddSingleton<IGraphQLRequestDeserializer>(new GraphQLRequestDeserializer(options => { }));
            services.AddSingleton<IGraphQLExecuter<WeatherSchema>, DefaultGraphQLExecuter<WeatherSchema>>();
            services.AddTransient<WeatherForecastController>();

            GraphQL.MicrosoftDI.GraphQLBuilderExtensions.AddGraphQL(services)
                .AddSystemTextJson()
                .AddGraphTypes()
                .AddSchema<WeatherSchema>();
        }

        /// <summary>
        /// Returns a new <see cref="IGraphQLBuilder"/> for the specified service collection and
        /// registers a default set of services required by GraphQL if they have not already been registered.
        /// <br/><br/>
        /// Does not include <see cref="IDocumentWriter"/>, and the default <see cref="IDocumentExecuter"/>
        /// implementation does not support subscriptions.
        /// <br/><br/>
        /// Also configures <see cref="ComplexityConfiguration"/> to be pulled from the dependency
        /// injection provider, overwriting values within <see cref="ExecutionOptions.ComplexityConfiguration"/>
        /// with values configured within the registered instance if set there.
        /// </summary>
        public static IGraphQLBuilder AddGraphQL(this IServiceCollection services)
            => new GraphQLBuilder(services);

        /// <summary>
        /// Registers <typeparamref name="TSchema"/> within the dependency injection framework. <see cref="ISchema"/> is also
        /// registered if it is not already registered within the dependency injection framework. Services required by
        /// <typeparamref name="TSchema"/> are instianted directly if not registered within the dependency injection framework.
        /// This can eliminate the need to register each of the graph types with the dependency injection framework, either
        /// manually or via <see cref="GraphQL.GraphQLBuilderExtensions.AddGraphTypes(IGraphQLBuilder)"/>. Singleton and scoped
        /// lifetimes are supported.
        /// </summary>
        /// <remarks>
        /// Schemas that implement <see cref="IDisposable"/> of a transient lifetime are not supported, as this will cause a
        /// memory leak if requested from the root service provider.
        /// </remarks>
        public static IGraphQLBuilder AddSelfActivatingSchema<TSchema>(this IGraphQLBuilder builder, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
            where TSchema : class, ISchema
        {
            if (serviceLifetime == ServiceLifetime.Transient && typeof(IDisposable).IsAssignableFrom(typeof(TSchema)))
            {
                // This scenario can cause a memory leak if the schema is requested from the root service provider.
                // If it was requested from a scoped provider, then there is no reason to register it as transient.
                // See following link:
                // https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#disposable-transient-services-captured-by-container
                throw new InvalidOperationException("A schema that implements IDisposable cannot be registered as a transient service.");
            }

            // Register the service with the DI provider as TSchema, overwriting any existing registration
            builder.Register(services =>
            {
                var selfActivatingServices = new SelfActivatingServiceProvider(services);
                var schema = ActivatorUtilities.CreateInstance<TSchema>(selfActivatingServices);
                return schema;
            }, serviceLifetime);

            // Now register the service as ISchema if not already registered.
            builder.TryRegister<ISchema>(services =>
            {
                var selfActivatingServices = new SelfActivatingServiceProvider(services);
                var schema = ActivatorUtilities.CreateInstance<TSchema>(selfActivatingServices);
                return schema;
            }, serviceLifetime);

            return builder;
        }

        /// <summary>
        /// Registers <see cref="SubscriptionDocumentExecuter"/> as a singleton of type
        /// <see cref="IDocumentExecuter"/> within the dependency injection framework.
        /// </summary>
        public static IGraphQLBuilder AddSubscriptionDocumentExecuter(this IGraphQLBuilder builder)
            => builder.AddDocumentExecuter<SubscriptionDocumentExecuter>();

        /// <summary>
        /// Adds the GraphQL authorization.
        /// </summary>
        /// <param name="builder">The GraphQL builder.</param>
        /// <returns></returns>
        public static IGraphQLBuilder AddGraphQLAuthorization(this IGraphQLBuilder builder)
        {
            builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            builder
                .Services
                .AddTransient<IValidationRule, AuthorizationValidationRule>()
                .AddAuthorization();
            return builder;
        }

        /// <summary>
        /// Adds the GraphQL authorization.
        /// </summary>
        /// <param name="builder">The GraphQL builder.</param>
        /// <param name="options">An action delegate to configure the provided <see cref="AuthorizationOptions"/>.</param>
        /// <returns>The GraphQL builder.</returns>
        public static IGraphQLBuilder AddGraphQLAuthorization(this IGraphQLBuilder builder, Action<AuthorizationOptions> options)
        {
            builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            builder
                .Services
                .AddTransient<IValidationRule, AuthorizationValidationRule>()
                .AddAuthorization(options);
            return builder;
        }

        /// <summary>
        /// Registers <see cref="DataLoaderDocumentListener"/> and <see cref="DataLoaderContextAccessor"/> within the
        /// dependency injection framework and configures the document listener to be added to the list of document
        /// listeners within <see cref="ExecutionOptions.Listeners"/> upon document execution.
        /// </summary>
        public static IGraphQLBuilder AddDataLoader(this IGraphQLBuilder builder)
        {
            builder.AddDocumentListener<DataLoaderDocumentListener>();
            builder.Register<IDataLoaderContextAccessor, DataLoaderContextAccessor>(ServiceLifetime.Singleton);
            return builder;
        }

        /// <summary>
        /// Adds the GraphQL authorization.
        /// </summary>
        /// <param name="builder">The GraphQL builder.</param>
        /// <returns></returns>
        public static IGraphQLBuilder AddGraphQLAuthorization(this IGraphQLBuilder builder)
        {
            builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            builder
                .Services
                .AddTransient<IValidationRule, AuthorizationValidationRule>()
                .AddAuthorization();
            return builder;
        }

        /// <summary>
        /// Adds the GraphQL authorization.
        /// </summary>
        /// <param name="builder">The GraphQL builder.</param>
        /// <param name="options">An action delegate to configure the provided <see cref="AuthorizationOptions"/>.</param>
        /// <returns>The GraphQL builder.</returns>
        public static IGraphQLBuilder AddGraphQLAuthorization(this IGraphQLBuilder builder, Action<AuthorizationOptions> options)
        {
            builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            builder
                .Services
                .AddTransient<IValidationRule, AuthorizationValidationRule>()
                .AddAuthorization(options);
            return builder;
        }