apollographql / apollo-client-nextjs

Apollo Client support for the Next.js App Router
https://www.npmjs.com/package/@apollo/experimental-nextjs-app-support
MIT License
358 stars 25 forks source link

How to pass next fetch options to getClient.query()? #25

Closed paales closed 2 months ago

paales commented 12 months ago

For a general fetch call next.js supports fetch options:

fetch('http://localhost:3000/api/graphql', {
  cache: 'force-cache'
  next: {
    revalidate: 1000,
    tags: ['bla', 'bla2'],
  },
})

In the README fetchOptions: { cache: "no-store" }, is suggested as a possible solution to disable nextjs' cache, which works, but this is on a global level and I think this will disable the cache for the complete route (not sure yet).

We'd like to do something like:

const { data } = await getClient().query({ query: userQuery, revalidate: 1000, tags: ['bla', 'bla2'] });
// or
const { data } = await getClient().query({ query: userQuery, cache: 'no-store' });

Maybe this is already possible, but wasn't clear to me how this should work.

patrick91 commented 12 months ago

hi @paales, yes this should be already possible by doing the following:

  const { data } = await getClient().query({ 
    query,
    context: {
      fetchOptions: {
        next: { revalidate: 5 },
      },
    },
  });
paales commented 12 months ago

Ohh, thanks! That makes sense. I was confused because of the lack of typings in in the context.

phryneas commented 11 months ago

About the typings: unfortunately, those would need to be different for every link you are using - these are for HttpLink, but if you were using something else it could have very different config options. So we unfortunately can't type that here :/

paales commented 11 months ago

I'm trying to make sense how to combine the fetchPolicy and the fetch cache option. I've written it out, what do you think about this conclusion?

Cache configuration on queries:

Request dedupe:

fetchPolicy cache Request chain
cache-first, standby force-cache Apollo Cache -> ~React.cache~ -> fetch cache -> fetch
cache-first, standby no-store Apollo Cache -> ~React.cache~ -> fetch
network-only, no-cache force-cache React.cache -> fetch cache -> fetch
network-only, no-cache no-store React.cache -> fetch
cache-only ? Apollo Cache

The difference between cache-first and network-only is that we're leveraging the React.cache instead of the Apollo Cache. Since the Apollo Cache is superior to the React.cache, we should probably only use the Apollo Cache and thus only use cache-first.

I can't think of a possible cache-only use case in a RSC environment, ideas? This looks like the only-if-cached RequestCache property, but that isn't documented in the next.js docs, they probably didn't think it made sense.

My conclusion: Always have the fetchPolicy set to cache-first and use Next.js cache property to handle any any caching between requests.

paales commented 11 months ago

About the typings: unfortunately, those would need to be different for every link you are using - these are for HttpLink, but if you were using something else it could have very different config options. So we unfortunately can't type that here :/

We're creating a wrapper for the getClient().query() so we'll handle it there.

phryneas commented 2 months ago

I'm doing some housekeeping so I'm closing some older issues that haven't seen activity in a while. If this is still relevant, please feel free to reopen the issue.

github-actions[bot] commented 2 months ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.