Skip to content

Conversation

@jtomaszewski
Copy link
Contributor

@jtomaszewski jtomaszewski commented Jan 9, 2026

Summary

  • Add createSuspenseQueryHook factory function that creates a typed hook with suspense: true enabled by default
  • The returned data is always defined (non-optional), since React Suspense guarantees data is available before the component renders
  • Config type omits suspense option (always enabled)

New exports

  • createSuspenseQueryHook - factory function for creating suspense hooks
  • SuspenseSWRResponse - response type with non-optional data
  • SuspenseSWRConfig - config type with suspense option omitted

Usage

import { createSuspenseQueryHook } from 'swr-openapi';

const useSuspenseQuery = createSuspenseQueryHook(client, "api");

// data is Pet[], not Pet[] | undefined
const { data } = useSuspenseQuery("/pets");

jtomaszewski and others added 2 commits January 9, 2026 14:51
Add `createSuspenseQueryHook` that creates a typed hook with `suspense: true`
enabled by default. The key difference from `createQueryHook` is that the
returned `data` is always defined (non-optional), since suspense guarantees
data is available before the component renders.

New exports:
- `createSuspenseQueryHook` - factory function for creating suspense hooks
- `SuspenseSWRResponse` - response type with non-optional `data`
- `SuspenseSWRConfig` - config type with `suspense` option omitted

Usage:
```ts
const useSuspenseQuery = createSuspenseQueryHook(client, "api");
const { data } = useSuspenseQuery("/pets"); // data is Pet[], not Pet[] | undefined
```

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Update hook-builders.md with createSuspenseQueryHook export and usage
- Create use-suspense-query.md page documenting the suspense hook
- Add useSuspenseQuery to VitePress sidebar navigation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@htunnicliff
Copy link
Owner

htunnicliff commented Jan 12, 2026

It looks like the types for suspense weren't working because the SWR configuration type was not being passed down to useSWR 🤦‍♂️

I was able to remedy that in #42 and release that fix in v5.6.0.

Want to give that a try?

@jtomaszewski
Copy link
Contributor Author

It helps, yes.

Although, I'd still be up for merging this. In my app example, we use suspense: true nearly everywhere. Even with that fix, we still need to write useQuery(..., ..., { suspense: true }) everywhere, which may feel boilerplate'y a bit, especially with simple requests such as useQuery("/posts", {}, { suspense: true }), which could be simplified to useSuspenseQuery("/posts") instead, or maybe just useQuery("/posts") if somebody would want to force suspense globally for their app.

Unfortunately, building that useSuspenseQuery wrapper on top of useQuery currently is difficult from the type definition side, as seen below:

image

That's minimal code required for that, and because of its' complexity, I can't even tell yet whether that actually works. (Before making this PR, I had something like that too, and I think it had some bug.)

Having it in this library (with it being covered by tests ;) ), would help me quite a bit.

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