Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

How to merge documents with GROQ in Sanity.io using the coalesce() function.

10 replies
Last updated: Oct 29, 2021
hey, anyone know if it’s possible to merge documents with GROQ? use case is merging separate locale documents onto the main/fallback locale, so for example if we had an EN and DE document any DE fields with values would be merged into the EN but anything missing from DE would just have the EN values
Oct 27, 2021, 8:11 PM
Hey User! You'll probably want to use the
coalesce()
GROQ function here. This will allow you to specify what's returned from GROQ if it comes across a null value. I can give more specific guidance if you share an example of the schema for these documents!
Oct 27, 2021, 8:29 PM
hey racheal, thanks!
to keep it simple, for these docs:

{ _id: 1, _type: "foo", lang: "en", title: "hello world", body: "something in english" }
{ _id: 2, _type: "foo", lang: "de", title: "hallo welt" }
{ _id: 3, _type: "foo", lang: "es", title: "hola mundo" }
i’d want the desired output for a query like
*[_type == "foo" && _id == 3 && lang == "es"]
to be:
{ _id: 3, lang: "es", title: "hola mundo", body: "something in english" }
Oct 28, 2021, 4:16 AM
Got it! In that case, the following should work for you:
*[_type == 'foo' && _id == 3 && lang == 'es']{
  _id,
  title,
  'body': coalesce(body, *[_type == 'foo' && _id == 1 && lang == 'en'].body)
}
Oct 28, 2021, 7:02 PM
ok, so every field would need its’ own subquery? that seems like it would be expensive if there were say a dozen fields… i suppose another way would be to just do a subquery for the whole doc and have the consumer merge in whatever fields have values
Oct 28, 2021, 9:02 PM
If they're combined into a single query with subqueries it will only be counted as a single query. You also shouldn't see a performance issue as GROQ can take a beating before lagging. That said, a common practice is to use something like the international input plugin to store translations within the same document. It may make it easier for you to handle missing fields on the frontend.
Oct 28, 2021, 9:12 PM
good to know thanks. i’ll take another look at liam’s plugin… any chance the sanity team will adopt/maintain that plugin or something similar in the future?
Oct 29, 2021, 6:43 PM
Ah, good question. I guess you could say there's a non-zero chance that could happen. It's a topic of internal discussion right now: when these plugins get created and adopted, how/when/who should be the one to maintain them? I'm sorry that's a bit vague, but a course of action hasn't been decided yet.
Oct 29, 2021, 6:52 PM
no worries, cant do everything all at once 🙂 glad to know the discussion is happening
Oct 29, 2021, 8:06 PM
thanks so much for all your help
Oct 29, 2021, 8:06 PM
Very happy to help!
Oct 29, 2021, 8:13 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.

Was this answer helpful?