-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathDapperGraphQLOptions.cs
More file actions
106 lines (95 loc) · 4.28 KB
/
DapperGraphQLOptions.cs
File metadata and controls
106 lines (95 loc) · 4.28 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Reflection;
using GraphQL;
using GraphQL.Types;
using Microsoft.Extensions.DependencyInjection;
namespace Dapper.GraphQL
{
/// <summary>
/// Options used to configure the dependency injection container for GraphQL and Dapper.
/// </summary>
public class DapperGraphQLOptions
{
private readonly IServiceCollection serviceCollection;
public DapperGraphQLOptions(IServiceCollection serviceCollection)
{
this.serviceCollection = serviceCollection;
}
/// <summary>
/// Adds a GraphQL query builder to the container.
/// </summary>
/// <typeparam name="TModelType">The model type to be queried.</typeparam>
/// <typeparam name="TQueryBuilder">The query builder class.</typeparam>
/// <returns>The GraphQLOptions object.</returns>
public DapperGraphQLOptions AddQueryBuilder<TModelType, TQueryBuilder>()
where TQueryBuilder : class, IQueryBuilder<TModelType>
{
serviceCollection.AddSingleton<IQueryBuilder<TModelType>, TQueryBuilder>();
return this;
}
/// <summary>
/// Adds a GraphQL query builder to the container.
/// </summary>
/// <param name="modelType">The model type to be queried.</param>
/// <param name="queryBuilderType">The query builder class, must implement IQueryBuilder<modelType></param>
/// <returns>The GraphQLOptions object.</returns>
public DapperGraphQLOptions AddQueryBuilder(Type modelType, Type queryBuilderType)
{
var queryBuilderInterface = typeof(IQueryBuilder<>).MakeGenericType(modelType);
if (!queryBuilderType.IsConcrete() || !queryBuilderInterface.IsAssignableFrom(queryBuilderType))
{
throw new ArgumentException($"QueryBuilder type must be concrete and implement IQueryBuilder<{modelType.Name}>.");
}
serviceCollection.Add(new ServiceDescriptor(queryBuilderInterface, queryBuilderType, ServiceLifetime.Singleton));
return this;
}
/// <summary>
/// Adds a GraphQL schema to the container.
/// </summary>
/// <typeparam name="TGraphSchema">The schema type to be mapped.</typeparam>
/// <returns>The GraphQLOptions object.</returns>
public DapperGraphQLOptions AddSchema<TGraphSchema>() where TGraphSchema : class, ISchema
{
serviceCollection.AddSingleton<TGraphSchema>();
return this;
}
/// <summary>
/// Adds a GraphQL schema to the container.
/// </summary>
/// <param name="graphSchemaType">The schema type to be mapped, must implement ISchema.</param>
/// <returns>The GraphQLOptions object.</returns>
public DapperGraphQLOptions AddSchema(Type graphSchemaType)
{
if (!graphSchemaType.IsConcrete() || !typeof(ISchema).IsAssignableFrom(graphSchemaType))
{
throw new ArgumentException("Type must be concrete and implement ISchema.");
}
serviceCollection.Add(new ServiceDescriptor(graphSchemaType, graphSchemaType, ServiceLifetime.Singleton));
return this;
}
/// <summary>
/// Adds a GraphQL type to the container.
/// </summary>
/// <typeparam name="TGraphType">The model type to be mapped.</typeparam>
/// <returns>The GraphQLOptions object.</returns>
public DapperGraphQLOptions AddType<TGraphType>() where TGraphType : class, IGraphType
{
serviceCollection.AddSingleton<TGraphType>();
return this;
}
/// <summary>
/// Adds a GraphQL type to the container.
/// </summary>
/// <param name="type">The model type to be mapped, must implement IGraphType.</param>
/// <returns>The GraphQLOptions object.</returns>
public DapperGraphQLOptions AddType(Type type)
{
if (!type.IsConcrete() || !typeof(IGraphType).IsAssignableFrom(type))
{
throw new ArgumentException("Type must be concrete and implement IGraphType.");
}
serviceCollection.Add(new ServiceDescriptor(type, type, ServiceLifetime.Singleton));
return this;
}
}
}