-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGraphQLExtensions.DependencyInjection.cs
More file actions
34 lines (31 loc) · 2.1 KB
/
Copy pathGraphQLExtensions.DependencyInjection.cs
File metadata and controls
34 lines (31 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma warning disable IDE0130 // Namespace does not match folder structure; by design.
namespace Microsoft.Extensions.DependencyInjection;
#pragma warning restore IDE0130 // Namespace does not match folder structure
/// <summary>
/// Provides <see cref="IServiceCollection"/> extension methods to register the GraphQL-lite <see cref="IGraphQLEngine"/>.
/// </summary>
public static class GraphQLExtensions
{
/// <summary>
/// Adds a singleton <see cref="GraphQLEngine"/> as the GraphQL-lite <see cref="IGraphQLEngine"/> and its registered query roots to the <paramref name="services"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">The action to configure the <see cref="GraphQLLiteOptions"/> (register roots via <see cref="GraphQLLiteOptions.AddQuery{TItem}(string, QueryArgsConfig, Func{QueryArgs?, PagingArgs?, CancellationToken, Task{IItemsResult{TItem}}})"/> and
/// <see cref="GraphQLLiteOptions.AddGet{TItem}"/>), given the root <see cref="IServiceProvider"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> to support fluent-style method-chaining.</returns>
/// <remarks>The <see cref="IGraphQLEngine"/> is registered as a singleton; where a root resolver needs a scoped dependency (e.g. a repository or application service), resolve it per-invocation
/// (e.g. via <c>CoreEx.ExecutionContext.GetRequiredService<T>()</c>, which reads from the ambient <c>ExecutionContext</c>'s scoped service provider) rather than capturing an instance resolved
/// from the <paramref name="services"/> collection's root <see cref="IServiceProvider"/>.</remarks>
public static IServiceCollection AddCoreExGraphQLLite(this IServiceCollection services, Action<GraphQLLiteOptions, IServiceProvider> configure)
{
services.ThrowIfNull();
configure.ThrowIfNull();
services.AddSingleton<IGraphQLEngine>(sp =>
{
var options = new GraphQLLiteOptions();
configure(options, sp);
return new GraphQLEngine(options);
});
return services;
}
}