Using Mutations API to populate a field in Sanity Studio
20 replies
Last updated: Jan 6, 2022
J
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
J
Great!
Jan 6, 2022, 12:27 AM
J
Great!
Jan 6, 2022, 12:27 AM
J
The weird thing is that I deployed with these initial values
ash:3
}
initialValue: {saved: false,
ash:3
}
Jan 6, 2022, 12:27 AM
J
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
J
You mean if I want to use Mutation?
Jan 6, 2022, 12:34 AM
J
But I would like to understand what I missed when deploying my schema.
Jan 6, 2022, 12:35 AM
J
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
J
Sorry, I have been coding for the last 15 hours my brain is fried.
Jan 6, 2022, 12:36 AM
J
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
J
Ok.
Jan 6, 2022, 12:38 AM
J
Make sense. I deployed before the inital value.
Jan 6, 2022, 12:38 AM
J
So now I need to patch everything.
Jan 6, 2022, 12:39 AM
J
Thank you Rachael.
Jan 6, 2022, 12:39 AM
J
Appreciate the patience 🙂
Jan 6, 2022, 12:39 AM
J
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.