Error when trying to create a dynamic query using Gatsby + Sanity

17 replies
Last updated: Feb 4, 2022
Hello, I'm using Gatsby + Sanity and the Gatsby static GraphQL queries have been working great. I am now trying to create a dynamic query using
fetch
or
axios
but I'm getting the following error
"description": "expected '}' following object body",
returned with a 400 status code. I'm using the same query that was a static query before. I've even tried using an external tool and the Vision tool with the same results.
AI Update

Based on the error you're seeing, it looks like you're trying to use GraphQL queries directly with Sanity's API, but Sanity actually uses GROQ (Graph-Relational Object Queries) as its query language, not GraphQL.

Here's what's happening: While Gatsby's source plugin for Sanity (gatsby-source-sanity) automatically converts your Sanity schema into a GraphQL layer that Gatsby can use for static queries, when you try to fetch data directly from Sanity using fetch or axios, you need to use GROQ syntax instead.

The Solution

You need to rewrite your query in GROQ format. Here's how to make a direct API call to Sanity using the Query API:

const query = `*[_type == "post"]{
  _id,
  title,
  slug
}`

const projectId = 'your-project-id'
const dataset = 'production'
const apiVersion = 'v2021-06-07'

fetch(`https://${projectId}.api.sanity.io/${apiVersion}/data/query/${dataset}?query=${encodeURIComponent(query)}`)
  .then(res => res.json())
  .then(data => console.log(data.result))

Key Differences

GraphQL (what Gatsby uses):

{
  allSanityPost {
    nodes {
      id
      title
      slug
    }
  }
}

GROQ (what Sanity's API uses):

*[_type == "post"]{
  _id,
  title,
  slug
}

Instead of using fetch or axios directly, I'd recommend using the Sanity JavaScript client, which handles the API calls and encoding for you:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'production',
  useCdn: true,
  apiVersion: '2021-06-07'
})

client.fetch(`*[_type == "post"]{_id, title, slug}`)
  .then(posts => console.log(posts))

You can test your GROQ queries in the Vision plugin in your Sanity Studio to make sure they work before using them in your code. The "expected '}' following object body" error you're getting is because the Sanity API is trying to parse your GraphQL query as GROQ and failing at the syntax differences.

I'm guessing I'm just doing it wrong, but my URL appears correct and, like I said, my query is unchanged
The Vision plugin can only handle GROQ queries. For GraphQL queries, you can use the playground (if you request one when you deploy your GraphQL API) or in GraphiQL, which I believe will live on localhost:8000 when you run
npm run dev
on your front end.
OK, that makes sense. I still have the same problem in tools like Insomnia or even in my app
OK, that makes sense. I still have the same problem in tools like Insomnia or even in my app
From Insomnia
Is your URL in Insomnia something like
<https://PROJECT_ID.api.sanity.io/v2021-03-25/data/query/dev?query=>…
?
Is your URL in Insomnia something like
<https://PROJECT_ID.api.sanity.io/v2021-03-25/data/query/dev?query=>…
?
yes
yes
Is that a GROQ specific url?
It is. Behind the scenes, we actually convert GraphQL queries to GROQ.
In your terminal, if you run
sanity graphql deploy --playground
in your studio folder you should get a URL at the end of the output, like this (as long as everything succeeds):

GraphQL API deployed to:
<https://PROJECT_ID.api.sanity.io/v1/graphql/dev/default>
If you go to that URL, you should be able to try your queries.
ahhh OK, I will give it a shot
ahhh OK, I will give it a shot
Got it! Thanks a lot for the help
I also need to remove the edges/node stuff. Is that also GROQ-related?
I think that’s because you’re in GraphQL Playground , which lets you test queries a bit more easily. I’m fairly sure you’ll still need your nodes/edges when you’re testing in GraphiQL (on your front end) and writing your queries in Gatsby.
OK, gotcha. Thanks again!

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?