Skip to content

feat(useSuspenseQuery): Add useSuspenseQuery generation #49

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

Merged
merged 2 commits into from
Apr 7, 2024

Conversation

seriouslag
Copy link
Collaborator

@seriouslag seriouslag commented Mar 23, 2024

The PR generates suspense query hooks. Addresses #45

Now instead of only having one generated index.ts, we now have 4 files.

  • common.ts - exports common types and variables for hooks.
  • queries.ts - exports the standard query hooks
  • suspense.ts - exports the new suspense query hooks
  • index.ts - exports all from common.ts and queries.ts - keeps the API as it is now

To use the standard queries (same as is is now):

import {
  usePetServiceFindPetsByStatus,
} from "../openapi/queries";

To use the new suspense queries:

import {
  usePetServiceFindPetsByStatusSuspense,
} from "../openapi/queries/suspense";

Full Example:

import { useDefaultClientFindPetsSuspense } from './openapi/queries/suspense';

function ChildComponent() {
  const { data } = useDefaultClientFindPetsSuspense({ tags: [], limit: 10 })

  return (
    <ul>
    {
      data?.map((pet, index) => (
         <li key={pet.id}>{pet.name}</li>
      ))
    }
    </ul>
  )
}

function ParentComponent() {
  return (
    <>
      <Suspense fallback={<>loading...</>}>
        <ChildComponent />
      </Suspense>
    </>
  );
}

@seriouslag seriouslag requested a review from 7nohe March 23, 2024 01:05
@7nohe
Copy link
Owner

7nohe commented Mar 30, 2024

The usage example should be simple. You just need to wrap the component that executesuseDefaultClientFindPetsSuspense with <Suspense>.

function ChildComponent() {
  const { data } = useDefaultClientFindPetsSuspense({ tags: [], limit: 10 })

  return (
    <ul>
    {
      data?.map((pet, index) => (
         <li key={pet.id}>{pet.name}</li>
      ))
    }
    </ul>
  )
}

function ParentComponent() {
  return (
    <>
      <Suspense fallback={<>loading...</>}>
        <ChildComponent />
      </Suspense>
    </>
  );
}

I agree with separating files related to Suspense queries from a maintainability perspective. I'd like to give it a try when I have some time.

@seriouslag
Copy link
Collaborator Author

seriouslag commented Apr 2, 2024

I have pushed a new commit which breaks up the file generation.

instead of only having one index.ts, we now have 4 files.

  • common.ts - exports common types and variables for hooks.
  • queries.ts - exports the standard query hooks
  • suspense.ts - exports the new suspense query hooks
  • index.ts - exports all from common.ts and queries.ts - keeps the API as it is now

suspense imports will have to imported through the suspense file ./openapi/queries/suspense

@7nohe

@seriouslag seriouslag requested a review from 7nohe April 2, 2024 19:33
@7nohe
Copy link
Owner

7nohe commented Apr 7, 2024

@seriouslag This is very cool. And it feels like after merging #50 , the next release could be published as v1. Thanks!

@7nohe 7nohe merged commit 4374671 into main Apr 7, 2024
@7nohe 7nohe deleted the feature/add-suspense-query-support branch April 7, 2024 15:57
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

Successfully merging this pull request may close these issues.

2 participants