1

I am trying to project my EF Core queries with Automapper to allow an 3rd layer in my application but I have some trouble allowing HotChocolate to request fields on my DTO and telling Automapper to include those fields in the projecting process.

Some important points beforehand:

  • Automapper will request all NavigationProperties on a Map when both properties are present
  • Except when the ExplicitExpansion setting is present
  • If ExplicitExpansion is set on a map, HotChocolate cannot expand the navigation properties of the IQueryable with the use of [UseProjection]

So I could ether load all navigation properties at once or none.

How can I tell ether HotChocolate to map the entities in my IQueryable, or how can I get the required keys in my query function to tell AutoMapper which properties to expand by using the IQueryable<T>.ProjectTo() method?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Venson
  • 1,772
  • 17
  • 37

1 Answers1

0

Did you try?

public class Query
{
    [UseProjection] //<----
    public IQueryable<FooDto> GetFoos([Service]YourService svc)=> svc.GetFooDtos();
}

If the projection is not too complex this should work

If the order of the projection is a problem you can also create custom attributes

    public class YourCustomMiddlewareAttribute : ObjectFieldDescriptorAttribute
    {
        public override void OnConfigure(
            IDescriptorContext context, 
            IObjectFieldDescriptor descriptor, 
            MemberInfo member)
        { 
            descriptor.Type<ListType<ObjectType<PersonDto>>>();
            descriptor.Use(next => async context =>
            {
                await next(context);
                if (context.Result is IQueryable<Person> persons)
                { 
                    context.Result = persons
                         .ProjectTo<PersonDto>()
                         .ToListAsync(context.RequestAborted);
                }
            })
        }
    }
public class Query
{
    [YourCustomMiddleware]
    [UseProjection]
    public IQueryable<FooDto> GetFoos([Service]YourService svc)=> svc.GetFooDtos();
}
Pascal Senn
  • 1,712
  • 11
  • 20
  • That does not work unfortunately. The HotChocolate Projection is applied *after* automapper has projected the entity within `GetFooDtos` and therefore expanded all or none navigation properties. – Venson Apr 27 '21 at 13:58
  • https://gist.github.com/JPVenson/4c8fce7aec5930a95206c780b7e68eba This is the query I used and explained in my 3 points. Is there a way to get all fields requested from HotChocolate? Then i could use them in my query to tell AutoMapper what fields should be expanded – Venson Apr 27 '21 at 14:01
  • If the oder is a problem, you can also create you custom attributes. I will update the answer – Pascal Senn Apr 27 '21 at 14:04
  • Hey, Thanks alot for the idea with a custom middelware it is definently a step in the right direction. But now I am getting an error. It seems like HotChocolate does not like it when i change the result type. Error: https://gist.github.com/JPVenson/825c1fd29a1b33ad84c85723dac98eb2 – Venson Apr 27 '21 at 14:31
  • You do have access to the type, so you can change the type too I will update again – Pascal Senn Apr 27 '21 at 17:16
  • I tried the explicit type setting, but this yields another HotChocolate error. https://gist.github.com/JPVenson/825c1fd29a1b33ad84c85723dac98eb2#file-usemappingmiddelware-cs-L24 – Venson Apr 27 '21 at 20:28
  • @Venson can you hard code the types first? just to make sure that it would work if the types are set correctly. – Pascal Senn Apr 27 '21 at 21:07