Querying multiple documents with dereferenced fields in a single query
12 replies
Last updated: Aug 16, 2022
A
Might be a silly question, but what am I doing wrong here?
All I'm trying to do is fetch multiple documents in a single query, while being able to define the fields for each
{ "error": { "description": "expected '}' following object body", "end": 65, "query": "*[_type in ["banner", "footer"]] { _type == "footer" => { ... } }", "start": 40, "type": "queryParseError" } }
Aug 16, 2022, 11:02 AM
K
Odd, that looks okay.
Aug 16, 2022, 11:21 AM
S
could you post your query just in case?
Aug 16, 2022, 12:12 PM
A
Sure - Here's all the logic simplified:
import { createClient } from "next-sanity"; const sanityClient = createClient({ dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || "production", projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || "", apiVersion: "2021-10-21", useCdn: process.env.NODE_ENV === "production", }); const res = await sanityClient.fetch(`*[_type in ["banner", "footer"]] { _type == "footer" => { ... } }`);
Aug 16, 2022, 12:18 PM
A
Alternatively, do you perhaps have some query to suggest for fetching multiple documents while dereferencing their content?
e.g.
But applied for multiple documents in a single query
e.g.
*[_type == "footer"] { ..., socials[] { ..., social-> } }
Aug 16, 2022, 12:23 PM
S
What exactly are you trying to do, if I may ask?
Aug 16, 2022, 12:36 PM
A
Just need to dereference documents within documents so their content can be accessible
Aug 16, 2022, 12:38 PM
S
yes there is a way to do it, but I cannot help unless I understand/have an example to work with šThis
wonderful groq tutorial (interactive) by
wonderful groq tutorial (interactive) by
user B
can help you quite a bit as well as this article in our docs about groqAug 16, 2022, 12:46 PM
A
Thanks for the links, it looks pretty great but it doesn't really touch on what I'm looking for
Essentially, let's imagine I have a document that references another document. When querying it we don't receive the actual document, but rather the reference of it. Instead, what I would need is the actual document dereferenced.
In other words if this is the schema:
To get the
Now, what would I do if I need to do it for multiple documents? I can of course write multiple queries but for effectiveness I was looking for some way to fetch them all in one, e.g.:
Only problem with that, is I'm not sure how I'd go about setting up the fields for each as the query above on the thread doesn't seem to work
Let me know if that clarifies, can retry in different terms if not
š
Essentially, let's imagine I have a document that references another document. When querying it we don't receive the actual document, but rather the reference of it. Instead, what I would need is the actual document dereferenced.
In other words if this is the schema:
export default { name: "footer", title: "Footer", type: "document", fields: [ { name: "title", title: "Title", type: "string", }, { name: "socials", title: "Socials", type: "array", of: [ { name: "social", title: "Social", type: "reference", to: { type: "social" }, }, ], }, ], };
footerdocument with the dereferenced
socialdocument I'd need to use this query:
*[_type == "footer"] { ..., socials[] { ..., social-> } }
*[_type in [ footer, otherDocument ]]
Let me know if that clarifies, can retry in different terms if not
š
Aug 16, 2022, 1:07 PM
Since you need to explicitly dereference you fields, I think it would be better to go about it this way:
That is, unless the fields you'll be dereferencing are exactly the same in each document type.
{ "footer": *[_type =='footer'] {...//whatever fields you need to dereference}, "otherDocument": *[_type == 'otherDocument']{...//whatever fields you need to dereference} }
Aug 16, 2022, 4:54 PM
A
Spot on! Wasn't aware of that syntax, that's very handy - Thank you!
Aug 16, 2022, 9:00 PM
A
Spot on! Wasn't aware of that syntax, that's very handy - Thank you!
Aug 16, 2022, 9: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.