jasonkuhrt / graphql-request

Minimal GraphQL client
MIT License
5.77k stars 307 forks source link

Add support for NextJS revalidate/tags directly from request #861

Closed ghost closed 1 month ago

ghost commented 2 months ago

Perceived Problem

There is no option to pass next revalidate/tags when performing a request inside component

Ideas / Proposed Solution(s)

Something like this would be usefull: client.request(GetMenusQuery, { ...vars }, { headers: {}, next: { revalidate: 60, tags: ['menu'] })

jasonkuhrt commented 1 month ago

Docs https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#fetching-data-on-the-server-with-fetch.

Does anyone know if fetch being passed a request object supports this extended API? Did some code searches on GitHub but didn't immediately find where it is defined.

jasonkuhrt commented 1 month ago

In general this should be possible with the new extension system. If it is not, I would like to know why, e.g. is it because the call to fetch must be changed (Nextjs options passed as second parameter to it).

graffle.extend(async ({ exchange }) => {
    exchange.input.request.next = { revalidate: 60, tags: [`menu`] }
    return exchange()
  }).document({
    foo: { query: { id: true } },
    bar: { query: { idNonNull: true } },
  })

In this case I am mutating instead of spreading the Request object to create a copy. I have some doubts about that.