Getting Global ID

minorbits
New Member
6 0 0

I already have an app with all the products listed that currently just redirects to the web on checkout. I want to be able to allow the user to see the variants of the products before opening the web and possible even use ApplePay, but I can't seem to figure out a way to pull that data from the SDK. I have the products ID and I have this code below for when I figure out how to get the global IDs.

 

let id    = GraphQL.ID(rawValue: "")

        let query = Storefront.buildQuery { $0

            .node(id: id) { $0

                .onProduct { $0

                    .id()

                    .title()

                }

            }

        }

 

Any help/guidance would be greatly appreciated.

0 Likes
Alex
Shopify Staff
Shopify Staff
1555 81 294

How are you getting the IDs in the first place? You can get a list of IDs with a products graphql query on the storefront, which you would be able to use for the part you're sharing.

 

I don't strongly recommend this, but if you already have a list of REST API IDs (ex. 1234567), you can construct a GID: gid://shopify/Product/9895276099

 

The base64 encoded version of that is the final representation of a GID. Again though, I don't recommend doing this (assuming the GID structure) if you can avoid it. It's best not to depend on that being the pattern forever. If we, for example, changed the structure of GIDs, your app might break. It's best to get the IDs with GraphQL itself first if possible.

 

Cheers.

0 Likes
domsteil
Shopify Partner
6 0 1

Hi Alex,

 

Is it possible to get the global ID without parsing "GID: gid://shopify/Product/9895276099" 

 

So ideally I could just get the string "9895276099"in the GraphQL request

0 Likes
juan3211
Shopify Partner
63 0 14

me too

0 Likes
Alex
Shopify Staff
Shopify Staff
1555 81 294

@juan3211 @domsteil You'd be looking for legacyResourceId rather than id which should exist on most resources with corresponding REST equivalents.

 

Cheers.

0 Likes
minorbits
New Member
6 0 0

Luckily for me out site add the web developer added the product handle to my API call and I just used the .productByHandle to get all the data on each product

0 Likes

Hey @juan3211 and @domsteil, did you find out the answer of getting the ID only instead of the whole string?

SPO - SEO App to research keywords & edit social link preview
0 Likes
Shayne
Shopify Staff
Shopify Staff
199 9 45

Currently, you're not able to fetch a portion of the GID. Your best option is to parse out the section of the string that you need from the GraphQL GID.

Shayne | Developer @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes