Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript React Query v5 wrong type infinite query #541

Open
groomain opened this issue Dec 17, 2023 · 2 comments
Open

Typescript React Query v5 wrong type infinite query #541

groomain opened this issue Dec 17, 2023 · 2 comments

Comments

@groomain
Copy link

Which packages are impacted by your issue?

No response

Describe the bug

Hi,

I also have an issue with infinite query. I tried upgrading to the last version.

I think this type is wrong

export function useInfiniteQuery<
TQueryFnData,
TError = DefaultError,
TData = InfiniteData,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,

Is it the good type for TData @TkDodo @neil585456525 ?

The TData = InfiniteData, should it be only TQueryFnData ?
because with the version 6.0.0 all my infinite queries types in typescript does not match what is really returned.

I have a strange typescript error on what I should have on my result data. I should have an array

{
pageParams : ... [],
pages: {
pageParams : unknown[],
pages: result[]
}[]
}
but what I really have is not an array

{
pageParams : ...,
pages: result[]
}
This type is used for the result and it already use InfiniteData. So I thinks it should not be in the useInfiniteQuery type params

export interface InfiniteQueryObserverSuccessResult<
TData = unknown,
TError = unknown,

extends InfiniteQueryObserverBaseResult<TData, TError> {
data: InfiniteData
error: null
isError: false
isLoading: false
isLoadingError: false
isRefetchError: false
isSuccess: true
status: 'success'
}

Thanks for your help!

Your Example Website or App

https://codesandbox.io/p/devbox/wild-water-xrq5tp?file=%2Ftypes.ts%3A90%2C5

Steps to Reproduce the Bug or Issue

Go to types find useInfiniteUserQuery
Types is

 <
      TData = InfiniteData<UserQuery>,
      TError = unknown
    >
    ```

### Expected behavior

    But should be 
    
    ```
     <
      TData =UserQuery,
      TError = unknown
    >
    ```

### Screenshots or Videos

_No response_

### Platform

- OS: Mac 0S 14.0
- NodeJS: 20.10.0
"@graphql-codegen/add": "^5.0.0",
    "@graphql-codegen/cli": "^4.0.1",
    "@graphql-codegen/typescript": "^4.0.1",
    "@graphql-codegen/typescript-operations": "^4.0.1",
    "@graphql-codegen/typescript-react-query": "^6.0.0",


### Codegen Config File

import { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
  schema: "schema.graphql",
  documents: "document.graphql",
  generates: {
    "types.ts": {
      plugins: [
        "typescript",
        "typescript-operations",
        "typescript-react-query",
      ],
      config: {
        reactQueryVersion: 5,
        legacyMode: false,
        addInfiniteQuery: true,
      },
    },
  },
};

export default config;


### Additional context

_No response_
@neil585456525
Copy link
Contributor

neil585456525 commented Dec 29, 2023

Hi @groomain, can you give more details about the expected return?

{
pageParams : ... [],
pages: {
pageParams : unknown[],
pages: result[]
}[]
}

I'm confused by the double layer of page and pageParams.
Because ideally the return should be

{
pageParams : unknown[],
pages: result[]
}

What's the select option you have wrote for query?

@TkDodo
Copy link

TkDodo commented Dec 29, 2023

types on this level look correct to me:

TData = InfiniteData<UserQuery>,
TError = unknown

because they are forwarded to useInfiniteQuery as:

useInfiniteQuery<UserQuery, TError, TData>

and in react-query, our defaults are similar:

function useInfiniteQuery<
  TQueryFnData,
  TError = DefaultError,
  TData = InfiniteData<TQueryFnData>,
  TQueryKey extends QueryKey = QueryKey,
  TPageParam = unknown,
>

so TQueryFnData is basically UserQuery, and that makes TData of type InfiniteData<UserQuery>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants