Registry / ai-ml / react-query-grpc-gateway

react-query-grpc-gateway

JSON →
library2.0.0jsnpmunverified

A React hook library that simplifies calling gRPC Gateway endpoints using TanStack Query. Current stable version is 2.0.0, with active development on GitHub. It provides `useServiceQuery` and `useServiceMutation` as drop-in replacements for `useQuery` and `useMutation`, allowing request configuration (headers, auth tokens, path prefix) to be propagated via React context. Key differentiators: works with generated TypeScript clients from protoc-gen-grpc-gateway-ts, includes `sideEffect` helpers for managing optimistic updates and cache invalidation across related queries.

npm install react-query-grpc-gateway
INSTALL
IMPORT
SIG · REACT-QUERY-GRPC-G
R
react-query-grpc-gateway
ai-mljavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ServiceContext
import { ServiceContext } from 'react-query-grpc-gateway'
import { ServiceContextProvider } from 'react-query-grpc-gateway'
Export name is ServiceContext, not ServiceContextProvider. Use .Provider inside.
useServiceQuery
import { useServiceQuery } from 'react-query-grpc-gateway'
import useServiceQuery from 'react-query-grpc-gateway'
Named export only (ESM). No default export.
useServiceMutation
import { useServiceMutation } from 'react-query-grpc-gateway'
const { useServiceMutation } = require('react-query-grpc-gateway')
Named export. CommonJS require will not work for this ESM-only package.
sideEffect
import { sideEffect } from 'react-query-grpc-gateway'
Utility function for chaining mutation side effects. Also named export.

Setting up the provider, using useServiceQuery for a GET request, and using useServiceMutation with sideEffect for optimistic updates and cache invalidation.

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { ServiceContext, useServiceQuery, useServiceMutation, sideEffect } from 'react-query-grpc-gateway'; import { getUser, updateUser, getUserList } from './generated-client'; // from protoc-gen-grpc-gateway-ts const queryClient = new QueryClient(); const requestOptions = { pathPrefix: process.env.API_HOST ?? 'http://localhost:8080', credentials: 'include', headers: { 'X-CSRF-Protection': '1' }, }; function AppProviders({ children }: { children: React.ReactNode }) { return ( <QueryClientProvider client={queryClient}> <ServiceContext.Provider value={requestOptions}> {children} </ServiceContext.Provider> </QueryClientProvider> ); } function UserProfilePage({ userID }: { userID: string }) { const { data, isLoading, error } = useServiceQuery(getUser, { id: userID }); if (isLoading) return <div>Loading...</div>; if (error) return <div>Error: {error.message}</div>; return <div>User: {data?.name}</div>; } function useUpdateUser() { return useServiceMutation(updateUser, { ...sideEffect(getUser, { mapKey: (update) => ({ id: update.id }), patchFn: (oldData, variables) => ({ ...oldData, name: variables.name }), updateFn: (oldData, response) => response, }), ...sideEffect(getUserList, { mapKey: () => null, invalidate: true, }), }); }
Debug
Known issues
breakingServiceContext is a plain object provider, not a context with a provider method.
fix
Use <ServiceContext.Provider value={...}> instead of <ServiceContextProvider>.
affects: >=2.0.0
deprecatedVersion 1.x used a different API (useQueryExtended).
fix
Upgrade to v2 and use useServiceQuery with new signature.
affects: <2.0.0
gotchasideEffect must be spread into mutation options object.
fix
Use ...sideEffect(getUser, {...}) when constructing mutation options.
affects: >=2.0.0
gotchaRequest configuration from ServiceContext is not applied if you provide custom options to useServiceQuery directly.
fix
Do not mix context options with direct options; prefer using context for global config.
affects: >=2.0.0
gotchaWorks only with generated TypeScript clients from protoc-gen-grpc-gateway-ts. Not a generic gRPC client.
fix
Ensure you are using the correct client generator.
affects: >=2.0.0
Errors
Common errors & fixes
Cannot find module 'react-query-grpc-gateway' or its corresponding type declarations.
Missing installation or TypeScript not resolving the package.
fix
npm install react-query-grpc-gateway @tanstack/react-query@^5.35.1
Property 'pathPrefix' does not exist on type 'RequestOptions'
TypeScript requires proper client generation; pathPrefix is not a standard fetch property.
fix
Ensure you have installed protoc-gen-grpc-gateway-ts and generated the client. PathPrefix is available on the generated request configuration.
Uncaught TypeError: serviceContext is not a function
Using ServiceContext as a function instead of as an object with .Provider.
fix
Use <ServiceContext.Provider value={...}> in JSX, not ServiceContext({...}).
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
@tanstack/react-queryrequiredpeer dependency for query hooks (useQuery, useMutation)
Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
react-query-grpc-gateway — npm install react-query-grpc-gateway · libregistry