Discussion about creating mutations for bulk uploading product catalogs to Sanity via Python script

5 replies
Last updated: Nov 2, 2022
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

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
  }
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.
Nov 2, 2022, 2:45 PM
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
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
Fantastic!!
Nov 2, 2022, 6:17 PM
Fantastic!!
Nov 2, 2022, 6:17 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?