GraphQL: Queries and mutations Tutorial
In this post we will give you information about GraphQL: Queries and mutations Tutorial. Hear we will give you detail about GraphQL: Queries and mutations TutorialAnd how to use it also give you demo for it if it is necessary.
we are going to learn about how to create queries and mutations in the graphql server.
Queries are used to get the data from the graphql endpoint like get method in theRest API
Mutations are used to create or update or delete the data in graphql.
Let’s learn how to create a query in graphql if you don’t know about Graphql then please check out myprevious tutorial GraphQL intro.
Everything in graphql starts with creating a Schema.In graphql type system, there are two special types which are Query type and Mutation type.
Schema
type User{ id:ID! name:String!}type Query{ user(id:ID!):User!}
First, we created a User Object type with two fields id,name and alsoQuery type with one field called user.
Note: ( ! ) means that field is not accepted null values.
In graphql, we need to create a resolver function for every field present in the Query type so thatgraphql knows how to respond to those queries.
const resolver = { Query:{ user:function(parent,args,ctx,info){ return users.find((user)=>user.id === ctx.id) } }}
Note: The resolver function and Query field should have the same name otherwise graphql gives you an error.Learn more about resolvers
How to run a query in graphql?
To run a query we need to write a field name inside the curly braces and you need to specify what data you need to get back from these query like in below we asked id and name for the user id:1.
{ user(id:1){ id name }}
The output from the graphql server will give you json data back.
Aliases in graphql
Sometimes we need to run the same query twice in that situations we can use aliases in graphql.
{ user1: user(id<span>: 1) { id name } user2: user(id: 2) { id name }}
Nested Queries
Nested queries are something like a query inside a query.
{ user1: user(id<span>: 1) { id name todos { title body } }}
You can learn here how to create nested queries in graphql
Mutations in graphql
For mutations, we need to define a Mutation type with the fields like how we defined Query fields in the Schema.
type Mutation{ createUser(name:String!):User! udpateUser(id:ID!,name:String):User! deleteUser(id:ID!):User!}
we defined three fields in the Mutation type which are createUser, updateUser, deleteUser which is used to create a new user or update a user or delete the user.
Examples of how to run mutations:
mutation{ createUser(name:"king"){ name }}
mutation{ deleteUser(id<span>:1){ id name }}
I have created a test API you can download it from github and play with it so that you canmore clarity about queries and mutations.
happy coding…
Learn more about how to create graphql api
Hope this code and post will helped you for implement GraphQL: Queries and mutations Tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs