Magento 2 How to create and update customer using GraphQL

In this article, we will show you how to create customer and update customer.

Create customer

Use the createCustomer mutation to create a new customers. To return or modify information about a customer. Customer token no need to set in the header of GraphQL for customer registration.

Syntax

Copy
mutation {
    createCustomer(
    input: CustomerInput!
  ) {
    CustomerOutput
  }
}

You can use the following example for create customer.

Request

Copy
mutation{
  createCustomer(
    input: {
      firstname: "Developer"
      lastname: "Test"
      email: "devtest@example.com"
      password: "Dev@test123"
      is_subscribed: true
    }
  ){
    customer{
      firstname
      lastname
      email
      is_subscribed
    }
  }
}

Response

Copy
{
  "data": {
    "createCustomer": {
      "customer": {
        "firstname": "Developer",
        "lastname": "Test",
        "email": "devtest@example.com",
        "is_subscribed": true
      }
    }
  }
}

Update Customer

Use updateCustomer mutation to update the customer’s personal information. To return or modify information about a customer. Customer token need to set in the header of GraphQL using Generate Customer Token.

Syntax

Copy
mutation {
  updateCustomer(
    input: CustomerInput!
  ) {
    CustomerOutput
  }
}

Request

Copy
mutation{
  updateCustomer(
    input:{
      firstname: "Devtest"
      lastname: "Developer"
    }
  ){
    customer{
      firstname
      lastname
    }
  }
}

Response

Copy
{
  "data": {
    "updateCustomer": {
      "customer": {
        "firstname": "Devtest",
        "lastname": "Developer"
      }
    }
  }
}

That's it!!

If you are looking for more GraphQL solutions then check out our another article which is List of GraphQL Queries and Mutations for Magento 2.

I hope this article helps you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.




Search
Recent Posts
    Tags
    Newsletter
    SocialFollow on Twitter