graphql-platform
graphql-platform copied to clipboard
Entity Framework tags are stripped out by Hot Chocolate
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the bug
Recent versions of the Entity Framework allow developers to "tag" their queries with any number of custom defined strings. This greatly improves traceability and determining exactly where in the codebase a particular query is being generated from. HC is stripping these tag strings out, presumably because of either HC projections, sorting, or paging attributes.
Steps to reproduce
- Setup a basic HC C# service with the Entity Framework 6.x.
- Write a query that gets tagged using the
TagWithextension method as seen below. - Resulting SQL does not contain the tag(s) added.
[UseDbContext(typeof(LegacyDbContext))]
[UsePaging(IncludeTotalCount = true)]
[UseProjection]
[UseSorting]
public async Task<IQueryable<Order>> GetOrderHistory(
[ScopedService] LegacyDbContext dbContext,
CancellationToken ct,
string userId,
)
{
var query = dbContext.Orders
.TagWith(nameof(GetOrderHistory))
.TagWith("i was here")
.Where(a => a.UserGuid == userId);
return query;
}
This should produce the following SQL:
-- GetOrderHistory
-- i was here
SELECT [o].OrderId
FROM [dbo].[Order] AS [o]
WHERE [o].[UserGuid] = @__userGuid_0
but instead procudes just:
SELECT [o].OrderId
FROM [dbo].[Order] AS [o]
WHERE [o].[UserGuid] = @__userGuid_0
Relevant log output
No response
Additional Context?
No response
Product
Hot Chocolate
Version
12.11.1
This is unrelated to your actual issue and you have probably just typed it out wrong for the example, but calling ToList() will materialize the query result in the resolver and all of the data middleware are applied in memory. If you want to forward Filters, Projections, etc. to the data source you need to return the "un-materialized" query:
return dbContext.Orders
.TagWith(nameof(GetOrderHistory))
.TagWith("i was here")
.Where(a => a.UserGuid == userId);
Also since you are using the latest version of HC, there are better ways to register the DbContext than UseDbContext: https://chillicream.com/docs/hotchocolate/integrations/entity-framework
That being said, I hope somebody else can help you with thr tag issue :)
Thanks, the ToList was a typo. Updated.
I think that, ideally, hotChocolate should produce a tag that matches the GraphQL query name out out of the box.
It appears this is working now just in case folks are still watching this thread.