question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make `useQuery` with `suspense: true` to return non-nullable `data`

See original GitHub issue

Describe the feature you’d like to request

{ suspense: true } should let us use data without checking its integrity.

We can use suspense: true in SSR now, see #3130

Describe the solution you’d like to see

Given

const postQuery = trpc.post.byId.useQuery({ id }, { suspense: true });

postQuery.data should be not be nullable.

Considerations: if enabled: true, we should not infer it as non-nullable

Desribe alternate solutions

  • Default options to set suspense: true
  • Add a useSuspenseQuery / useSuspenseInfiniteQuery

Another approach would be to enable this behavior globally when creating the react-query hooks.

export trpc = createTRPCReact<Context>({suspense: true})

Additional information

  • enabled: true/false should still work
  • We need to narrow the generic so we still can’t pass extra params to opts

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
KATTcommented, Nov 7, 2022

I definitely want this ASAP.

I have started using suspense in my app now as server-side suspense now works.

2reactions
iway1commented, Nov 6, 2022

Splitting the queries into different components

@KATT I don’t think this is a viable solution in practice because (1) that’s going to be more work for the developer and (2) would make it impossible to create certain types of user interfaces.

I guess (2) is more important. The issue is that we’d be forced to create multiple <Suspense/> components each with their own fallback and wouldn’t be able to create a single fallback with multiple queries.

It’s not uncommon to have some piece of UI wait for data from multiple requests before showing that piece of UI, and to show the same common fallback when either of the queries are loading (which means we’re forced to have each query within the same suspense component).

In such situations, we either don’t use suspense or we have a request waterfall. I think mix and matching suspense: true and suspense: false is also probably bad practice because of lack of consistency within a codebase.

So, yeah, suspense: true simply doesn’t cover all use cases.

Adding support for useQueries()

This would be great! This would solve the problem. I would probably switch to <Suspense/> if we had useQueries, personally. And if we could figure out the typescript magic to have it return non-nullable data as well that would really great.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding React suspense + data fetching support #9627 - GitHub
I really hope you at Apollo figure out how to make Suspense work with data. ... const { data, error } = useQuery(query,...
Read more >
Using Suspense and React Query: Tutorial with examples
In this article, we'll explore using React Query alongside Suspense to build a simple recipe app that will fetch recipe data from an...
Read more >
Fetching data with React - GQty
This hook returns the core query object, and it will detect the usage of it, and suspend ... import { Suspense } from...
Read more >
Suspense | TanStack Query Docs
NOTE: Suspense mode for React Query is experimental, same as Suspense for data fetching itself. These APIs WILL change and should not be...
Read more >
Vue Bindings | urql Documentation
We see that useQuery accepts options and returns a result object. In this case we've set the query option to our GraphQL query....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found