|
| 1 | +using System.Reflection; |
| 2 | +using GraphQL.DI; |
| 3 | + |
| 4 | +namespace GraphQL; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Provides extension methods to configure GraphQL.NET services within a dependency injection framework. |
| 8 | +/// </summary> |
| 9 | +public static class GraphQLDIBuilderExtensions |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Performs the following: |
| 13 | + /// <list type="bullet"> |
| 14 | + /// <item> |
| 15 | + /// Registers <see cref="DIObjectGraphType{TDIGraph}"/> and |
| 16 | + /// <see cref="DIObjectGraphType{TDIGraph, TSource}"/> as generic types. |
| 17 | + /// </item> |
| 18 | + /// <item> |
| 19 | + /// Scans the calling assembly for classes that implement <see cref="IDIObjectGraphBase{TSource}"/> |
| 20 | + /// and registers them as transients within the DI container. |
| 21 | + /// </item> |
| 22 | + /// <item> |
| 23 | + /// Scans the calling assembly for classes that implement <see cref="IDIObjectGraphBase{TSource}"/> and |
| 24 | + /// registers clr type mappings on the schema between that <see cref="DIObjectGraphType{TDIGraph, TSource}"/> |
| 25 | + /// (constructed from that class and its source type), and the source type. |
| 26 | + /// Skips classes where the source type is <see cref="object"/>, or where the class is marked with |
| 27 | + /// the <see cref="DoNotMapClrTypeAttribute"/>, or where another graph type would be automatically mapped |
| 28 | + /// to the specified type, or where a graph type has already been registered to the specified clr type. |
| 29 | + /// </item> |
| 30 | + /// </list> |
| 31 | + /// </summary> |
| 32 | + public static IGraphQLBuilder AddDI(this IGraphQLBuilder builder) |
| 33 | + => AddDI(builder, Assembly.GetCallingAssembly()); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Performs the following: |
| 37 | + /// <list type="bullet"> |
| 38 | + /// <item> |
| 39 | + /// Registers <see cref="DIObjectGraphType{TDIGraph}"/> and |
| 40 | + /// <see cref="DIObjectGraphType{TDIGraph, TSource}"/> as generic types. |
| 41 | + /// </item> |
| 42 | + /// <item> |
| 43 | + /// Scans the specified assembly for classes that implement <see cref="IDIObjectGraphBase{TSource}"/> |
| 44 | + /// and registers them as transients within the DI container. |
| 45 | + /// </item> |
| 46 | + /// <item> |
| 47 | + /// Scans the specified assembly for classes that implement <see cref="IDIObjectGraphBase{TSource}"/> and |
| 48 | + /// registers clr type mappings on the schema between that <see cref="DIObjectGraphType{TDIGraph, TSource}"/> |
| 49 | + /// (constructed from that class and its source type), and the source type. |
| 50 | + /// Skips classes where the source type is <see cref="object"/>, or where the class is marked with |
| 51 | + /// the <see cref="DoNotMapClrTypeAttribute"/>, or where another graph type would be automatically mapped |
| 52 | + /// to the specified type, or where a graph type has already been registered to the specified clr type. |
| 53 | + /// </item> |
| 54 | + /// </list> |
| 55 | + /// </summary> |
| 56 | + public static IGraphQLBuilder AddDI(this IGraphQLBuilder builder, Assembly assembly) |
| 57 | + { |
| 58 | + return builder |
| 59 | + .AddDIGraphTypes() |
| 60 | + .AddDIGraphBases(assembly) |
| 61 | + .AddDIClrTypeMappings(assembly); |
| 62 | + } |
| 63 | +} |
0 commit comments