- Notifications
You must be signed in to change notification settings - Fork22
TypeScript-first expansion pack for TanStack Query that gives you Protobuf superpowers.
License
connectrpc/connect-query-es
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Connect-Query is an wrapper aroundTanStack Query (react-query), written in TypeScript and thoroughly tested. It enables effortless communication with servers that speak theConnect Protocol.
- Quickstart
- Connect-Query API
TransportProvideruseTransportuseQueryuseSuspenseQueryuseInfiniteQueryuseSuspenseInfiniteQueryuseMutationcreateConnectQueryKeycallUnaryMethodcreateProtobufSafeUpdatercreateQueryOptionscreateInfiniteQueryOptionsaddStaticKeyToTransportConnectQueryKeyQueryOptionsQueryOptionsWithSkipTokenInfiniteQueryOptionsInfiniteQueryOptionsWithSkipToken
npm install @connectrpc/connect-query @connectrpc/connect-web
Tip
If you are using something that doesn't automatically install peerDependencies (npm older than v7), you'll want to make sure you also have@bufbuild/protobuf,@connectrpc/connect, and@tanstack/react-query installed.@connectrpc/connect-web is required for definingthe transport to be used by the client.
Connect-Query will immediately feel familiar to you if you've used TanStack Query. It provides a similar API, but instead takes a definition for your endpoint and returns a typesafe API for that endpoint.
First, make sure you've configured your provider and query client:
import{createConnectTransport}from"@connectrpc/connect-web";import{TransportProvider}from"@connectrpc/connect-query";import{QueryClient,QueryClientProvider}from"@tanstack/react-query";constfinalTransport=createConnectTransport({baseUrl:"https://demo.connectrpc.com",});constqueryClient=newQueryClient();functionApp(){return(<TransportProvidertransport={finalTransport}><QueryClientProviderclient={queryClient}><YourApp/></QueryClientProvider></TransportProvider>);}
With configuration completed, you can now use theuseQuery hook to make a request:
import{useQuery}from'@connectrpc/connect-query';import{say}from'your-generated-code/eliza-ElizaService_connectquery';exportconstExample:FC=()=>{const{ data}=useQuery(say,{sentence:"Hello"});return<div>{data}</div>;};
That's it!
The code generator does all the work of turning your Protobuf file into something you can easily import. TypeScript types all populate out-of-the-box. Your documentation is also converted toTSDoc.
One of the best features of this library is that once you write your schema in Protobuf form, the TypeScript types are generated and then inferred. You never again need to specify the types of your data since the library does it automatically.
To make a query, you need a schema for a remote procedure call (RPC). A typed schema can be generated withprotoc-gen-es. It generates an export for every service:
/** *@generated from service connectrpc.eliza.v1.ElizaService */exportdeclareconstElizaService:GenService<{/** * Say is a unary RPC. Eliza responds to the prompt with a single sentence. * *@generated from rpc connectrpc.eliza.v1.ElizaService.Say */say:{methodKind:"unary";input:typeofSayRequestSchema;output:typeofSayResponseSchema;};}>;
protoc-gen-connect-query is an optional additional plugin that exports every RPC individually for convenience:
import{ElizaService}from"./eliza_pb";/** * Say is a unary RPC. Eliza responds to the prompt with a single sentence. * *@generated from rpc connectrpc.eliza.v1.ElizaService.Say */exportconstsay:(typeofElizaService)["method"]["say"];
For more information on code generation, see thedocumentation forprotoc-gen-connect-query and thedocumentation forprotoc-gen-es.
constTransportProvider:FC<PropsWithChildren<{transport:Transport;}>>;
TransportProvider is the main mechanism by which Connect-Query keeps track of theTransport used by your application.
Broadly speaking, "transport" joins two concepts:
- The protocol of communication. For this there are two options: theConnect Protocol, or thegRPC-Web Protocol.
- The protocol options. The primary important piece of information here is the
baseUrl, but there are also other potentially critical options like request credentials, wire serialization options, or protocol-specific options like Connect's support forHTTP GET.
With these two pieces of information in hand, the transport provides the critical mechanism by which your app can make network requests.
To learn more about the two modes of transport, take a look at the Connect-Web documentation onchoosing a protocol.
To get started with Connect-Query, simply import a transport (eithercreateConnectTransport orcreateGrpcWebTransport from@connectrpc/connect-web) and pass it to the provider.
A common use case for the transport is to add headers to requests (like auth tokens, etc). You can do this with a custominterceptor.
import{QueryClient,QueryClientProvider}from"@tanstack/react-query";import{TransportProvider}from"@connectrpc/connect-query";constqueryClient=newQueryClient();exportconstApp=()=>{consttransport=createConnectTransport({baseUrl:"<your baseUrl here>",interceptors:[(next)=>(request)=>{request.header.append("some-new-header","some-value");// Add your headers herereturnnext(request);},],});return(<TransportProvidertransport={transport}><QueryClientProviderclient={queryClient}><YourApp/></QueryClientProvider></TransportProvider>);};
For more details about what you can do with the transport, see theConnect-Web documentation.
constuseTransport:()=>Transport;
Use this helper to get the default transport that's currently attached to the React context for the calling component.
Tip
All hooks accept atransport in the options. You can use the Transport from the context, or create one dynamically. If you create a Transport dynamically, make sure to memoize it, because it is taken into consideration when building query keys.
functionuseQuery<IextendsDescMessage,OextendsDescMessage,SelectOutData=MessageShape<O>,>(schema:DescMethodUnary<I,O>,input?:SkipToken|MessageInitShape<I>,{ transport, ...queryOptions}:UseQueryOptions<I,O,SelectOutData>={},):UseQueryResult<SelectOutData,ConnectError>;
TheuseQuery hook is the primary way to make a unary request. It's a wrapper around TanStack Query'suseQuery hook, but it's preconfigured with the correctqueryKey andqueryFn for the given method.
Any additionaloptions you pass touseQuery will be merged with the options that Connect-Query provides to @tanstack/react-query. This means that you can pass any additional options that TanStack Query supports.
Identical to useQuery but mapping to theuseSuspenseQuery hook fromTanStack Query. This includes the benefits of narrowing the resulting data type (data will never be undefined).
functionuseInfiniteQuery<IextendsDescMessage,OextendsDescMessage,ParamKeyextendskeyofMessageInitShape<I>,>(schema:DescMethodUnary<I,O>,input:|SkipToken|(MessageInitShape<I>&Required<Pick<MessageInitShape<I>,ParamKey>>),{ transport, pageParamKey, getNextPageParam, ...queryOptions}:UseInfiniteQueryOptions<I,O,ParamKey>,):UseInfiniteQueryResult<InfiniteData<MessageShape<O>>,ConnectError>;
TheuseInfiniteQuery is a wrapper around TanStack Query'suseInfiniteQuery hook, but it's preconfigured with the correctqueryKey andqueryFn for the given method.
There are some required options foruseInfiniteQuery, primarilypageParamKey andgetNextPageParam. These are required because Connect-Query doesn't know how to paginate your data. You must provide a mapping from the output of the previous page and getting the next page. All other options passed touseInfiniteQuery will be merged with the options that Connect-Query provides to @tanstack/react-query. This means that you can pass any additional options that TanStack Query supports.
Identical to useInfiniteQuery but mapping to theuseSuspenseInfiniteQuery hook fromTanStack Query. This includes the benefits of narrowing the resulting data type (data will never be undefined).
functionuseMutation<IextendsDescMessage,OextendsDescMessage>(schema:DescMethodUnary<I,O>,{ transport, ...queryOptions}:UseMutationOptions<I,O,Ctx>={},):UseMutationResult<MessageShape<O>,ConnectError,PartialMessage<I>>;
TheuseMutation is a wrapper around TanStack Query'suseMutation hook, but it's preconfigured with the correctmutationFn for the given method.
Any additionaloptions you pass touseMutation will be merged with the options that Connect-Query provides to @tanstack/react-query. This means that you can pass any additional options that TanStack Query supports.
This function is used under the hood ofuseQuery and other hooks to compute aqueryKey for TanStack Query. You can use it to create keys yourself to filter queries.
useQuery creates a query key with the following parameters:
- The qualified name of the RPC.
- The transport being used.
- The request message.
- The cardinality of the RPC (either "finite" or "infinite").
- Adds a DataTag which brands the key with the associated data type of the response.
The DataTag type allows @tanstack/react-query functions to properly infer the type of the data returned by the query. This is useful for things likeQueryClient.setQueryData andQueryClient.getQueryData.
To create the same key manually, you simply provide the same parameters:
import{createConnectQueryKey,useTransport}from"@connectrpc/connect-query";import{ElizaService}from"./gen/eliza_pb";constmyTransport=useTransport();constqueryKey=createConnectQueryKey({schema:ElizaService.method.say,transport:myTransport,// You can provide a partial message here.input:{sentence:"hello"},// This defines what kind of request it is (either for an infinite or finite query).cardinality:"finite",});// queryKey:["connect-query",{transport:"t1",serviceName:"connectrpc.eliza.v1.ElizaService",methodName:"Say",input:{sentence:"hello"},cardinality:"finite",},];
You can create a partial key that matches all RPCs of a service:
import{createConnectQueryKey}from"@connectrpc/connect-query";import{ElizaService}from"./gen/eliza_pb";constqueryKey=createConnectQueryKey({schema:ElizaService,cardinality:"finite",});// queryKey:["connect-query",{serviceName:"connectrpc.eliza.v1.ElizaService",cardinality:"finite",},];
Infinite queries have distinct keys. To create a key for an infinite query, use the parametercardinality:
import{createConnectQueryKey}from"@connectrpc/connect-query";import{ListService}from"./gen/list_pb";// The hook useInfiniteQuery() creates a query key with cardinality: "infinite",// and passes on the pageParamKey.constqueryKey=createConnectQueryKey({schema:ListService.method.list,cardinality:"infinite",pageParamKey:"page",input:{preview:true},});
functioncallUnaryMethod<IextendsDescMessage,OextendsDescMessage>(transport:Transport,schema:DescMethodUnary<I,O>,input:MessageInitShape<I>|undefined,options?:{signal?:AbortSignal;},):Promise<O>;
This API allows you to directly call the method using the provided transport. Use this if you need to manually call a method outside of the context of a React component, or need to call it where you can't use hooks.
Creates a typesafe updater that can be used to update data in a query cache. Used in combination with a queryClient.
import{createProtobufSafeUpdater,useTransport}from'@connectrpc/connect-query';import{useQueryClient}from"@tanstack/react-query";...constqueryClient=useQueryClient();consttransport=useTransport();queryClient.setQueryData(createConnectQueryKey({schema:example, transport,input:{},cardinality:"finite",}),createProtobufSafeUpdater(example,(prev)=>{if(prev===undefined){returnundefined;}return{ ...prev,completed:true,};}));
** Note: This API is deprecated and will be removed in a future version.ConnectQueryKey now contains type information to make it safer to usesetQueryData directly. **
functioncreateQueryOptions<IextendsDescMessage,OextendsDescMessage>(schema:DescMethodUnary<I,O>,input:SkipToken|PartialMessage<I>|undefined,{ transport,}:{transport:Transport;},):{queryKey:ConnectQueryKey;queryFn:QueryFunction<MessageShape<O>,ConnectQueryKey>|SkipToken;structuralSharing:(oldData:unknown,newData:unknown)=>unknown;};
A functional version of the options that can be passed to theuseQuery hook from@tanstack/react-query. When called, it will return the appropriatequeryKey,queryFn, andstructuralSharing flag. This is useful when interacting withuseQueries API or queryClient methods (likeensureQueryData, etc).
An example of how to use this function withuseQueries:
import{useQueries}from"@tanstack/react-query";import{createQueryOptions,useTransport}from"@connectrpc/connect-query";import{example}from"your-generated-code/example-ExampleService_connectquery";constMyComponent=()=>{consttransport=useTransport();const[query1,query2]=useQueries([createQueryOptions(example,{sentence:"First query"},{ transport}),createQueryOptions(example,{sentence:"Second query"},{ transport}),]); ...};
functioncreateInfiniteQueryOptions<IextendsDescMessage,OextendsDescMessage,ParamKeyextendskeyofMessageInitShape<I>,>(schema:DescMethodUnary<I,O>,input:|SkipToken|(MessageInitShape<I>&Required<Pick<MessageInitShape<I>,ParamKey>>),{ transport, getNextPageParam, pageParamKey,}:ConnectInfiniteQueryOptions<I,O,ParamKey>,):{getNextPageParam:ConnectInfiniteQueryOptions<I,O,ParamKey>["getNextPageParam"];queryKey:ConnectInfiniteQueryKey<I>;queryFn:|QueryFunction<MessageShape<O>,ConnectInfiniteQueryKey<I>,MessageInitShape<I>[ParamKey]>|SkipToken;structuralSharing:(oldData:unknown,newData:unknown)=>unknown;initialPageParam:PartialMessage<I>[ParamKey];};
A functional version of the options that can be passed to theuseInfiniteQuery hook from@tanstack/react-query.When called, it will return the appropriatequeryKey,queryFn, andstructuralSharing flags, as well as a few other parameters required foruseInfiniteQuery. This is useful when interacting with some queryClient methods (likeensureQueryData, etc).
Transports are taken into consideration when building query keys for associated queries. This can cause issues with SSR since the transport on the server is not the same transport that gets executed on the client (cannot be tracked by reference). To bypass this, you can use this method to add an explicit key to the transport that will be used in the query key. For example:
import{addStaticKeyToTransport}from"@connectrpc/connect-query";import{createConnectTransport}from"@connectrpc/connect-web";consttransport=addStaticKeyToTransport(createConnectTransport({baseUrl:"https://demo.connectrpc.com",}),"demo",);
typeConnectQueryKey=[/** * To distinguish Connect query keys from other query keys, they always start with the string "connect-query". */"connect-query",{/** * A key for a Transport reference, created with createTransportKey(). */transport?:string;/** * The name of the service, e.g. connectrpc.eliza.v1.ElizaService */serviceName:string;/** * The name of the method, e.g. Say. */methodName?:string;/** * A key for the request message, created with createMessageKey(), * or "skipped". */input?:Record<string,unknown>|"skipped";/** * Whether this is an infinite query, or a regular one. */cardinality?:"infinite"|"finite";},];
TanStack Query manages query caching for you based on query keys.QueryKeys in TanStack Query are arrays with arbitrary JSON-serializable data - typically handwritten for each endpoint. In Connect-Query, query keys are more structured, since queries are always tied to a service, RPC, input message, and transport. For example, a query key might look like this:
["connect-query",{transport:"t1",serviceName:"connectrpc.eliza.v1.ElizaService",methodName:"Say",input:{sentence:"hello there",},cardinality:"finite",},];
The factorycreateConnectQueryKey makes it easy to create aConnectQueryKey, including partial keys for query filters.
Return type ofcreateQueryOptions assuming SkipToken was not provided.
interfaceQueryOptions<IextendsDescMessage,OextendsDescMessage>{queryKey:ConnectQueryKey<O>;queryFn:QueryFunction<MessageShape<O>,ConnectQueryKey<O>,MessageShape<I>>;structuralSharing:(oldData:unknown,newData:unknown)=>unknown;}
Return type ofcreateQueryOptions when SkipToken is provided.
interfaceQueryOptionsWithSkipToken<IextendsDescMessage,OextendsDescMessage,>extendsOmit<QueryOptions<I,O>,"queryFn">{queryFn:SkipToken;}
Return type ofcreateInfiniteQueryOptions assuming SkipToken was not provided.
interfaceInfiniteQueryOptions<IextendsDescMessage,OextendsDescMessage,ParamKeyextendskeyofMessageInitShape<I>,>{getNextPageParam:ConnectInfiniteQueryOptions<I,O,ParamKey>["getNextPageParam"];queryKey:ConnectQueryKey<O>;queryFn:QueryFunction<MessageShape<O>,ConnectQueryKey<O>,MessageInitShape<I>[ParamKey]>;structuralSharing:(oldData:unknown,newData:unknown)=>unknown;initialPageParam:MessageInitShape<I>[ParamKey];}
Return type ofcreateInfiniteQueryOptions when SkipToken is provided.
interfaceInfiniteQueryOptionsWithSkipToken<IextendsDescMessage,OextendsDescMessage,ParamKeyextendskeyofMessageInitShape<I>,>extendsOmit<InfiniteQueryOptions<I,O,ParamKey>,"queryFn">{queryFn:SkipToken;}
Connect-query (along with all other javascript based connect packages) can be tested with thecreateRouterTransport function from@connectrpc/connect. This function allows you to create a transport that can be used to test your application without needing to make any network requests. We also have a dedicated package,@connectrpc/connect-playwright for testing withinplaywright.
For playwright, you can see a sample testhere.
Each function that interacts with TanStack Query also provides for options that can be passed through.
import{useQuery}from'@connectrpc/connect-query';import{example}from'your-generated-code/example-ExampleService_connectquery';exportconstExample:FC=()=>{const{ data}=useQuery(example,undefined,{// These are typesafe options that are passed to underlying TanStack Query.refetchInterval:1000,});return<div>{data}</div>;};
Here is a high-level overview of how Connect-Query fits in with Connect-Web and Protobuf-ES:
Your Protobuf files serve as the primary input to the code generatorsprotoc-gen-connect-query andprotoc-gen-es. Both of these code generators also rely on primitives provided by Protobuf-ES. The Buf CLI produces the generated output. The final generated code usesTransport from Connect-Web and generates a final Connect-Query API.
Transport is a regular JavaScript object with two methods,unary andstream. See the definition in the Connect-Web codebasehere.Transport defines the mechanism by which the browser can call a gRPC-web or Connect backend. Read more about Transport on theconnect docs.
You can use Connect-Web and Connect-Query together if you like!
Connect-Query also supports gRPC-web! All you need to do is make sure you callcreateGrpcWebTransport instead ofcreateConnectTransport.
That said, we encourage you to check out theConnect protocol, a simple, POST-only protocol that works over HTTP/1.1 or HTTP/2. It supports server-streaming methods just like gRPC-Web, but is easy to debug in the network inspector.
If theTransport attached to React Context via theTransportProvider isn't working for you, then you can override transport at every level. For example, you can pass a custom transport directly to the lowest-level API likeuseQuery orcallUnaryMethod.
Connect-Query does require React, but the core (createConnectQueryKey andcallUnaryMethod) is not React specific so splitting off aconnect-solid-query is possible.
When you might not have access to React context, you can usecreateQueryOptions and provide a transport directly. For example:
import{say}from"./gen/eliza-ElizaService_connectquery";functionprefetch(){returnqueryClient.prefetchQuery(createQueryOptions(say,{sentence:"Hello"},{transport:myTransport}),);}
Tip
Transports are taken into consideration when building query keys. If you want to prefetch queries on the server, and hydrate them in the client, make sure to use the same transport key on both sides withaddStaticKeyToTransport.
Connect-Query currently only supports Unary RPC methods, which use a simple request/response style of communication similar to GET or POST requests in REST. This is because it aligns most closely with TanStack Query's paradigms. However, we understand that there may be use cases for Server Streaming, Client Streaming, and Bidirectional Streaming, and we're eager to hear about them.
At Buf, we strive to build software that solves real-world problems, so we'd love to learn more about your specific use case. If you can provide a small, reproducible example, it will help us shape the development of a future API for streaming with Connect-Query.
To get started, we invite you to open a pull request with an example project in the examples directory of the Connect-Query repository. If you're not quite sure how to implement your idea, don't worry - we want to see how you envision it working. If you already have an isolated example, you may also provide a simple CodeSandbox or Git repository.
If you're not yet at the point of creating an example project, feel free to open an issue in the repository and describe your use case. We'll follow up with questions to better understand your needs.
Your input and ideas are crucial in shaping the future development of Connect-Query. We appreciate your input and look forward to hearing from you.
Offered under theApache 2 license.
About
TypeScript-first expansion pack for TanStack Query that gives you Protobuf superpowers.
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors13
Uh oh!
There was an error while loading.Please reload this page.

