4

I need to do something extra with the IQueryable generated, but im not able to create an interceptor in order to get the IQueryable (e.g log the query created by the GraphQL request).

Im still diving into the great material that is Hot chocolate, but for starters i have this:

enter image description here

Straight forward right? But now want an interceptor(or something like that) that gives me the rest of the generated IQueryable before the result to the body response.

Thank you,

Júlio Almeida
  • 1,539
  • 15
  • 23

1 Answers1

9

You can do this by using a middleware.

public class Query
{
    [UseYourCustom]
    [UseProjection]
    [UseFiltering]
    [UseSorting]
    public IQueryable<Person> GetPersons() => //...
}

public class UseYourCustomAttribute : ObjectFieldDescriptorAttribute
{
    public override void OnConfigure(
        IDescriptorContext context,
        IObjectFieldDescriptor descriptor,
        MemberInfo member)
    {
        descriptor.Use(next => async context =>
        {
            // before the resolver pipeline
            await next(context);
            // after the resolver pipeline

            if (context.Result is IQueryable<Person> query)
            {
               // all middleware are applied to `query`
            }
        });
    }
}

In Version 12 you will also be able to do the following:

public class Query
{
    [UseProjection]
    [UseFiltering]
    [UseSorting]
    public IQueryable<Person> GetPersons(IResolverContext context)
    {
        IQueryable<Person> person = //...

        var allMiddlewareApplied = persons
            .Sort(context)
            .Filter(context)
            .Project(context);

        return allMiddlewareApplied
    }
}

Pascal Senn
  • 1,712
  • 11
  • 20
  • Dear Pascal, i really need this behavior now, but mine is to get the list of ids from the where filtering of the GraphQL query, and rewrite the EF LINQ query before Hotchocolate creates the select, my question goes here : https://stackoverflow.com/questions/74809250/how-to-override-the-filtering-link-query-generated-by-hotchocolate – Mahamad Husen Dec 15 '22 at 11:20
  • How can I add paging middleware to this? with ApplyCursorPaginationAsync()? – Meeting Attender Mar 31 '23 at 22:32