Querying for nested parent references and generating URLs in a Slack thread.
5 replies
Last updated: Mar 10, 2022
W
Hi everyone, is it possible to create a loop within a query to avoid the nesting I'm doing with the parent below?Basically it's checking for a parent reference on the parent page so we can generate a URL.
Also wondering if it's possible to create an output within the query that would join all of these together like "fullSlug":
parent.slug/ parent.slug/slug.current
`const link = ``
Also wondering if it's possible to create an output within the query that would join all of these together like "fullSlug":
parent.slug/ parent.slug/slug.current
`const link = ``
title,
url,
"page": page->{
"type": _type,
"slug": slug.current,
"parent": parent->{
"slug": slug.current,
"id": _id,
"parent": parent->{
"slug": slug.current,
"id": _id,
"parent": parent->{
"slug": slug.current,
"id": _id,
"parent": parent->{
"slug": slug.current,
"id": _id
}
}
}
},
},
Mar 8, 2022, 7:10 PM
W
Thanks User, I thought that was the case but wasn't sure if I was missing something.Also just confirming that there isn't a smarter way that I could output the fullslug within the query as a variable without writing a separate function?
Mar 8, 2022, 7:14 PM
The only suggestion I would make would be that you may want to handle the compounding of the different slugs within the Studio itself, similar to this guide.
But if that's not an option I'm not sure that there is a cleaner way to GROQ it out.
But if that's not an option I'm not sure that there is a cleaner way to GROQ it out.
Mar 8, 2022, 7:20 PM
W
great, thanks
user M
that is what I'm looking forMar 8, 2022, 7:29 PM
W
user M
if it helps anyone else, I've written the function from the docs to allow for endless nesting. Is it possible to extend the "onAfterPublish" function so that we could automate this if a parent updates their slug, to then update all children without it being a manual process?async function getPrefix(doc, source, ref) {
const docTitle = doc[source]
`// const refQuery = `*[_id == $ref][0].title``
const refParams = {ref: doc?.[ref]?._ref}
if (!refParams.ref) {
return slugify(docTitle, slugifyConfig)
}
// const childTitles = [docTitle];
const refTitles = await getParentTitles(doc);
if (!refTitles) {
return slugify(docTitle, slugifyConfig)
}
const slugArray = refTitles.filter((p) => p).map((p) => slugify(p, slugifyConfig))
return slugArray
}
async function getParentTitles(doc, childTitles){
if(childTitles){
childTitles.unshift(doc.title);
} else {
childTitles = [doc.title];
}
// Fetch the parent pages
if( doc.parent ){
const ref = doc.parent._ref;
const params = {ref: ref};`const query = `*[_type == "page" && _id == "${ref}"][0]{"title": title, "parent": parent->{"_ref": _id, "title": title}}`;`
const foundDoc = await client.fetch(query, params);
if( foundDoc !== undefined ){
childTitles = await getParentTitles(foundDoc, childTitles);
}
}
return childTitles;
}
Mar 10, 2022, 8:04 PM
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.