Dedicated to the Hydrogen framework, headless commerce, and building custom storefronts using the Storefront API.
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.
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
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.
{ customer(customerAccessToken: "..."){ addresses(first: 10) { edges { node { id address1 ...etc } } } } }
mutation customerAddressCreate($customerAccessToken: String!, $address: MailingAddressInput!) { customerAddressCreate(customerAccessToken: $customerAccessToken, address: $address) { customerAddress { id } } }
mutation customerAddressUpdate($id:ID!, $customerAccessToken: String!, $address: MailingAddressInput!) { customerAddressUpdate(id: $id, customerAccessToken: $customerAccessToken, address: $address) { customerAddress { id } } }
Scott | Developer Advocate @ Shopify
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