Skip to content

Hydration API can not handle undefined values #1978

@iksent

Description

@iksent

Describe the bug
This issue is very similar to this one: #1661

To Reproduce

  1. Next.js + ReactQuery + GraphQL + TypeScript + GraphQL codegen
  2. My Code:

[pages/_app.tsx]

class MyApp extends App<AppProps> {
  queryClient: QueryClient | null = null

  constructor(props) {
    super(props)

    this.queryClient = new QueryClient({
      defaultOptions: {
        queries: {
          refetchOnWindowFocus: false,
          refetchOnMount: false,
          cacheTime: 1000 * 60 * 100,
          retryDelay: 1000 * 5,
          staleTime: 1000 * 60 * 10,
        },
      },
    })
  }

  static async getInitialProps(appContext: AppContext) {
    return await appGetInitialProps({
      appContext,
      redirectRoute: ROUTES.main.index,
      isPrivatePath: () => false,
    })
  }

  render() {
    const { pageProps, Component } = this.props
    console.log('pageProps', pageProps.dehydratedState)
    return (
      <WithProviders store={store} dialogVariants={dialogVariants}>
        <QueryClientProvider client={this.queryClient}>
          <Hydrate state={pageProps.dehydratedState}>
            <MainLayout>
              <Component {...pageProps} />
            </MainLayout>
          </Hydrate>
          <ReactQueryDevtools initialIsOpen={false} />
        </QueryClientProvider>
      </WithProviders>
    )
  }
}

[pages/index.tsx]

export const _getHomepageArticles = fetcher<
  GetHomepageArticlesQuery,
  GetHomepageArticlesQueryVariables
>(GetHomepageArticlesDocument)

export const useGetHomepageArticlesQuery = <
  TData = GetHomepageArticlesQuery,
  TError = unknown
>(
  variables?: GetHomepageArticlesQueryVariables,
  options?: UseQueryOptions<GetHomepageArticlesQuery, TError, TData>,
) =>
  useQuery<GetHomepageArticlesQuery, TError, TData>(
    ['getHomepageArticles', variables],
    _getHomepageArticles,
    options,
  )

const Index = () => {
  const { data, ...other } = useGetHomepageArticlesQuery()
  console.log('Index Page:', data, 'params:', other)
  return <IndexPage />
}

export const getServerSideProps: GetServerSideProps = async (context) => {
  const queryClient = new QueryClient()
  await queryClient.prefetchQuery('getHomepageArticles', _getHomepageArticles)
  console.log('Result Cache:', queryClient.getQueryCache())
  return {
    props: {
      dehydratedState: dehydrate(queryClient),
    },
  }
}

Expected behavior
useGetHomepageArticlesQuery should return filled in data param.

Current behavior
useGetHomepageArticlesQuery returns data = undefined and isFetching = true (data is being loaded at the client = preloaded state is being ignored).

Logs Screenshot

Снимок экрана 2021-03-16 в 14 21 37

Desktop:

  • OS: MacOS M1
  • Browser: Chrome
  • RQ Version: Tried latest and 3.5.13 from the issue above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions