DEV Community

Discussion on: About a .NET 5 (RC1) Web application with GraphQL 3

Collapse
 
azmimansur profile image
AZMIMANSUR • Edited

Thanks, Antonio

Working now.

Thread Thread
 
azmimansur profile image
AZMIMANSUR

Hi Antonio,

Question again.

I created a service below to update, I'm still confused about how to mapper changed between clientModel with client ?

public async Task<Client> EditClientAsync(Guid id, ClientModel clientModel, CancellationToken cancellationToken)
{
    if (clientModel is null)
    {
        NotificationContext.AddNotificationWithType(ServicesResource.Object_Null, typeof(ClientModel));
        return default;
    }

    var client = await Repository.GetByIdAsync(
        id: id,
        asTracking: true,
        cancellationToken: cancellationToken);

    //Mapper ?

    var updateClient = await OnEditAsync(client, cancellationToken);

    return updateClient;
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
antoniofalcaojr profile image
Antonio Falcão Jr.

Hi AZMIMANSUR!

In this case, the abstractions implemented in the application already resolve a "save" of a new entity (root aggregate) for you. Just need to add a new field in the respective mutation class.

For that, and based on "GraphQL for .NET", you will need an InputObjectGraphType<T> for the input, and an ObjectGraphType<T> for output.

public class StoreMutation : ObjectGraphType
{
    public StoreMutation()
    {
            FieldAsync<ClientModelGraphType>(
                name: "createClient",
                arguments: new QueryArguments(new QueryArgument<NonNullGraphType<ClientInputGraphType>> {Name = "client"}),
                resolve: async context 
                    => await context.RequestServices
                        .GetRequiredService<IClientService>()
                        .SaveAsync(
                            model: context.GetArgument<ClientModel>("client"), 
                            cancellationToken: context.CancellationToken));
    }
}
Enter fullscreen mode Exit fullscreen mode

Obs: Is necessary to create a new migration including the new table "Clients".