Create Customer Mutation: Magento 2 GraphQL Mutation

When we develop a shopping app in Magento using some other library like RackJS then we need to create mutation to load the data and retrieve it in the library like ReackJS.  Here we have given an example for Create Customer mutation in Magento 2 for a new customer account.

So in order to get the modified information about a customer, it's always good to use customer tokens in GraphQL calls in the header.

 

Create Customer Mutation for Magento 2 SignUp

Here we have created a sample example for mutation creation in GraphQL. This shows how we can receive the data of Magento 2customer account SignUp using GraphQL.

mutation {
createCustomer(
input: {
firstname: "Hemant"
lastname: "Singh"
email: "cs.hemantsingh@gmail.com"
password: "Admin$#21"
is_subscribed: true
}
) {
customer {
firstname
lastname
email
is_subscribed
}
}
}

 

Response of Create Customer Mutation Magento 2

Here is how to get the response when we type the query in Altair GraphQL tools.

Data we get in the JSON format.
Create Customer Mutation GraphQL

 

Magento 2 Customer Retrieve Data in GraphQL

When we type the query and the customer doesn't exist then it shows an error like the below image. Basically, it's not an error it's a message response that the customer doesn't exist.

{
customer {
firstname
lastname
suffix
email
addresses {
firstname
lastname
street
city
region {
region_code
region
}
postcode
country_code
telephone
}
}
}

 

Response When Customer Not Exist

Customer Not Exist

 

Generate Customer Tokens In GraphQL

If the customer already created an account then we can get the generated customer tokens using the below GraphQL query.

So this token basically we use to authorize the request header field for any queries and mutations.

mutation {
generateCustomerToken(email: "cs.hemantsingh@gmail.com", password: "Admin$#21") {
token
}
}

Response

Generate Customer Tokens

So when we try to generate the customer tokens in Magento 2 Using GraphQL then mutation needs a customer email address and password in the payload, as you can see in the above example how we have given customer details like email and password.

Token Validity in Magento is generally for 1 hour.

Change Token Validity: Admin > Stores > Settings > Configuration > Services > OAuth > Access Token Expiration > Customer Token Lifetime.

 

 

Recommended Post:

WhatsApp Chat: Add Magento 2 Whatsapp Extension

Sort By Price: Add Sort By Price High to Low & Low To High in Magento 2

Magento 2 SmartSearch: Best Smart Search Auto Complete Module