-
-
Notifications
You must be signed in to change notification settings - Fork 761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom Interface Support for HotChocolate.Types.Analyzers Codegen with [DataLoader] Attribute #8023
Comments
I currently handle this as follows:
|
This is pretty much the same as the "Current workaround" I mentioned. You still have to register your DataLoaders in DI yourself, because the code-generated |
Ah, right, I'm registering them using Scrutor. Your suggestion seems reasonable, but we'll see what Michael says. 🙂 |
That is another thing I am concerned about because this may be a mistake. Getting a DataLoader is more complex than just getting a scoped service; it manages its own scope. It's better to leave it to code generation. |
Ya, this was an old side project, and a lot has changed since then. As a temporary solution, does it work to call |
This is incorrect, in a clean architecture project you define the interfaces without any generator in your In the infrastructure sits the generator and it will produce a partial class. In there implement your interface to the partial class and wire up DI. You do not need to completely wire up DI just add your interface as an additional. internal sealed partial class BrandByIdDataLoader : IBrandByIdDataLoader;
services.AddScoped<IBrandByIdDataLoader>(s => s.GetRequiredService<BrandByIdDataLoader>()); |
I am sorry, but why is this incorrect? I have my I have my generator in Infrastructure (Persistence) layer generating And I am asking if
Oh, ok, that ExecutionDataLoaderScope.cs looks scary, but if its ok to simply |
this remark is incorrect ... i should have quoted .... We have built the code generation with clean architecture in mind. in your infrastructure layer you need to configure the code gen to not generate interfaces for the DataLoader and then use the partials. In general when we designed it we considered the interface on the attribute but found it inflexible and error prone. With a partial class you get lots more flexibility. |
Fair enough 👍 Thank for your time. This part helped me a lot:
Because I thought that you have to get your DataLoader via |
Product
Hot Chocolate
Is your feature request related to a problem?
When using
HotChocolate.Types.Analyzers
codegen with[DataLoader]
attribute, it would be beneficial to have a way to generate and register DataLoaders with custom interfaces.Reason:
Currently, there is no way to use the code-generated IDataLoader<,> implementation in a Clean (Onion) Architecture.
edit: Well, this statement of mine is wrong. You have to write partial class to generated dataloader by hand, just something quick as
internal sealed partial class BrandByIdDataLoader : IMyAwesomeDataLoader;
and add it to DI withservices.AddScoped<IMyAwesomeDataLoader>(s => s.GetRequiredService<BrandByIdDataLoader>());
Clean Architecture in a Nutshell:
MyAwesomeApp(.Presentation)
->MyAwesomeApp.Infrastructure
(GreenDonut.Data.EntityFramework + HotChocolate.Types.Analyzers) ->MyAwesomeApp.Application
(GreenDonut.Abstractions + MediatR) ->MyAwesomeApp.Domain
This is based on the schema from the blog post: Hot Chocolate 15
The problem is that the auto-generated interfaces are in
MyAwesomeApp.Infrastructure
, but the MediatR RequestHandler implementations are inMyAwesomeApp.Application
, making them inaccessible.Current workaround:
MyAwesomeApp.Infrastructure
(partial to the source-generated one) marking it as implementing the custom interface.The solution you'd like
My code
IMyAwesomeDataLoader interface in
MyAwesomeApp.Application
library:Implementation in
MyAwesomeApp.Infrastructure
library:Generated code
Generated
GreenDonutDataLoaderModule.735550c.g.cs
Generated
GreenDonutDataLoaderModule.735550c.g.cs
(Registration with DI)Possible problems:
typeof
in attribute is possible from C# version 7.3 (May 2018) but I am not sure. My googlefu is failing me, but AFAIK its possible from the same version as usage ofnameof()
in attribute.class FooDataLoader : BatchDataLoader<,>, IMyAwesomeDataLoader {}
). That should be new feature request, because its more complicated - there can be more then one interface to register.The text was updated successfully, but these errors were encountered: