How to create a new document inside an array in Sanity
26 replies
Last updated: Sep 4, 2020
D
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
L
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
D
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
D
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
D
Is there a way to achieve this without?
Sep 3, 2020, 8:36 PM
D
Perhaps this is what you want? https://www.sanity.io/docs/http-patches#insert-Q47ag9yE
Sep 3, 2020, 8:40 PM
D
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
D
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
D
Use the create mutation instead of patch https://www.sanity.io/docs/http-mutations#create-c732f27330a4
Sep 3, 2020, 9:53 PM
D
Exactly, but the same query doesn't work with "create" instead of "patch"
Sep 3, 2020, 9:53 PM
D
Can't figure out how to write the correct query?
Sep 3, 2020, 9:53 PM
D
"mutation failed on document "...": Document does not have a type"
Sep 3, 2020, 9:54 PM
D
create: {
'id': id,
"set": {
'options': [{
'_type': 'option',
'_key': key,
'title': title,
'duration': duration,
'price': price
}]
}}
Sep 3, 2020, 9:56 PM
D
also tried without "set"
Sep 3, 2020, 9:56 PM
D
You need to provide a document type
_type: 'your-document-type'
Sep 3, 2020, 9:58 PM
D
yes - I'm sending '_type': 'option'
Sep 3, 2020, 9:58 PM
D
same as patch query
Sep 3, 2020, 9:58 PM
D
On the root document
Sep 3, 2020, 9:58 PM
D
create: {
"_id": id, "_type": "your-type", "options": [...]
}
Sep 3, 2020, 10:01 PM
D
(on mobile so excuse my brevity)
Sep 3, 2020, 10:01 PM
D
Ok, got it! That doesn't result in error but also doesn't create the option item š¤
Sep 3, 2020, 10:01 PM
D
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
D
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
D
patch: {
'id': id,
"insert": {
"before": "options[-1]",
'items': [{
'_type': 'option',
'_key': key,
'title': title,
'duration': duration,
'price': price
}]
}
}
Sep 4, 2020, 9:37 AM
D
this is what worked
Sep 4, 2020, 9:37 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.