🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Best practices for GROQ webhooks in Sanity.io and resolving an issue with delta::changedAny syntax.

9 replies
Last updated: Oct 8, 2021
Hi, I have two questions about GROQ webhooks:

changedAny syntaxLooks like the
docs here are off when you do
delta::changedAny(<more than one field>)
more. I had to do two paretheses, e.g
delta::changedAny((name, phone))
, not to get invalid GROQ.

Trigger on both update and created with changedAny So, what’s the best practice (if it exists any best practices yet :)) when registrering webhooks. I’m trying to make a webhook for a specific type of documents fire on both creation
and on update only when specific fields has changed. I’m trying
_type == "customer" && delta::changedAny((field1, field2, ...))
. This hook won’t fire on creation because if the
delta::changedAny
I guess.
So do you recommend registering two separate webhooks in this case? Or is there a way to say
“fire on both creation, and on update iff these fields changed”?
Oct 8, 2021, 7:15 PM
Hi Stian. Thanks for pointing out about the required extra parentheses. I’ll take a look.
For your second question, I expect one webhook would be better because it’s dry—unless it’s not possible. I’m just testing something now.
Oct 8, 2021, 7:26 PM
Yep, my thought as well:• One webhook is DRY
• And it’s easier to avoid redundant roundtrips and bytes over the wire #climatechange
🙂
Oct 8, 2021, 7:38 PM
Yep, my though as well:• One webhooks i DRY
• And it’s fewer bytes over the wire #climatechange
🙂
Oct 8, 2021, 7:38 PM
I don’t know if this is the best approach, but I figured you could use
before()._createdAt == null
to test for the creation use case.

_type == 'customer' && (delta::changedAny((name, phone)) || before()._createdAt == null)
Oct 8, 2021, 7:42 PM
Ah, of couse. Could also do
before() == null
, perhaps?
Oct 8, 2021, 7:46 PM
That was my initial plan in the projection:

{
  "created": before() == null,
}
Oct 8, 2021, 7:47 PM
Using just
before()
does seem to work as well.
Oct 8, 2021, 7:51 PM
Thanks for quick help! I think we’re trying to go for one webhook per document type then 🙂
Oct 8, 2021, 8:06 PM
You’re welcome! Just updating those guides to reflect your observation. Thanks again for pointing that out.
Oct 8, 2021, 8:07 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?