One of the best ways to share queryKey and queryFn between multiple places, yet keep them co-located to one another, is to use the queryOptions helper. At runtime, this helper just returns whatever you pass into it, but it has a lot of advantages when using it with TypeScript. You can define all possible options for a query in one place, and you'll also get type inference and type safety for all of them.
import { queryOptions } from '@tanstack/angular-query-experimental' @Injectable({ providedIn: 'root', }) export class QueriesService { private http = inject(HttpClient) post(postId: number) { return queryOptions({ queryKey: ['post', postId], queryFn: () => { return lastValueFrom( this.http.get<Post>( `https://jsonplaceholder.typicode.com/posts/${postId}`, ), ) }, }) } } // usage: postId = input.required({ transform: numberAttribute, }) queries = inject(QueriesService) postQuery = injectQuery(() => this.queries.post(this.postId())) queryClient.prefetchQuery(this.queries.post(23)) queryClient.setQueryData(this.queries.post(42).queryKey, newPost)
import { queryOptions } from '@tanstack/angular-query-experimental' @Injectable({ providedIn: 'root', }) export class QueriesService { private http = inject(HttpClient) post(postId: number) { return queryOptions({ queryKey: ['post', postId], queryFn: () => { return lastValueFrom( this.http.get<Post>( `https://jsonplaceholder.typicode.com/posts/${postId}`, ), ) }, }) } } // usage: postId = input.required({ transform: numberAttribute, }) queries = inject(QueriesService) postQuery = injectQuery(() => this.queries.post(this.postId())) queryClient.prefetchQuery(this.queries.post(23)) queryClient.setQueryData(this.queries.post(42).queryKey, newPost)
For Infinite Queries, a separate infiniteQueryOptions helper is available.
You can still override some options at the component level. A very common and useful pattern is to create per-component select functions:
// Type inference still works, so query.data will be the return type of select instead of queryFn queries = inject(QueriesService) query = injectQuery(() => ({ ...groupOptions(1), select: (data) => data.title, }))
// Type inference still works, so query.data will be the return type of select instead of queryFn queries = inject(QueriesService) query = injectQuery(() => ({ ...groupOptions(1), select: (data) => data.title, }))