connectrpc / connect-query-es

TypeScript-first expansion pack for TanStack Query that gives you Protobuf superpowers.
https://connectrpc.com/docs/web/query/getting-started
Apache License 2.0
237 stars 17 forks source link

How to use multiple transports effectively? #315

Closed malinskibeniamin closed 9 months ago

malinskibeniamin commented 10 months ago

Currently I am using 1 TransportProvider - is it possible to use more than 1 transport without overriding the previous provider? I would like to choose on a query/mutation basis whether to use a transport #1 or #2, what would be the cleanest way to achieve this?

paul-sachs commented 10 months ago

TransportProvider is using React context, so you can use multiple providers, and they will be the default providers used by queries made by components rendered underneath. All of the APIs that use transport also accept transport as an optional argument, so you can override the default.

const queryUsingTransport1 = useQuery(method, undefined, { transport: transport1 });
const mutationUsingTransport2 = useMutation(method, { transport: transport2 });
const infiniteQueryUsingDefaultTransport = useInfiniteQuery(method, { page: 0n }, {
  getNextPageParam: (lastPage) => lastPage.page + 1n,
  pageParamKey: "page"
});
paul-sachs commented 9 months ago

Closing unless there are any more concerns or questions about multi transport usage.