DEV Community

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

Collapse
 
azmimansur profile image
AZMIMANSUR

I just try to add migrations, found error : Unable to create an object of type 'StoreDbContext'. For the different patterns supported at design time, see go.microsoft.com/fwlink/?linkid=85...

Collapse
 
antoniofalcaojr profile image
Antonio Falcão Jr. • Edited

Hello AZMIMANSUR!

This project has the database context in a different layer than the entry point.

To perform migrations, in this case, you must indicate both projects in the command:

dotnet ef migrations add "new_migration" -p .\src\Dotnet5.GraphQL3.Store.Repositories\ -s .\src\Dotnet5.GraphQL3.Store.WebAPI\
Enter fullscreen mode Exit fullscreen mode
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".