Next.js Discord

Discord Forum

Reuseable Query Not working, What am I doing wrong?

Answered
Thrianta posted this in #help-forum
Open in Discord
ThriantaOP
I have a query for fetching my list of blog post
I am wanting to move it into a util file so i can use it on multiple pages
Cannot get it to find the module

The query
const queryBlogList = groq*[_type=='blog'] { mainImage, author->, blogCategories[]->, publistedAt, snippet, title, slug, tripDate, } | order(_createdAt desc);

It works in the page but exporting it from another file is not working
Cant figure out what im going wrong, ive tried all AI solutions getting the same answer check the imports the import location is fine ive tried multiple ways of getting to it but nothing helps.
Answered by Thrianta
I have also tried a slightly different method using this fetch function
//sanity.fetch.ts
import 'server-only';

import type { QueryParams } from '@sanity/client';
import { draftMode } from 'next/headers';

import { client } from './sanity.client';

export const token = process.env.SANITY_API_READ_TOKEN;

const DEFAULT_PARAMS = {} as QueryParams;
const DEFAULT_TAGS = [] as string[];

export async function sanityFetch<QueryResponse>({
  query,
  params = DEFAULT_PARAMS,
  tags = DEFAULT_TAGS,
}: {
  query: string;
  params?: QueryParams;
  tags: string[];
}): Promise<QueryResponse> {
  const isDraftMode = draftMode().isEnabled;
  if (isDraftMode && !token) {
    throw new Error(
      'The `SANITY_API_READ_TOKEN` environment variable is required.',
    );
  }

  return client.fetch<QueryResponse>(query, params, {
    cache: 'force-cache',
    ...(isDraftMode && {
      cache: undefined,
      token: token,
      perspective: 'previewDrafts',
    }),
    next: {
      ...(isDraftMode && { revalidate: 30 }),
      tags,
    },
  });
}

I get the same results when the query is in the page file it works when its moved to the queries file it returns nothing to the page
View full answer

2 Replies

ThriantaOP
I have also tried a slightly different method using this fetch function
//sanity.fetch.ts
import 'server-only';

import type { QueryParams } from '@sanity/client';
import { draftMode } from 'next/headers';

import { client } from './sanity.client';

export const token = process.env.SANITY_API_READ_TOKEN;

const DEFAULT_PARAMS = {} as QueryParams;
const DEFAULT_TAGS = [] as string[];

export async function sanityFetch<QueryResponse>({
  query,
  params = DEFAULT_PARAMS,
  tags = DEFAULT_TAGS,
}: {
  query: string;
  params?: QueryParams;
  tags: string[];
}): Promise<QueryResponse> {
  const isDraftMode = draftMode().isEnabled;
  if (isDraftMode && !token) {
    throw new Error(
      'The `SANITY_API_READ_TOKEN` environment variable is required.',
    );
  }

  return client.fetch<QueryResponse>(query, params, {
    cache: 'force-cache',
    ...(isDraftMode && {
      cache: undefined,
      token: token,
      perspective: 'previewDrafts',
    }),
    next: {
      ...(isDraftMode && { revalidate: 30 }),
      tags,
    },
  });
}

I get the same results when the query is in the page file it works when its moved to the queries file it returns nothing to the page
Answer
ThriantaOP
routing it through the fetch function did work

 const blogs = await sanityFetch({ query: queryBlogList });


Cody messed up my imports on one of its attempts