Solved

Consistency between integer ID and GID, are they always the same?

AldiRvr
Shopify Partner
2 0 0

Hi, I'm trying to migrate from using Admin REST API to the GraphQL API.

One thing that caught my attention is that the objects in REST API and GraphQL has a different yet similar id format.

 

Let's say for a particular product, the id from REST API is 12345, and the gid from GraphQL will be gid://shopify/Product/12345.

 

What I want to know is, are the integer ID and the last part of gid always the same? In other words, if a product has the integer id `x`, is it guaranteed that the gid will be `gid://shopify/Product/${x}`?

 

I've read this discussion and also some docs about the gid and how to migrate from REST to GraphQL, but seems that none of them mentioned whether id and gid are always consistent or not. I noticed that for "InventoryLevel" object, the gid has a slightly different format, but that's the only case and I personally don't use it in my app.

 

A bit more context,

I have stored the integer ID of some objects (mostly Product and ProductImage) in my database because I need it when interacting with the REST API. In order to migrate to GraphQL, I'm initially thinking of storing the gid as well, but for the case of object like "Product", I'm considering to generate the gid with something like `"gid://shopify/Product/" + product.id` to save some storage and not dealing with unnecessary complex stuff.

 

Thanks in advance!

Accepted Solution (1)

ShopifyDevSup
Shopify Staff
1358 226 475

This is an accepted solution.

Hey @AldiRvr

 

This is interesting. I definitely see how storing these values without the entire GID can save on space. In most cases, the GID and Simple ID will be the same (for example the product ID), so it should work in your context. I can't guarantee that all fields will be this way though. 

 

What I would recommend is to first validate the fields are the same before storing them. As mentioned in the doc you looked at above, many REST fields will include the equivalent GID in the response, and alternatively, many Graphql objects have a legacyResourceID field that will share the equivalent ID in REST. You can use those fields to first validate that they are the same. Alternatively, if you're retrieving the data from REST, use the admin_graphql_api_id field to populate the id for your storage, stripping away the "gid://shopify/Product/" fields first 🙂 

 

Hope that helps! 

 

- Kyle G.

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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

View solution in original post

Replies 3 (3)

ShopifyDevSup
Shopify Staff
1358 226 475

This is an accepted solution.

Hey @AldiRvr

 

This is interesting. I definitely see how storing these values without the entire GID can save on space. In most cases, the GID and Simple ID will be the same (for example the product ID), so it should work in your context. I can't guarantee that all fields will be this way though. 

 

What I would recommend is to first validate the fields are the same before storing them. As mentioned in the doc you looked at above, many REST fields will include the equivalent GID in the response, and alternatively, many Graphql objects have a legacyResourceID field that will share the equivalent ID in REST. You can use those fields to first validate that they are the same. Alternatively, if you're retrieving the data from REST, use the admin_graphql_api_id field to populate the id for your storage, stripping away the "gid://shopify/Product/" fields first 🙂 

 

Hope that helps! 

 

- Kyle G.

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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

AldiRvr
Shopify Partner
2 0 0

Hi Kyle, thank you very much for your quick response.

 

I see, understood then.

 

However, about this one


I can't guarantee that all fields will be this way though.

 Do you mind explaining what you meant by "all fields"?

 

Thank you!

ShopifyDevSup
Shopify Staff
1358 226 475

Hey @AldiRvr

 

Happy to clarify. What I meant by all fields, is I can't guarantee that all of the current and future GID's are going to map 1:1 with the corresponding ID in REST. This is why it's good to do a quick check before using it to ensure that it's equal. 

 

Thanks, 

 

- Kyle G. 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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