graphql-code-generator-community icon indicating copy to clipboard operation
graphql-code-generator-community copied to clipboard

fix(typescript-react-apollo): fix the type returned by useSuspenseQuery

Open nekocode opened this issue 1 year ago • 2 comments

This PR adds overload signatures to the generated useSuspenseQuery hooks to distinguish whether the parameter skipToken is passed. If the passed value is not skipToken, return non-nullable data. Example:

// @ts-ignore   <--- Force ignore "This overload signature is not compatible with its implementation signature"
export function useGetUserSuspenseQuery(
  baseOptions?: Apollo.SuspenseQueryHookOptions<
    Types.GetUserQuery,
    Types.GetUserQueryVariables
  >,
): Apollo.UseSuspenseQueryResult<
  Types.GetUserQuery, // Return non-nullable type
  Types.GetUserQueryVariables
>;

export function useGetUserSuspenseQuery(
  baseOptions?:
    | Apollo.SkipToken
    | Apollo.SuspenseQueryHookOptions<
        Types.GetUserQuery,
        Types.GetUserQueryVariables
      >,
): Apollo.UseSuspenseQueryResult<
  Types.GetUserQuery | undefined, // Otherwise return nullable type
  Types.GetUserQueryVariables
>;

export function useGetUserSuspenseQuery(
  baseOptions?:
    | Apollo.SkipToken
    | Apollo.SuspenseQueryHookOptions<
        Types.GetUserQuery,
        Types.GetUserQueryVariables
      >,
) {
  const options =
    baseOptions === Apollo.skipToken
      ? baseOptions
      : { ...defaultOptions, ...baseOptions };
  return Apollo.useSuspenseQuery<
    Types.GetUserQuery,
    Types.GetUserQueryVariables
  >(GetUserDocument, options);
}

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Related #838

Type of change

Please delete options that are not relevant.

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] This change requires a documentation update

Checklist:

  • [x] I have followed the CONTRIBUTING doc and the style guidelines of this project
  • [x] I have performed a self-review of my own code
  • [x] I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [x] My changes generate no new warnings
  • [x] I have added tests that prove my fix is effective or that my feature works
  • [x] New and existing unit tests pass locally with my changes
  • [ ] Any dependent changes have been merged and published in downstream modules

nekocode avatar Oct 26 '24 09:10 nekocode

🦋 Changeset detected

Latest commit: 959c5cfb1fa17da966d8c6831b76e3fddcf07b23

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@graphql-codegen/typescript-react-apollo Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Oct 26 '24 09:10 changeset-bot[bot]

definitively needed, thank you for this 🙏

eltonio450 avatar Nov 12 '24 15:11 eltonio450