Mutation
Mutation has to start with the keyword mutation
unlike query. Mutation is similar to POST
, PUT
in REST API.
Client side
To write a mutation, we follow the below format:
mutation AddUserToDatabase {
addUser(user: {username: "test", password: "password"}) {
username
password
}
}
in here, we put in our object as an argument
{
username: "test",
password: "password"
}
Serverside
On serverside, first we will need a schema:
type Mutation {
addUser(user: UserInput): User
}
input UserInput {
username: String
password: String
}
type User {
username: String
password: String
}
Note: Here we have to declare our own input
. Even if we have the User
to be the exact same type as UserInput
, we still need to declare it.
Next, we declare our resolver:
var rootValue = {
addUser: (args, context, info) => {
return args.user;
}
};
Our resolver simply just return the inputed user
.
{
"data": {
"addUser": {
"username": "test",
"password": "password"
}
}
}