Discussion about creating mutations for bulk uploading product catalogs to Sanity via Python script
5 replies
Last updated: Nov 2, 2022
J
Hi All! I’m currently working on some bulk upload for uploading product catalogs to sanity via a python script (later to be made into lamda function). All is going quite smoothly however… I am trying to make a mutation for the HTTP API that adds a reference to each variant per product. My mutation looks as follows
However when I call this, the products are created in such a way that they crash the desk, as so…
Edit: Note that variant.id and variant.key are both strings and are currently the same thing. I set the ID when I upload the variants originally and so have assumed I can reuse them for references.
mutations = [] for taxon in Taxons: for product in taxon.products: mutations.append({ 'createOrReplace': { '_type': 'product', '_id': product.id, 'name': product.name, # 'description': product.description, 'composition': product.composition, 'coo': product.coo, 'slug': product.slug, 'minimumOrderQuantity': product.moq, 'orderIncrement': product.oqi, 'reference': product.reference, # 'leadtime': product.leadtime, 'variants': { '_type': 'array', 'of': [ { '_type': 'reference', '_ref': variant.id, '_key': variant.key } for variant in product.variants ] } } }) mutation = { 'mutations': mutations }
Edit: Note that variant.id and variant.key are both strings and are currently the same thing. I set the ID when I upload the variants originally and so have assumed I can reuse them for references.
Nov 2, 2022, 2:45 PM
J
see attached
Nov 2, 2022, 2:49 PM
Hey
user U
! What immediately stands out to me is that your variants array isn't structured correctly. If that field is an array in your schema, you directly set the array. No need to wrap it in an object with a specified _type!
Nov 2, 2022, 6:04 PM
J
Hazar I’ve fixed it.
mutations.append({ 'createOrReplace': { '_type': 'product', '_id': product.id, 'name': product.name, # 'description': product.description, 'composition': product.composition, 'coo': product.coo, 'slug': product.slug, 'minimumOrderQuantity': product.moq, 'orderIncrement': product.oqi, 'reference': product.reference, 'variants': [ { '_type': 'reference', '_ref': variant.id, '_key': variant.key } for variant in product.variants ] } })
Nov 2, 2022, 6:16 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.