Mutations
To perform a mutation you need to have a root Mutation object that is an ObjectGraphType
.
Mutations make modifications to data and return a result. You can only have a single root
Mutation object. By default according to specification mutations are executed serially.
Instead of using the query
keyword, you are required to use mutation
. Similar to a
query
, you can omit the Operation
name if there is only a single operation in the request.
mutation ($human:HumanInput!) {
createHuman(human: $human) {
id
name
}
}
The JSON request for this mutation would look like:
{
"query": "mutation ($human:HumanInput!){ createHuman(human: $human) { id name } }",
"variables": {
"human": {
"name": "Boba Fett",
"homePlanet": "Kamino"
}
}
}
C# class would look like:
public class Human
{
public string Name { get; set; }
public string HomePlanet { get; set; }
}
Set the Mutation
property on your Schema
.
public class StarWarsSchema : Schema
{
public StarWarsSchema(IServiceProvider provider)
: base(provider)
{
Query = provider.Resolve<StarWarsQuery>();
Mutation = provider.Resolve<StarWarsMutation>();
}
}
A mutation
GraphType
looks identical to a query
GraphType
. The difference is you are allowed to mutate data.
public class StarWarsMutation : ObjectGraphType
{
public StarWarsMutation(StarWarsData data)
{
Field<HumanType>("createHuman")
.Argument<NonNullGraphType<HumanInputType>>("human")
.Resolve(context =>
{
var human = context.GetArgument<Human>("human");
return data.AddHuman(human);
});
}
}
To provide a set of input values you must use InputObjectGraphType
.
public class HumanInputType : InputObjectGraphType
{
public HumanInputType()
{
Name = "HumanInput";
Field<NonNullGraphType<StringGraphType>>("name");
Field<StringGraphType>("homePlanet");
}
}
StarWarsData
is an in-memory data store.
public class StarWarsData
{
private List<Human> _humans = new List<Human>();
public Human AddHuman(Human human)
{
human.Id = Guid.NewGuid().ToString();
_humans.Add(human);
return human;
}
}
See the StarWars example for a full implementation.