Storefront API id's not the same

Storefront API id's not the same

frankbui
Tourist
3 0 4

So when I retrieve a list of addresses from the storefront api for a customer it gives me back the ids no problem.  However when I refresh and get the list again the id's appear to change. As another point they include an access token.  The problem I'm having is that there isn't a 'get single address query' in the storefront graphql endpoint (as far as I can tell, although I am bit new to graphql).  So my plan was to just get the list and search for the id on refresh and cache the addresses in memory.   Is there a better strategy to accomplish retrieving a single address by ID? and/or can I turn off the access token being appended to the id.

Replies 4 (4)

SBD_
Shopify Staff
1829 271 414

 there isn't a 'get single address query' in the storefront graphql endpoint 

It depends on which address you're after. If it's the customer's default address, a defaultAddress field is available:

 
{ 
  customer(customerAccessToken:"..."){
    defaultAddress {
      address1
      address2
      city
    }
  }
}
 

Scott | Developer Advocate @ Shopify 

frankbui
Tourist
3 0 4

Yeah but in the scenario where I would like to allow for the user to add multiple addresses, and then edit each of those addresses.  Our UI has the address form on its own page, so to cover all my bases that means a refresh should keep it on that address/page.

SBD_
Shopify Staff
1829 271 414
In that case I'd suggest getting a list of addresses on page load and storing the IDs:

{ 
  customer(customerAccessToken: "..."){
    addresses(first: 10) {
        edges {
          node {
            id
            address1
            ...etc
          }
        }
      }
    }
}

If the user creates a new address, keep track of its ID:

mutation customerAddressCreate($customerAccessToken: String!, $address: MailingAddressInput!) {
  customerAddressCreate(customerAccessToken: $customerAccessToken, address: $address) {
    customerAddress {
      id
    }
  }
}

When a change is made, use these IDs to update the address:

mutation customerAddressUpdate($id:ID!, $customerAccessToken: String!, $address: MailingAddressInput!) {
  customerAddressUpdate(id: $id, customerAccessToken: $customerAccessToken, address: $address) {
    customerAddress {
      id
    }
  }
}
 

Scott | Developer Advocate @ Shopify 

weotch
Shopify Partner
24 0 11

You can parse the id integer out of the base64ed ID to get a consistent id per record.  For example, if your id is

Z2lkOi8vc2hvcGlmeS9NYWlsaW5nQWRkcmVzcy83MDUxNjEzNDA1MzY4P21vZGVsX25hbWU9Q3VzdG9tZXJBZGRyZXNzJmN1c3RvbWVyX2FjY2Vzc190b2tlbj1TT01FX0FDQ0VTU19UT0tFTg==

Then...

id = atob(address.id).match(/^gid:\/\/shopify\/\w+\/(\w+)/)[1]
// id = 7051613405368