How would one go about efficiently creating metafields for products in shops with 10s of thousands of products

Robert40
Visitor
3 0 0

I'm developing an app and to be efficient and reduce the amount of data pulled from my servers (better for page load speeds etc) I'd like to store some data in the metafields.

Those are quite cool but what if a shop has, say 50k products? If I use the API I will have to wait for my installation process to run multiple days! Is there any way to create those metafields in bulk? Or any other efficient way? I can't find anything in the API description.

Replies 5 (5)

Alex
Shopify Staff
1561 81 341

One more hacky approach I've seen is actually storing all of your metafield data in one metafield by passing it a hash value:

{ "metafield": { "key": { "sub_key": "value", "sub_key_2": "value" } } }

We don't currently have a createMetafield mutation on our GraphQL admin API but that might be something to look for in the future as well, the whole idea behind our GQL api is to reduce request count so I wouldn't be surprised if that made things a bit easier too.

Alex | 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 the Shopify Help Center or the Shopify Blog

Robert40
Visitor
3 0 0

hmmmm yeah that would be a little bit better (4x better in my case) - would liquid render that tho in the templates?

I was looking through GQL, got very excited, learnt the basics just to find out that metafields were not a part of that API yet ¯\_(ツ)_/¯

Alex
Shopify Staff
1561 81 341

Hey Robert,

My mistake! I got confused because the way we handle it is a bit more different from the RESTful approach than I thought. You can add metafields through the product object directly rather than creating the metafield on its own:

https://help.shopify.com/en/api/graphql-admin-api/reference/mutation/productupdate

We just don't have a mutation to create a metafield externally. As long as you do it through the product you should be good. It's worth noting you can also perform multiple mutations in one request as well as long as you're within the complexity limit.

Cheers.

Alex | 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 the Shopify Help Center or the Shopify Blog

Robert40
Visitor
3 0 0

Riiiight. So you're saying that I can mutate multiple products at a time and as part of that mutation create a metafield? Do I understand correctly?

Could you share an example of how that would look like?

_Dominik_
Shopify Partner
31 0 11

Hey Alex,

related question. Can we create/update metafields on the shop level through the GraphQL Admin API yet? I see there is a "metafieldDelete" mutation bur I cannot find something along the lines of "shopUpdate" that woul let me do updates of shop metafields analog to "productUpdate".

Any ideas?

Thanks a ton for your time!

~Dom