Connecting existing Gatsby project to Sanity: gatsby-source-sanity vs GROQ client
Gatsby project when I discovered Sanity.
I started a Sanity project from
scratch, as I wanted to play around and understand everything in detail first, which I did, it is fun, making a few content modelling.Now, I would like to make the link between the Sanity studio and Gatsby, enabling
watchmode for instance and all the good stuff in the same philosophy of understanding what I am doing and how it actually works.I have tried the
example-company-website-gatsby-sanity-combo : it's working beautifully but there is plenty of unwanted things that comes with it and I already have a existing Gatsby project that is specific.Same thing for the or the
portofolio , the portfolio-preview-poc , the startup-starter-kit or the sanity-template-gatsby-blog that comes with everything set (Netlify): very nice but already orientated sets.I would like to have more information to understand the choices that I can possibly make in that setting (what they mean for the future of the project)
So I think that what I want (e.g. a step-by-step information on that setup between existing Gatsby and existing Sanity) will be around the
README of the gatsby-source-sanity .Am I wrong?
With just focusing on this plugin, am I missing anything of the cool features somewhere?
If so what are the possibilities?
Is this plugin the most efficient choice?
Regarding this plugin, it seems that everything comes around the GraphQL API, which is what brought me to Sanity originally.
But now I understand that Sanity has historically build around GROQ, so I would like to know if it is possible to query in GROQ from Gatsby and how with this plugin.
It seems that to query with GROQ you must deal not with this plugin
but with the javascript client described here .Is that it?
I think that GROQ is more efficient than GraphQL at build time: am I wrong?
Thanks in advance for your precious help
⭐
gatsby help groq
Great question! You're on the right track thinking about gatsby-source-sanity as your main integration point. Let me break down your options and clarify the GROQ vs GraphQL situation with Gatsby.
Yes, gatsby-source-sanity is what you need
The gatsby-source-sanity plugin is indeed the standard way to connect your existing Gatsby project to your existing Sanity project. You're right to focus on its README - it gives you a clean, minimal setup without all the extra starter template code.
How gatsby-source-sanity actually works
Here's the key thing to understand: gatsby-source-sanity fetches data from Sanity and transforms it into Gatsby's GraphQL layer. The plugin makes only two API requests during the entire build process - it first fetches the GraphQL Schema Definitions from Sanity's deployed GraphQL API, then streams all accessible documents using the project's export endpoint.
So the flow is:
- Plugin fetches from Sanity's Content Lake
- Plugin transforms data into Gatsby nodes
- You query those nodes with GraphQL in your Gatsby pages/components
This is actually efficient - you get to query with GraphQL in your Gatsby components (which is the standard Gatsby pattern), and the plugin handles the Sanity integration efficiently.
Can you use GROQ directly in Gatsby?
Yes! You have two options:
Option 1: Use gatsby-source-sanity (recommended for most cases)
- Handles build-time data fetching automatically
- You query with GraphQL in your components
- Generates special
_rawfields for Portable Text data
Option 2: Use @sanity/client directly with GROQ
- Import the JavaScript client directly in your Gatsby files
- Write raw GROQ queries using
client.fetch - Useful for client-side queries or custom build logic
- Can be used alongside
gatsby-source-sanity
Example of using the client directly:
import sanityClient from '@sanity/client'
const client = sanityClient({
projectId: 'your-project-id',
dataset: 'production',
apiVersion: '2023-05-03',
useCdn: true
})
// In a Gatsby page or during build
const data = await client.fetch(`*[_type == "post"]{title, slug}`)Is GROQ more efficient than GraphQL at build time?
Both GROQ and GraphQL are query interfaces to the same underlying Content Lake, so the performance difference at build time is minimal. GROQ is Sanity's native query language and is more expressive for certain content transformations and projections, but for Gatsby builds, the efficiency comes from how gatsby-source-sanity fetches data (just two API requests total), not which query language you use.
The real advantage of GROQ is its expressiveness and the fact that it's designed specifically for Sanity's content structure. But when using gatsby-source-sanity, you're querying Gatsby's GraphQL layer (which is already optimized), not Sanity directly during component renders.
About watchMode and cool features
The gatsby-source-sanity plugin gives you:
watchMode: true: Sets up a listener that pushes changes from Sanity to Gatsby in real-time without additional API queries during developmentoverlayDrafts: true: Preview draft content in development while only showing published content in production (requires a read token)- Image optimization: Integration with Gatsby's image processing
_rawfields: Special fields for accessing raw Portable Text data with reference resolution
You're not missing anything by using just this plugin - it's the recommended approach and includes all the integration features you need.
My recommendation
- Start with
gatsby-source-sanityfollowing its README - Deploy your GraphQL API from your studio folder:
sanity graphql deploy - Configure
watchMode: truein development for live updates - Add
overlayDrafts: truewith a read token for draft previews - Use GraphQL in your Gatsby components (standard Gatsby pattern)
- Only reach for
@sanity/clientwith GROQ if you need client-side queries or custom build-time logic that doesn't fit the source plugin pattern
This gives you a clean, minimal setup between your existing projects while maintaining all the powerful features. You'll understand exactly what's happening without the extra template code you don't need.
Show original thread6 replies
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.