Skip to content

Comments

feat(react-query): add httpClientInjection: 'reactQueryMeta' option#2920

Draft
FR073N wants to merge 1 commit intoorval-labs:masterfrom
FR073N:FR073N/react-query-injection
Draft

feat(react-query): add httpClientInjection: 'reactQueryMeta' option#2920
FR073N wants to merge 1 commit intoorval-labs:masterfrom
FR073N:FR073N/react-query-injection

Conversation

@FR073N
Copy link
Contributor

@FR073N FR073N commented Feb 7, 2026

Hey team, I'd love to get your feedback on this implementation.

Fix #2887

Context

When using client: 'react-query' with httpClient: 'axios', there's no clean way to inject a configured axios instance at runtime. The custom mutator creates a singleton at import time, which blocks NPM package distribution, SSR, and test mocking.

Linked to :

This PR

New option httpClientInjection: 'reactQueryMeta' that leverages React Query's native meta feature:

export default defineConfig({
  petstore: {
    output: {
      client: 'react-query',
      httpClient: 'axios',
      httpClientInjection: 'reactQueryMeta',
    },
    input: { target: './petstore.yaml' },
  },
});

Consumer side:

import axios from 'axios';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { useListPets } from '@my-org/petstore-sdk';

// Consumer configures their own instance
const axiosInstance = axios.create({ baseURL: 'https://api.example.com' });
axiosInstance.interceptors.request.use(addAuthHeader);

// Inject via QueryClient meta
const queryClient = new QueryClient({
  defaultOptions: {
    queries: { meta: { axiosInstance } },
    mutations: { meta: { axiosInstance } },
  },
});

function Pets() {
  // Hook resolves the instance automatically — consumer doesn't pass anything
  const { data } = useListPets();
  return <ul>{data?.map((pet) => <li key={pet.id}>{pet.name}</li>)}</ul>;
}

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <Pets />
    </QueryClientProvider>
  );
}

Main benefit

With this option, you can build a real SDK from orval-generated code and publish it on NPM. The axios instance is injected by the consumer, not hardcoded in the package. Hooks return Promise<T> instead of Promise<AxiosResponse<T>> for SSR compatibility with dehydrate().

Looking for feedback

If you use orval-generated code with react-query, I'd appreciate your input:

  • Does this approach (meta in QueryClient) work for you?
  • Returning T instead of AxiosResponse<T> — acceptable?
  • Any other injection patterns you'd prefer?

Includes tests, docs, and a complete sample in samples/react-query/queryclient-meta/.

@FR073N FR073N marked this pull request as draft February 7, 2026 08:11
@FR073N FR073N force-pushed the FR073N/react-query-injection branch 3 times, most recently from 8a7fca6 to f0a07a3 Compare February 7, 2026 08:27
…s instance injection

Adds support for injecting axios instances at runtime via QueryClient.defaultOptions.meta,
enabling NPM package distribution, SSR, and mock injection without custom mutators.

Generated hooks automatically resolve the axios instance from:
- queries.meta.axiosInstance for queries
- mutations.meta.axiosInstance for mutations

HTTP functions now return Promise<T> (data unwrapped) instead of Promise<AxiosResponse<T>>
to ensure SSR dehydrate() can serialize the cache.
@FR073N
Copy link
Contributor Author

FR073N commented Feb 7, 2026

@melloware Here I created the PR, but as you said we may need inputs to be sure this is acceptable. Thank you.

@melloware
Copy link
Collaborator

Yep I would definitely like others input as this one concerns me. I am not against it I just have concerns!

I will ask others to weigh in

@melloware
Copy link
Collaborator

looks like merge conflicts

@melloware melloware added tanstack-query TanStack Query related issue axios Axios related issue labels Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

axios Axios related issue tanstack-query TanStack Query related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

React Query - Axios Instance Injection via QueryClient Meta

2 participants