question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

typescript-react-query does not work during SSR with queryClient.prefetchQuery in Next

See original GitHub issue

Describe the bug

Following the example snippets in the docs and using SSR with Next, the prefetchQuery never makes a request.

Reading the docs one would need exposeQueryKeys and exposeFetcher. exposeFetcher even states: “For each generate query hook adds fetcher field with a corresponding GraphQL query using the fetcher. It is useful for queryClient.fetchQuery and queryClient.prefetchQuery.” And the provided sample await queryClient.prefetchQuery(userQuery.getKey(), () => userQuery.fetcher())

In setting up the SSR for Next, this doesn’t actually make any requests but instead populates a dehydrated state with a query hash and key and no data.

Screen Shot 2022-07-06 at 7 59 32 PM

Another interesting outcome is that arguments are ignored. So query.fetcher({ arg: value }) doesn’t do anything but a type error will be present if the query requires an argument.

Your Example Website or App

https://github.com/wsfuller/next-space-x

Steps to Reproduce the Bug or Issue

  1. Pull down provided repo
  2. yarn
  3. yarn dev
  4. localhost:3000

No data is pulled during SSR

Expected behavior

Would expect a pre-populated HTML document that accepts arguments when using the queryClient.prefetchQuery as per the documentation

Screenshots or Videos

Screen Shot 2022-07-06 at 7 59 32 PM Screen Shot 2022-07-07 at 7 48 28 AM

Platform

  • OS: macOS 12.4
  • NodeJS: 16.14.2
  • graphql version: 16.5.0
  • @graphql-codegen/* version(s): cli: 2.7.0, typescript: 2.6.0, typescript-graphql-request: 4.4.11, typescript-operations: 2.4.2, typescript-react-query: 3.5.15

Codegen Config File

overwrite: true
schema: "https://api.spacex.land/graphql/"
documents: "./src/graphql/**/*.gql"
generates:
  src/graphql/generated/index.ts:
    plugins:
      - typescript
      - typescript-operations
      - typescript-react-query
    config:
      pureMagicComment: true
      exposeQueryKeys: true
      fetcher: # note have tried setting this to graphql-request and continues to fail
        endpoint: "https://api.spacex.land/graphql/"
        fetchParams:
          headers:
            Content-Type: "application/json"
      exposeFetcher: true

Additional context

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

4reactions
wsfullercommented, Jul 12, 2022

Still looking into this but it seems like the exposeFetcher docs maybe incorrect.

Docs (not working): await queryClient.prefetchQuery(userQuery.getKey(), () => userQuery.fetcher())

Updated (working): await queryClient.prefetchQuery(userQuery.getKey(), userQuery.fetcher())

As far as using fetchQuery in place of prefetchQuery not seeing an issue. I’ve included a working repo in my original comment, I’m wondering if your error is stemming from something else. Changing the fetcher end point is a FetchError, so I’m curious if code generation didn’t run properly for the Type Error

0reactions
RedFourcommented, Oct 7, 2022

Updated (working): await queryClient.prefetchQuery(userQuery.getKey(), userQuery.fetcher())

Does anyone know the correct syntax for prefetching, when fetcher is set to graphql-request? The generated useMyCustomQuery hooks do not have a fetcher() method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Query SSR prefetchQuery not firing request
I'm using Next, React Query, and GraphQL Code Generator. Once everything is generated the SSR implementation using queryClient.prefetchQuery ...
Read more >
SSR | TanStack Query Docs
Create a new QueryClient instance for each page request. This ensures that data is not shared between users and requests. Prefetch the data...
Read more >
React Query v4 + SSR in Next JS - DEV Community ‍ ‍
SSR data fetching + caching mechanism is a bit tricky in next js. ... new QueryClient() // prefetch data on the server await...
Read more >
How to use GraphQL Code Generator with React Query - Nhost
Install the following npm packages required to run GraphQL Code Generator. These packages will help us: Generate types for TypeScript.
Read more >
NextJS and React-Query - Server Side fetching of data - SSR
In this quick tutorial you'll learn how to fetch data on the server with React-Query and NextJS. This is great as the data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found