Question

I am using Magento GraphQL api in my project. To create a customer address I used createCustomerAddress mutation(createCustomerAddress).

Below is the mutation that I have called to create the customer address :

mutation createAddress {
  createCustomerAddress(
    input: {
      firstname: "test"
      lastname: "name"
      company: "networld"
      telephone: "1231231231"
      street: ["test address line 1", "test address line 2"]
      city: "Rajkot"
      region: { region:"Gujarat", region_code: "GJ" }
      postcode: "360001"
      country_code: IN
    }
  ) {
    id
    prefix
    firstname
    lastname
    middlename
    city
    company
    country_code
    default_billing
    default_shipping
    postcode
    region {
      region
      region_code
    }
    street
    suffix
    telephone
    vat_id
  }
}

This is working properly and returning me the result as below :

{
  "data": {
    "createCustomerAddress": {
      "id": 44,
      "prefix": null,
      "firstname": "test",
      "lastname": "name",
      "middlename": null,
      "city": "Rajkot",
      "company": "networld",
      "country_code": "IN",
      "default_billing": false,
      "default_shipping": false,
      "postcode": "360001",
      "region": {
        "region": "Gujarat",
        "region_code": "GJ"
      },
      "street": [
        "test address line 1",
        "test address line 2"
      ],
      "suffix": null,
      "telephone": "1231231231",
      "vat_id": null
    }
  }
}

But, now when I query to get the customer address, it returning wrong region_code.

Here is the query I written to get the customer address :

query{
  customer{
    addresses{
      id
      firstname
      lastname
      street
      city
      region{
        region
        region_code
      }
      country_code
      postcode
      telephone
    }
  }
}

Result :

{
  "data": {
    "customer": {
      "addresses": [
        {
          "id": 44,
          "firstname": "test",
          "lastname": "name",
          "street": [
            "test address line 1",
            "test address line 2"
          ],
          "city": "Rajkot",
          "region": {
            "region": "Gujarat",
            "region_code": "Gujarat"
          },
          "country_code": "IN",
          "postcode": "360001",
          "telephone": "1231231231"
        }
      ]
    }
  }
}

As you can see, region_code in this query result and region_code in mutation result was different. Query not returning region_code that generated from the mutation. Mutation generated region_code was GJ and query returned region_code was Gujarat.

Can anyone help me why this is happening ? How to solve it ?

Was it helpful?

Solution

I have checked your query. It seems like you will have pass one additional field while calling "createCustomerAddress" mutation within "region" array. You will have to pass "region_id" along with "region" and "region_code" in that array.

Like this :

region: {
      region: "Georgia"
      region_code: "GA"
      region_id:19
    } 

If you pass like this then you will get the exact region code as "GA" in customer query which you call after this. I have already done this practise in my Altair. It should work fine for you as well.

FYI : My magento2 version is 2.3.3

Thanks,

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top