reactjssanitygroq

Unable to fetch tweets using groq/sanity


I'm doing a code-along for a twitter clone and am not able to get tweets. I'm using Sanity and can confirm the data is there, and is fetched w/o error on sanity client. I think the issue is with groq, I've uninstalled, reinstalled it, installed it as a standalone, and I'm still getting an empty array when console logging. Any Ideas?

Here is the getTweets below

import { groq } from 'next-sanity'
import type { NextApiRequest, NextApiResponse } from 'next'
import { sanityClient } from '../../sanity'
import { Tweet } from '../../typings'


const feedQuery = groq`
*[_type =='tweet' && !blockTweet]{_id,...} | 
order(_createdAt desc)`


type Data = {
  tweets: Tweet[]
}

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<Data>
) {
    const tweets: Tweet[] = await sanityClient.fetch(feedQuery);

  console.log(tweets)
  res.status(200).json({ tweets })
}


Solution

  • Do you have vision installed? You can run your query and see what comes back without relying on your frontend code. Sanity Vision

    The work incrementally in vision trying:

    *[_type =='tweet']
    

    Then to see if your conditional is correct:

    *[_type =='tweet' && !blockTweet]
    

    Then add the data after in brackets

        *[_type =='tweet' && !blockTweet] {
    _id,
    ...
    }
    

    Try this:

    const feedQuery = groq`
    *[_type == 'tweet']{
      _id,
      ...
    }| order(_createdAt desc)`