Using Mutations API to populate a field in Sanity Studio

20 replies
Last updated: Jan 6, 2022
Hey folks! I have a new field in Sanity Studio and I forgot to add an initial value for it...Is there a way for me to populate that field through the command lines for my hundreds of entries?
Jan 6, 2022, 12:09 AM
There is! You can use the Mutations API .
Jan 6, 2022, 12:24 AM
Great!
Jan 6, 2022, 12:27 AM
Great!
Jan 6, 2022, 12:27 AM
The weird thing is that I deployed with these initial values
initialValue: {
    saved: false,
    ash:3
  }
Jan 6, 2022, 12:27 AM
My field is in studio but the initial value is not...
Jan 6, 2022, 12:29 AM
In this situation, you wouldn't use
initialValue
. You would directly set the field. So, it would be something like:
{
  "saved": "false",
  "ash": "3"
}
Jan 6, 2022, 12:33 AM
You mean if I want to use Mutation?
Jan 6, 2022, 12:34 AM
But I would like to understand what I missed when deploying my schema.
Jan 6, 2022, 12:35 AM
Correct. Am I misinterpreting that second question 😅 ?
Jan 6, 2022, 12:35 AM
export default {
  name: "logs",
  title: "Burn logs",
  type: "document",
  fields: [
    {
      title: "Date",
      name: "date",
      type: "datetime",
      options: {
        dateFormat: 'YYYY-MM-DD',
        timeFormat: 'HH:mm'
      },
    },
    {
      title: "Collections",
      name: "collections",
      type: "reference",
      to: { type: 'collections' },
    },
    {
      title: "Token",
      description: "The token ID",
      name: "token",
      type: "string"
    },
    {
      title: "TX",
      description: "Transaction ID",
      name: "transactionID",
      type: "string"
    },
    {
      title: "ID",
      name: "id",
      type: "string"
    },
    {
      title: "Thumbnail",
      name: "thumbnail",
      type: "image"
    },
    {
      title: "Owner",
      name: "owner",
      type: "string"
    },
    {
      title: "Voxel ID",
      description: "This is the MINT ID of the voxel installation used to burn the NFT.",
      name: "voxelID",
      type: "string"
    },
    {
      title: "Number of ASH dropped",
      description: "Amount of $ASH dropped during the burning process...",
      name: "ash",
      type: "number",
      validation: Rule => Rule.required().integer().positive()
    },
    {
      title: "Booster ID",
      description: "This is the MINT ID of the booster NFT.",
      name: "boosterID",
      type: "string"
    },
    {
      title: "Last Words",
      name: "lastWords",
      type: "string",
      initialValue: "Rest in peace"
    },
    {
      title: 'Burn log published on discord?',
      name: 'published',
      type: 'boolean'
    },
    {
      title: 'Last words saved?',
      name: 'saved',
      type: 'boolean'
    }
  ],
  initialValue: {
    saved: false,
    ash:3
  }
}
Jan 6, 2022, 12:35 AM
Sorry, I have been coding for the last 15 hours my brain is fried.
Jan 6, 2022, 12:36 AM
Could you please confirm that I used 'initialValue' properly in this case?
Jan 6, 2022, 12:36 AM
OK, cool. Thanks for that. Yes, that looks correct. That initial value would show up in a newly created document.
Jan 6, 2022, 12:38 AM
Ok.
Jan 6, 2022, 12:38 AM
Make sense. I deployed before the inital value.
Jan 6, 2022, 12:38 AM
So now I need to patch everything.
Jan 6, 2022, 12:39 AM
Thank you Rachael.
Jan 6, 2022, 12:39 AM
Appreciate the patience 🙂
Jan 6, 2022, 12:39 AM
For sure! Let me know if it's not working as intended!
Jan 6, 2022, 12:39 AM
Of course. Thanks.
Jan 6, 2022, 12:39 AM

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.

Was this answer helpful?