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:
queries = inject(QueriesService)
injectQuery(this.queries.post(1))
injectQueries({
queries: [this.queries.post(1), this.queries.post(2)],
})
queryClient.prefetchQuery(this.queries.post(23))
queryClient.setQueryData(this.queries.post(42).queryKey, newGroups)
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:
queries = inject(QueriesService)
injectQuery(this.queries.post(1))
injectQueries({
queries: [this.queries.post(1), this.queries.post(2)],
})
queryClient.prefetchQuery(this.queries.post(23))
queryClient.setQueryData(this.queries.post(42).queryKey, newGroups)
For Infinite Queries, a separate infiniteQueryOptions helper is available.