Connecting Sanity with Gatsby and querying data in GraphQL.
34 replies
Last updated: May 21, 2021
A
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
http://localhost:8000/___graphql
May 21, 2021, 8:24 AM
P
Hi
user U
š Have you deployed your GraphQL API? To do so, you can run sanity graphql deployfrom the studio folder. Please let me know if you run into any trouble doing so š
May 21, 2021, 8:26 AM
A
I have done that
May 21, 2021, 8:27 AM
A
May 21, 2021, 8:28 AM
A
In gatsby
May 21, 2021, 8:28 AM
P
Indeed, it should show up afterwards. You may have to restart the development server and run
Also, if your dataset is private, make sure you have added a token with read rights to your source plugin configuration.
gatsby cleanto 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
A
That helped !
May 21, 2021, 8:34 AM
A
So any time schema changes , I need to re run studio ?
May 21, 2021, 8:34 AM
A
re-deploy?
May 21, 2021, 8:34 AM
P
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
A
Thanks a lot Peter.
GraphQL/gatsby/Sanity is new to me.
š
GraphQL/gatsby/Sanity is new to me.
š
May 21, 2021, 8:44 AM
P
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
P
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
A
Just one more questions, how can I query ALL data from a schema ?
May 21, 2021, 8:45 AM
A
I am so used to SQL
May 21, 2021, 8:45 AM
A
Select * from.. lol
May 21, 2021, 8:45 AM
A
I am doing
query MyQuery {
sanityTestimonial {
id
}
}
query MyQuery {
sanityTestimonial {
id
}
}
May 21, 2021, 8:46 AM
A
It's bringing up only the first one?
May 21, 2021, 8:46 AM
P
You could try querying
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
allSanityTestimonialinstead 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
A
How's GROQ used in a gatsby environment ?
May 21, 2021, 8:50 AM
P
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
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
A
That's awesome Peter, thanks for the help.I'll continue using gatsby-source-sanity for now
May 21, 2021, 8:52 AM
P
Sounds great (and makes sense) š
May 21, 2021, 8:54 AM
A
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
A
Otherwise I am getting the hang on it so far
May 21, 2021, 8:55 AM
P
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
Unless you are querying with a read token in Gatsby, you may not see drafts there.
.) - the
_idwill 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
P
Only documents on the root path (so without any dots) are public by default.
May 21, 2021, 9:29 AM
A
The all 3 are published
May 21, 2021, 10:09 AM
A
Actually are 3 are published
May 21, 2021, 10:09 AM
A
I am calling them here:
http://localhost:8000/___graphql
http://localhost:8000/___graphql
May 21, 2021, 10:10 AM
A
http://localhost:3333/vision on studio I get 3
May 21, 2021, 10:10 AM
P
Could you share the _id and _type of the three documents you're seeing in the studio?
May 21, 2021, 10:11 AM
A
"840d935c-aa5d-42de-9129-660e73b87ef9"
"c2a5fa5b-991c-45e4-b58d-cdec11b7c402"
"f7c68f37-7c60-4852-a40c-4f557ef953a3"
"c2a5fa5b-991c-45e4-b58d-cdec11b7c402"
"f7c68f37-7c60-4852-a40c-4f557ef953a3"
May 21, 2021, 10:12 AM
A
"testimonial" _type
May 21, 2021, 10:12 AM
P
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.