graphql-dotnet/conventions

Allow internal methods

huysentruitw opened this issue · 3 comments

I'm currently stuck on something I'm trying to solve and the only thing I can think of that we'd have to modify the ObjectReflector so it also finds internal methods. Let me try to explain the situation:

We are creating a pluggable component packed in a NuGet package. The component exposes a public MyComponentQuery and MyComponentMutation class. In the host, we can simply plug these two classes in our GraphQL Query and Mutation object respectively.

However, I currently have to expose too much inside the component because of the public methods, f.e.: public async Task<MyModel> Get([Inject] IMyRepository repository, int id), this means I also have to make IMyRepository public and all our database entities returned by the repository, even though we're mapping them to an output model inside the method. This makes the usage of the package less clear.

I could inject an IServiceProvider instead of IMyRepository and than resolve the repository from the provider instead, but this makes unit-tests a little bit harder as I need to setup a DI container or mock the GetService<IMyRepository> call.

Does anyone have an other solution to this problem?

tlil commented

Hmm, I see. Have you tried to re-jig it using a data loader and delegating registration of each ID-to-object mapping for type T to the respective assembly?

E.g., something like this:

var dl = new DataLoader(/* ... */);
moduleA.RegisterDataLoaderMappings(dl);
// (...)
moduleZ.RegisterDataLoaderMappings(dl);

You could then skip the dependency injection in the GraphQL object / resolve, and do something like (excuse me for the use of pseudo code, but hopefully you get the gist):

public async Task<MyModel> Get(IContext ctx, ID id) => ctx.dataLoader.Get(id);

Thanks for your response Tommy.

I'm not sure if I understand what you're trying to do there. I'm currently not using DataLoader yet in this setup. What do you see inside RegisterDataLoaderMapping and can you be more specific about that Get implementation?

tlil commented

Ah, just going through old issues and found that I hadn't replied to this yet, sorry.

Have you checked out Lee's video walk-through (https://www.youtube.com/watch?v=OQTnXNCDywA)? :-)

Will close out this issue. But happy to chat further if you wanted to discuss alternative approaches.