json-api-dotnet / JsonApiDotNetCore

A framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core.
https://www.jsonapi.net
MIT License
674 stars 160 forks source link

Async GetMeta resource definition hook #1602

Open kostas-kapasakis opened 1 month ago

kostas-kapasakis commented 1 month ago

Is your feature request related to a problem? Please describe. No

Describe the solution you'd like It would be ideal to be able to have an asynchronous GetMetaAsync in order to use Repositories and Services to provide resource specific information.,

In several scenarios that we may want to show meta information for each resource which are not static , the current synchronous implementation unfortunately would lead to break async , await chains that would lead to performance issues,

An example is a scenario in which the meta section would hold authorization information for the resource and would retrieve them from a logic that required data from DB , thus services and repositories calls .

Describe alternatives you've considered As alternative solutions currently we have thought that we either add a respective property in the resource class or just using GetAwaiter().GetResult() in the asynchronous calls inside the GetMeta().

Additional context Not sure if the GetMeta has a different scope or wasnt meant to be dealing with async operations , so please correct me if thats the case.

Thanks in advance.

bkoelman commented 1 month ago

It's unclear to me what you're proposing. Where would this method exist? IResourceDefinition.GetMeta executes as part of rendering the response, I don't like the idea of executing additional queries from there, which introduces an N+1 problem. I would expect such logic to run from IResourceService if the data originates from another system, or as part of IResourceRepository if the data exists in the same database (joined with the existing query). From there, it could be stored in the resource itself (in a non-publicly exposed property), which gets later converted to JSON:API meta. It highly depends on what exactly you need.

kostas-kapasakis commented 1 month ago

Hello @bkoelman thanks for the response.

I like the idea that you propose , but as far as I searched I wasnt able to affect the resource's meta section outside of the GetMeta hook.

Is there a way to affect the resource's meta section outside of the GetMeta hook ?

bkoelman commented 1 month ago

No, there isn't, that's why I suggested to fetch the data upfront and store in it resource properties, so it's available for you to produce meta.

kostas-kapasakis commented 1 month ago

I see . thanks now it makes more sense.

In terms of timing , would you suggest fetching data on the service or is there a more suitable definition hook for that ?

bkoelman commented 1 month ago

As I said, it depends on what you need.

Provide the exact details on what you're trying to accomplish, including which endpoints need to contain meta, where the data comes from, etc. if you need more specific guidance.