THX-1138
THX-1138

Reputation: 21730

How to use projection on mutations using HotChocolate?

When using a mutation convention, the output of the method is a single object:

public class Mutation
{
    [UseMutationConvention]
    public User? UpdateUserNameAsync([ID] Guid userId, string username)
    {
    //...
    }
}

Does it mean that there is no way to avoid over-fetching from the database? In other words, do I have to load the user object completely (meaning put an Include and ThenInclude for every descendant property) before returning it?

To put it differently (only to demonstrate the problem rather than to propose a solution) - I could return IQueriable<User> that returns a collection containing a single object - that way I can make use of [UseProjection] which will avoid over-fetching from the database but the response of the mutation will then be an array, which is non-idiomatic.

Upvotes: 0

Views: 69

Answers (1)

Boocher
Boocher

Reputation: 27

When using a mutation convention, the output of the method is a single object:

You can have the return type be whatever you want, a bool, string, custom object, etc.

As far as loading the whole object, depends how you plan to implement the return type. We would need more details on the API.

The beauty of GraphQL is that the requester can specify what info it wants and ignore the rest, you can take advantage of that. I'd assume to run the update method you would have to load the entity into memory anyway in most cases, why not just return that in the operation?

Upvotes: 0

Related Questions