0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-16 22:50:20 +00:00
query/docs/framework/angular/guides/query-options.md
Wonsuk Choi 390de5a8dd
docs(angular-query-experimental): add 'readonly' and use descriptive names for class fields in guide examples (#11445)
* docs(angular-query-experimental): add 'readonly' and use descriptive names for class fields in guide examples

* docs(angular-query-experimental): use descriptive names for class fields in 'placeholder-query-data.md'
2026-09-08 11:39:41 +09:00

1.2 KiB

id title ref
query-options Query Options docs/framework/react/guides/query-options.md
import { queryOptions, noop } from '@tanstack/angular-query-experimental'

@Injectable({
  providedIn: 'root',
})
export class QueriesService {
  private readonly 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.query(this.queries.post(23)).catch(noop)
queryClient.setQueryData(this.queries.post(42).queryKey, newPost)
// Type inference still works, so groupQuery.data will be the return type of select instead of queryFn
queries = inject(QueriesService)

groupQuery = injectQuery(() => ({
  ...groupOptions(1),
  select: (data) => data.title,
}))