graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

Entity Framework tags are stripped out by Hot Chocolate

Open Strandedpirate opened this issue 3 years ago • 2 comments

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

  1. Setup a basic HC C# service with the Entity Framework 6.x.
  2. Write a query that gets tagged using the TagWith extension method as seen below.
  3. 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

Strandedpirate avatar Jul 29 '22 07:07 Strandedpirate

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 :)

tobias-tengler avatar Jul 29 '22 08:07 tobias-tengler

Thanks, the ToList was a typo. Updated.

Strandedpirate avatar Jul 29 '22 09:07 Strandedpirate

I think that, ideally, hotChocolate should produce a tag that matches the GraphQL query name out out of the box.

kawazoe avatar Feb 28 '23 15:02 kawazoe

It appears this is working now just in case folks are still watching this thread.

alexmarmon avatar Mar 15 '24 19:03 alexmarmon