πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Connecting Sanity with Gatsby and querying data in GraphQL.

34 replies
Last updated: May 21, 2021
Hi everyone, can anyone help me with connecting Sanity <-> Gatsby ? I have the gatsby-source-sanity plugin installed and configured but the schema is not showing up in

http://localhost:8000/___graphql
May 21, 2021, 8:24 AM
Hi
user U
πŸ‘‹ Have you deployed your GraphQL API? To do so, you can run
sanity graphql deploy
from the studio folder. Please let me know if you run into any trouble doing so πŸ™‚
May 21, 2021, 8:26 AM
I have done that
May 21, 2021, 8:27 AM
Once that is done
I should be able to see my data in


http://localhost:8000/___graphql correct ?
May 21, 2021, 8:28 AM
In gatsby
May 21, 2021, 8:28 AM
Indeed, it should show up afterwards. You may have to restart the development server and run
gatsby clean
to make sure there's no cache getting in the way.
Also, if your dataset is private, make sure you have added a token with read rights to your source plugin configuration.
May 21, 2021, 8:32 AM
That helped !
May 21, 2021, 8:34 AM
So any time schema changes , I need to re run studio ?
May 21, 2021, 8:34 AM
re-deploy?
May 21, 2021, 8:34 AM
That's correct: every time you update your schema (not the content), you'll have to re-deploy the GraphQL API for the changes to be reflected on the Gatsby side as well.
May 21, 2021, 8:43 AM
Thanks a lot Peter.
GraphQL/gatsby/Sanity is new to me.
πŸ™‚
May 21, 2021, 8:44 AM
It's quite a lot to get your head around at the beginning, but that's why we're here for any questions πŸ™‚ Looking forward to seeing what you'll build!
May 21, 2021, 8:45 AM
It's quite a lot to get your head around at the beginning, but that's why we're here for any questions πŸ™‚ Looking forward to seeing what you'll build!
May 21, 2021, 8:45 AM
Just one more questions, how can I query ALL data from a schema ?
May 21, 2021, 8:45 AM
I am so used to SQL
May 21, 2021, 8:45 AM
Select * from.. lol
May 21, 2021, 8:45 AM
I am doing
query MyQuery {
sanityTestimonial {
id
}
}
May 21, 2021, 8:46 AM
It's bringing up only the first one?
May 21, 2021, 8:46 AM
You could try querying
allSanityTestimonial
instead to get all of them.
Admittedly, this is easier using Sanity's own open-source query language (GROQ), but people tend to still use GraphQL with Gatsby. In GROQ, you could get
all data with
*
, or a specific document type's data with
*[_type == "testimonial"]
. Just sharing this for context, although it won't be relevant for your project at this point: https://www.sanity.io/docs/what-about-graphql πŸ™‚
May 21, 2021, 8:49 AM
How's GROQ used in a gatsby environment ?
May 21, 2021, 8:50 AM
It's generally not used with Gatsby, as GraphQL is the default there. If you're getting started, you may want to stick to GraphQL as it'll be easier to find specific answers for Gatsby.
However, if you're feeling adventurous, here's a plugin to use GROQ with Gatsby:
https://github.com/kmcaloon/gatsby-plugin-groq
May 21, 2021, 8:52 AM
That's awesome Peter, thanks for the help.I'll continue using gatsby-source-sanity for now
May 21, 2021, 8:52 AM
Sounds great (and makes sense) πŸ™‚
May 21, 2021, 8:54 AM
One last question, I have 3 items, on sanity GROQ the result is 3 but on graphQL endpoint I am getting only 2 ? Despite using no filters
May 21, 2021, 8:55 AM
Otherwise I am getting the hang on it so far
May 21, 2021, 8:55 AM
Any chance you are querying from inside the studio or from the CLI with GROQ? If so, you might be getting both the draft and the published version of the same document in one case. You can identify the draft by its ID on a so-called non-root path (a path with a
.
) - the
_id
will be formatted like
drafts.<id>
in that case.
Unless you are querying with a read token in Gatsby, you may not see drafts there.
May 21, 2021, 9:28 AM
Only documents on the root path (so without any dots) are public by default.
May 21, 2021, 9:29 AM
The all 3 are published
May 21, 2021, 10:09 AM
Actually are 3 are published
May 21, 2021, 10:09 AM
I am calling them here:
http://localhost:8000/___graphql
May 21, 2021, 10:10 AM
May 21, 2021, 10:10 AM
Could you share the _id and _type of the three documents you're seeing in the studio?
May 21, 2021, 10:11 AM
"840d935c-aa5d-42de-9129-660e73b87ef9"
"c2a5fa5b-991c-45e4-b58d-cdec11b7c402"

"f7c68f37-7c60-4852-a40c-4f557ef953a3"
May 21, 2021, 10:12 AM
"testimonial" _type
May 21, 2021, 10:12 AM
Considering they are published and of the same type, they should indeed show up in GraphiQL as well. Could you try another restart with
gatsby clean
? This shouldn't be necessarily normally but it's to rule out some issues.
May 21, 2021, 10:32 AM

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?