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/default-query-function.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.1 KiB

id title ref
default-query-function Default Query Function docs/framework/react/guides/default-query-function.md
// Define a default query function that will receive the query key
const defaultQueryFn: QueryFunction = async ({ queryKey }) => {
  const { data } = await axios.get(
    `https://jsonplaceholder.typicode.com${queryKey[0]}`,
  )
  return data
}

// provide the default query function to your app with defaultOptions
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      queryFn: defaultQueryFn,
    },
  },
})

bootstrapApplication(MyAppComponent, {
  providers: [provideTanStackQuery(queryClient)],
})

export class PostsComponent {
  // All you have to do now is pass a key!
  readonly postsQuery = injectQuery<Array<Post>>(() => ({
    queryKey: ['/posts'],
  }))
  // ...
}

export class PostComponent {
  // You can even leave out the queryFn and just go straight into options
  readonly postQuery = injectQuery<Post>(() => ({
    enabled: this.postIdSignal() > 0,
    queryKey: [`/posts/${this.postIdSignal()}`],
  }))
  // ...
}