Creating a new document inside an array in Sanity.io
28 replies
Last updated: Sep 4, 2020
A
Hi everyone! What's the correct way to make a mutation for this type of data in my document?
{ title: 'Contact options', name: 'options', type: 'array', of: [{ title: 'Option', type: 'option' }] }
Sep 3, 2020, 8:29 PM
H
This works for editing existing ones under an ID, but how do I create a new document under the same ID?
patch: {
'id': id,
"set": {
'options': [{
'_type': 'option',
'_key': key,
'title': title,
'duration': duration,
'price': price
}]
}
}
Sep 3, 2020, 8:30 PM
J
Have you had a look here? https://github.com/sanity-io/sanity/tree/next/packages/%40sanity/client#adding-elements-to-an-array
Sep 3, 2020, 8:32 PM
H
Thank you. Due to the project setup I'm not using the client at the moment, although it does make it a lot easier I think
Sep 3, 2020, 8:35 PM
H
Is there a way to achieve this without?
Sep 3, 2020, 8:36 PM
J
Perhaps this is what you want? https://www.sanity.io/docs/http-patches#insert-Q47ag9yE
Sep 3, 2020, 8:40 PM
A
Yes, with the exception that I'd like to create a new document and it seems patches are for modifying?
Sep 3, 2020, 9:39 PM
A
I have a document and inside this reference to another one:
{ title: 'Contact options', name: 'options', type: 'array', of: [{ title: 'Option', type: 'option' }] }
Sep 3, 2020, 9:50 PM
A
I want to create a new item (type option) inside this document (profile). I am able to edit existing option items with the below patch query, where ID is the name of parent document (profile):
patch: {
'id': id,
"set": {
'options': [{
'_type': 'option',
'_key': key,
'title': title,
'duration': duration,
'price': price
}]
}
}
Sep 3, 2020, 9:52 PM
J
Use the create mutation instead of patch https://www.sanity.io/docs/http-mutations#create-c732f27330a4
Sep 3, 2020, 9:53 PM
A
Exactly, but the same query doesn't work with "create" instead of "patch"
Sep 3, 2020, 9:53 PM
A
Can't figure out how to write the correct query?
Sep 3, 2020, 9:53 PM
A
"mutation failed on document "...": Document does not have a type"
Sep 3, 2020, 9:54 PM
A
create: {
'id': id,
"set": {
'options': [{
'_type': 'option',
'_key': key,
'title': title,
'duration': duration,
'price': price
}]
}
}
Sep 3, 2020, 9:55 PM
A
create: {
'id': id,
"set": {
'options': [{
'_type': 'option',
'_key': key,
'title': title,
'duration': duration,
'price': price
}]
}}
Sep 3, 2020, 9:56 PM
A
also tried without "set"
Sep 3, 2020, 9:56 PM
J
You need to provide a document type
_type: 'your-document-type'
Sep 3, 2020, 9:58 PM
A
yes - I'm sending '_type': 'option'
Sep 3, 2020, 9:58 PM
A
same as patch query
Sep 3, 2020, 9:58 PM
J
On the root document
Sep 3, 2020, 9:58 PM
J
create: {
"_id": id, "_type": "your-type", "options": [...]
}
Sep 3, 2020, 10:01 PM
J
(on mobile so excuse my brevity)
Sep 3, 2020, 10:01 PM
A
Ok, got it! That doesn't result in error but also doesn't create the option item 🤔
Sep 3, 2020, 10:01 PM
A
strange - still works with "patch" but just overrides the item every time. but doesn't do anything with "create" + document type
Sep 3, 2020, 10:06 PM
A
Right - i can see now it created the document type option separately, just not inside the parent document
Sep 3, 2020, 10:09 PM
A
the goal is to actually modify an existing document by creating a new one inside
Sep 3, 2020, 10:13 PM
A
wondering if it has to be patch + create somehow?
Sep 3, 2020, 10:13 PM
A
patch: {
'id': id,
"insert": {
"before": "options[-1]",
'items': [{
'_type': 'option',
'_key': key,
'title': title,
'duration': duration,
'price': price
}]
}
}
Sep 4, 2020, 9:37 AM
A
this is what worked
Sep 4, 2020, 9:37 AM
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.