diff --git a/Sample/GraphTypes/TodoType.cs b/Sample/GraphTypes/TodoType.cs index a700f3e..135c4c5 100644 --- a/Sample/GraphTypes/TodoType.cs +++ b/Sample/GraphTypes/TodoType.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using GraphQL.DataLoader; using GraphQL.DI; using Sample.DataLoaders; @@ -31,12 +28,13 @@ public class TodoObjType : DIObjectGraphBase public static bool Completed(Todo source) => source.Completed; public static DateTime? CompletedOn(Todo source) => source.CompletionDate; - public IDataLoaderResult CompletedBy(Todo source) + // here it is using an instance field "Source" + public IDataLoaderResult CompletedBy() { - if (!source.CompletedByPersonId.HasValue) + if (!Source.CompletedByPersonId.HasValue) return null; - return _personDataLoader.LoadAsync(source.CompletedByPersonId.Value); + return _personDataLoader.LoadAsync(Source.CompletedByPersonId.Value); } } } diff --git a/Sample/Startup.cs b/Sample/Startup.cs index 367a15c..5a00f11 100644 --- a/Sample/Startup.cs +++ b/Sample/Startup.cs @@ -1,9 +1,9 @@ +using System.Diagnostics; using EfLocalDb; using GraphQL; using GraphQL.AspNetCore3; using GraphQL.DI; using GraphQL.MicrosoftDI; -using GraphQL.Server; using GraphQL.SystemTextJson; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -11,11 +11,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Sample.DataLoaders; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; namespace Sample { @@ -34,21 +29,12 @@ public void ConfigureServices(IServiceCollection services) services.AddRazorPages(); services.AddGraphQL(b => b + .AddSchema() .ConfigureExecutionOptions(opts => opts.UnhandledExceptionDelegate = async e => Debug.WriteLine($"Unhandled exception:\n{e.Exception}\n")) .AddSystemTextJson() .AddDIGraphTypes() + .AddClrTypeMappings() .AddGraphTypes()); - services.AddSingleton(); - //foreach (var type in typeof(TodoSchema).Assembly.GetTypes().Where(x => x.IsClass && !x.IsAbstract && !x.IsGenericTypeDefinition)) { - // var baseType = type.BaseType; - // while (baseType != null) { - // if (baseType.IsGenericType && baseType.GetGenericTypeDefinition() == typeof(DIObjectGraphBase<>)) { - // services.AddScoped(type); - // break; - // } - // baseType = baseType.BaseType; - // } - //} //construct temporary database with scoped dbcontext instances services.AddSingleton(_ => new SqlInstance(builder => new TodoDbContext(builder.Options))); diff --git a/Sample/TodoSchema.cs b/Sample/TodoSchema.cs index 3e844c0..6561bf2 100644 --- a/Sample/TodoSchema.cs +++ b/Sample/TodoSchema.cs @@ -1,9 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Threading.Tasks; -using GraphQL.DI; using GraphQL.Types; using Sample.GraphTypes; @@ -13,76 +8,8 @@ public class TodoSchema : Schema { public TodoSchema(IServiceProvider serviceProvider, QueryType query, MutationType mutation) : base(serviceProvider) { - //this code will be an extension in a future version of GraphQL.NET - foreach (var typeMapping in GetClrTypeMappings(typeof(TodoSchema).Assembly)) { - RegisterTypeMapping(typeMapping.ClrType, typeMapping.GraphType); - } - Query = query; Mutation = mutation; } - - - /// - /// Scans the specified assembly for classes that inherit from , - /// , or , and - /// returns a list of mappings between matched classes and the source type or underlying enum type. - /// Skips classes where the source type is , or where the class is marked with - /// the . - /// - public static List<(Type ClrType, Type GraphType)> GetClrTypeMappings(Assembly assembly) - { - var typesToRegister = new Type[] - { - typeof(ObjectGraphType<>), - typeof(InputObjectGraphType<>), - typeof(EnumerationGraphType<>), - }; - - //create a list of type mappings - var typeMappings = new List<(Type clrType, Type graphType)>(); - - //loop through each type in the specified assembly - foreach (var graphType in assembly.GetTypes()) { - //skip types that are not graph types - if (!typeof(IGraphType).IsAssignableFrom(graphType)) - continue; - - //skip abstract types and interfaces - if (graphType.IsAbstract || graphType.IsInterface) - continue; - - //skip types marked with the DoNotRegister attribute - if (graphType.GetCustomAttributes(false).Any(y => y.GetType() == typeof(DoNotMapClrTypeAttribute))) - continue; - - //start with the base type - var baseType = graphType.BaseType; - while (baseType != null) { - //skip types marked with the DoNotRegister attribute - if (baseType.GetCustomAttributes(false).Any(y => y.GetType() == typeof(DoNotMapClrTypeAttribute))) - break; - - //look for generic types that match our list above - if (baseType.IsConstructedGenericType && typesToRegister.Contains(baseType.GetGenericTypeDefinition())) { - //get the base type - var clrType = baseType.GetGenericArguments()[0]; - - //as long as it's not of type 'object', register it - if (clrType != typeof(object)) - typeMappings.Add((clrType, graphType)); - - //skip to the next type - break; - } - - //look up the inheritance chain for a match - baseType = baseType.BaseType; - } - } - - //return the list of type mappings - return typeMappings; - } } }