Discussion on using GROQ vs GraphQL for Sanity.io, with GROQ being recommended for its capabilities.
18 replies
Last updated: Apr 12, 2021
E
Hey all, happy Friday đ Just looking into Sanity and wondering: would you consider GraphQL to have first-class support? Or will the experience generally be smoother using GROQ? It's not a dealbreak either way.
Apr 9, 2021, 8:55 PM
M
GROQ is more capable than GraphQL in any setting, so I would definitely recommend using that over gql.
Apr 9, 2021, 11:27 PM
E
In that it's more sanity-aware? I haven't played with it yet
Apr 9, 2021, 11:28 PM
M
Install the vision plugin in the studio and you can try it out (I only wish there was autocompletion). You can use some pretty good filtering with GROQ.
You can have a play around on this site as well:
https://groq.dev/
You can have a play around on this site as well:
https://groq.dev/
Apr 9, 2021, 11:33 PM
M
And thereâs some pretty good docs here: https://www.sanity.io/docs/groq
Apr 9, 2021, 11:33 PM
E
Okay this is really helpful. Looking at the "Complex" variant at groq.dev, I can see what you're saying about its power. Just need to weigh whether that justifies eschewing all the tooling that comes with GraphQL (Typescript, React, etc)
Apr 9, 2021, 11:36 PM
M
Itâs definitely more capable for Sanity than GraphQL. But even in general I find that it has the edge on some things, itâs more the tooling around it that gql takes. You can perform more operations on your queries with GROQ than gql. Unlike gql GROQ has a spread operator to grab all the fields (though it doesnât resolve references).
Apr 9, 2021, 11:37 PM
M
Yeah for sure, gql has a lot more tooling support. Especially in the sense that it builds an API schema which offers up nice documentation, autocompletion and type generation.
Apr 9, 2021, 11:38 PM
E
Lots of good food for thought, thanks!
Apr 9, 2021, 11:40 PM
M
Using GraphQL with Sanity still works fine. The Gatsby plugin uses GraphQL.
Apr 9, 2021, 11:40 PM
M
No worries!
Apr 9, 2021, 11:40 PM
G
I only wish there was autocompletion.
Ctrl-<space>should work in Vision, albeit it doesnât appear to be context-aware.
Apr 10, 2021, 3:20 PM
M
I've tried that out of habit a few times and it doesn't seem to do anything for me đ¤ˇââď¸
Apr 10, 2021, 3:25 PM
G
Any chance youâre overriding that keybinding with TextExpander or something similar?
Apr 10, 2021, 3:28 PM
M
I don't even know what that is đ
but it works in GraphiQL so I don't think so.
Apr 10, 2021, 3:29 PM
M
I'll give it another go next week when I work in the studio again
Apr 10, 2021, 3:30 PM
G
Eliot: I personally love GROQ. It just worksâand works really well with Sanity. You donât need to define a schema and can just query any JSON data with it. You can play around with the GROQ Arcade at https://groq.dev/ . Espen gave a recorded talk a few years ago that helps distinguish GROQ a bit. About 4 minutes in he points out exactly what Max said about autocompletion. There are also a few notes about why Sanity uses GROQ here .
As GROQ is a Sanity creation, the docs tend to be written using GROQ queries (outside of the docs that are specific to the
GraphQL API ).
There are plenty of people in this community using both GROQ and GraphQL so I think youâll get great support either way.
As GROQ is a Sanity creation, the docs tend to be written using GROQ queries (outside of the docs that are specific to the
GraphQL API ).
There are plenty of people in this community using both GROQ and GraphQL so I think youâll get great support either way.
Apr 10, 2021, 3:41 PM
E
Great info thanks! To me, defining a schema is an advantage from a code stability perspective. But I come from a non-JS background đ
Apr 12, 2021, 1:56 PM
G
Understandable, and GROQ wonât mind at all (and as you may have already seen in your research, when using Sanity a schema will be a requirement).
Apr 12, 2021, 2:36 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.