Introduction to GraphQL Mutations
source link: https://www.tuicool.com/articles/fUFZfam
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
In my previous blog, “ GraphQL Queries: GraphQL Essentials Part 1 ”, we learned how to generate queries by building a real-world application.
In this article, we will learn how to implement GraphQL mutations in our application.
What do we mean by mutations?
There are three things which fall under the umbrella of GraphQL mutations and they are:
- Create (POST)
- Update (PUT/PATCH)
- Delete
The structure remains the same as queries, and the only change is we take the data from the variables for the fields which we need to create, change, and delete via the API request.
As seen in the image, it's a blog application where we will initially be creating the users, their posts, and comments. We will also update their attributes and delete them too with mutations
We will begin by adding new operations in the GraphQL type definitions (schemas), arguments, and resolver functions.
Note : Kindly refer to the project structure and installed modules from my previous article. We will begin by creating new files and learn how mutations work.
Create a mutations.js file in the root of our project folder “graphql_nodejs”:
touch mutations.js
Insert the code below into the mutations.js
file to create type definitions. We will create a mutations object to define the input type.
We have created a mutation createNewUser
which takes the input params such as name
and email
as a String
data type, and age
as an Integer
which are the mandatory fields.
Now, we will create the resolver function createNewUser
.
The resolvers are the set of functions which give us the expected results for our type definitions, and we are expecting the createNewUser
function to create the new user if they don’t exist and then return the created user from the request.
some()
: This function will check all the elements of the array and returns true if the element exists. To learn more on some function, check the link below:
createNewUser API method
Creating a blog Post types
Typedefs for createPost
Resolver for createPost
As we can see in the above code, we are first checking whether the user exists, and if it does, we will create a new post or throw an error
Delete User
Typedefs for deleteUser
Resolver for deleteUser
deleteUser API:
Update User
Typedefs for updateUser
As you can see for the updateUser
mutation, we haven’t provided any field as the mandatory one, so you can update users name, email, or age.
Resolver for updateUser
In the above code, for updating the users, we will first check whether the user and its email exists. We are also checking whether the email and name is a string.
updateUser API:
Conclusion
In this article, we have learned the practical way of implementing GraphQL mutations with a GraphQL server in action .
I have created a repository for this app on my Github , please feel free to fork the code and try to run all the commands/code which I have mentioned above
I hope this article was helpful and hope you were able to understand how GraphQL mutations work. If you liked it please leave some claps to show your support. Also, leave your responses below and reach out to me if you face any issues.
Follow me on Twitter | Check out my LinkedIn | See my GitHub
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK