Mutations
Lets start mutations by defining some Data Transfer Objects (DTO) that will provide our responses from the mutations.
The first is an interface for all mutation responses from our API to provide an ok and errors field.
using System.Collections.Generic;
namespace SlackClone.DTO
{
public interface IMutationResponse
{
public bool ok { get; set; }
public List<string> errors { get; set; }
}
}Next we will define the DTO for the mutation response.
using System;
using System.Collections.Generic;
namespace SlackClone.DTO
{
public class InsertMutationResponse : IMutationResponse
{
public bool ok { get; set; }
public List<string> errors { get; set; }
public Guid InsertedId { get; set; }
public InsertMutationResponse(bool _ok = false, List<string> _errors = null, Guid _id = default)
{
ok = _ok;
errors = _errors;
InsertedId = _id;
}
}
}
Next we will define the data structure for the inputs we want to receive to perform the mutation.
We can now define the Input Type in the GraphQL schema like below and set the Content field to required.
Now that we have the data structure we need for the input we can go ahead and write the method that will actually perform the mutation and save the data to the database.
Lastly we need to add the MutationType to our GraphQL Schema like seen below on line 39.
We can view the mutation got added in the Schema by launching the project and viewing the docs in playground.

We can next perform the mutation in the playground like below.
If all went well you should see the response below.
Last updated