Connecting existing Gatsby project to Sanity: gatsby-source-sanity vs GROQ client

6 replies
Last updated: Apr 25, 2020
🌝 Hi Sanity!I was into my new
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
AI Update

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:

  1. Plugin fetches from Sanity's Content Lake
  2. Plugin transforms data into Gatsby nodes
  3. 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)

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 development
  • overlayDrafts: 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
  • _raw fields: 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

  1. Start with gatsby-source-sanity following its README
  2. Deploy your GraphQL API from your studio folder: sanity graphql deploy
  3. Configure watchMode: true in development for live updates
  4. Add overlayDrafts: true with a read token for draft previews
  5. Use GraphQL in your Gatsby components (standard Gatsby pattern)
  6. Only reach for @sanity/client with 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 thread
6 replies
So, if I understand correctly, you're looking for something like this? https://dev.to/doylecodes/making-new-projects-with-gatsby-sanity-30nh

With just focusing on this plugin, am I missing anything of the cool features somewhere?
Nope, this plugin is pretty cool and the best way to integrate with Gatsby and Sanity. If you use it, you may also want to look at this: https://www.gatsbyjs.org/packages/gatsby-transform-portable-text/?=sanity

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.
GROQ is the main query language which also power our GraphQL API. However, the plugin will only fetch the GraphQL schema definition from our API to prevent missing fields in Gatsby, and the export endpoint to stream all the documents in one go to Gatsby‘s in-memory datatore. From there Gatsby have an internal API.
Although you can use any data-fetching method you like inside of
gatsby-node.js
 (or even client-side if you like), we recommend using Gatsby‘s GraphQL API, since that will give you most of the features like real-time preview etc. Else you might want to look at Next.js, which is less opinionated.

I think that GROQ is more efficient than GraphQL at build time: am I wrong?
Not in this case, since we use a pretty nifty method where we stream all your documents to Gatsby really fast.
So, if I understand correctly, you're looking for something like this? https://dev.to/doylecodes/making-new-projects-with-gatsby-sanity-30nh

With just focusing on this plugin, am I missing anything of the cool features somewhere?
Nope, this plugin is pretty cool and the best way to integrate with Gatsby and Sanity. If you use it, you may also want to look at this: https://www.gatsbyjs.org/packages/gatsby-transform-portable-text/?=sanity

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.
GROQ is the main query language which also power our GraphQL API. However, the plugin will only fetch the GraphQL schema definition from our API to prevent missing fields in Gatsby, and the export endpoint to stream all the documents in one go to Gatsby‘s in-memory datatore. From there Gatsby have an internal API.
Although you can use any data-fetching method you like inside of
gatsby-node.js
 (or even client-side if you like), we recommend using Gatsby‘s GraphQL API, since that will give you most of the features like real-time preview etc. Else you might want to look at Next.js, which is less opinionated.

I think that GROQ is more efficient than GraphQL at build time: am I wrong?
Not in this case, since we use a pretty nifty method where we stream all your documents to Gatsby really fast.
Thanks
user Y
!!
I knew the first article but not the second with portable text.
I will have a close look at it, because that is precisely what brought my attention to Sanity (your blog post taking distances with markdown, which touched something that I was not aware of but feeling without knowing it at the back of my head).

Ok so the
gatsby-source-sanity plugin and GraphQL are natural in this case otherwise it's more Next.js.I'll stay on Gatsby!

In the meantime, I have found also
this (I didn't know).To me, it is far too minimal because I am not able to get the detail of this
file .Where should it has to be put in `gatsby-config`for instance?

config is maid differently here with a
client-config.js file called from gatsby-config.js .
While I think there are doing roughly the same thing (but am I wrong?),
which of those config would you recommend?
(or where can I find a more detailed information?)

Also if I compare my actual existing folder structure in Gatsby and what I see in the 
portfolio-preview-poc , or the startup-starter-kit , I can see lots of differences.Anything that you would recommend to change?

A reflection that I have on another point: as Sanity is focused on content modelling, wouldn't it be natural to propose thru a plugin a natural exposition to Google structured data schema for instance?

I am planning to implement a collection of Events json-ld structured data, but I feel a little bit like reinventing the wheel (on the top of localization)
I am talking of the Event schema but the same thing applies to Article, BreadcrumbList, Person, Organization, Webpage... etc... etc...

Does Sanity has an opinion on that?
Some prefer to put the sanity config in its own file because they want to use the information several places, but it's perfectly OK to put it inside of
gatsby-config.js
 like it's said in the README.md .
Sorry I modified my post in the meantime!?
I beg your pardon.

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.

Was this answer helpful?