Skip to content

Commit 43bb50a

Browse files
committed
add client and query api pages
1 parent 5ea5f7c commit 43bb50a

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

website/docs/api/builder.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ Binds all query methods to this class instance, so that they can be called after
133133
asFrozen(): this
134134
```
135135

136-
Returns a frozen version of this builder. The frozen version cannot be modified using `with*` methods.
136+
Returns a frozen version of this builder. The frozen version cannot be modified using `with*` methods, so those methods will not be available.
137+
The methods in the [Query API](./query) will still be available.
137138

138139
### client
139140

website/docs/api/client.mdx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,46 @@
11
---
22
title: Client API
33
sidebar_position: 10
4+
hide_table_of_contents: true
45
---
56

6-
Work in progress. This page is not finished yet.
7+
The Client API provides imperative methods, similar to how QueryClient and QueryCache can be used in Tanstack Query.
8+
It can be accessed via [`builder.client`](./builder#client) in the `QueryBuilder` instance. Available methods are:
9+
10+
```ts
11+
ensureData(vars, queryOptions): Promise<Data>;
12+
ensureInfiniteData(vars, paginationOptions): Promise<InfiniteData>;
13+
refetch(vars, queryFilters, refetchOptions): Promise<void>;
14+
fetch(vars, queryOptions): Promise<Data>;
15+
fetchInfinite(vars, queryOptions): Promise<InfiniteData>;
16+
isFetching(vars, queryFilters): number;
17+
prefetch(vars, queryOptions): Promise<Data>;
18+
prefetchInfinite(vars, paginationOptions): Promise<InfiniteData>;
19+
reset(vars, queryFilters, resetOptions): Promise<void>;
20+
remove(vars, queryFilters): void;
21+
cancel(vars, queryFilters, cancelOptions): Promise<void>;
22+
invalidate(vars, queryFilters, invalidateOptions): Promise<void>;
23+
getData(vars, queryFilters): Data | undefined;
24+
setData(vars, updater, setDataOptions): Data | undefined;
25+
getState(vars): QueryState;
26+
getMutation(vars, mutationFilters): Mutation;
27+
isMutating(vars, mutationFilters): number;
28+
mutate(vars, mutationOptions): Promise<Data>;
29+
```
30+
31+
The main use case for these methods is to manipulate the cache outside of React components.
32+
For example, you can use `ensureData` in a router before the route is loaded,
33+
to ensure that the data for a specific article is in the cache, and if not, fetch it from the server.
34+
35+
Most of these methods correspond to the methods in [`QueryClient`](https://tanstack.com/query/latest/docs/reference/QueryClient) of Tanstack Query. For example, `ensureData` is similar to `ensureQueryData`, and `refetch` is similar to `refetchQueries`.
36+
The main difference is that these methods are pre-populated with the variables in the builder, so you don't need to pass them again. For example, suppose we have a builder like this:
37+
38+
```ts
39+
const articlesQuery = builder.withPath("/articles/:id").withData<ArticleData>();
40+
```
41+
42+
We can fetch the data for a specific article imperatively like this:
43+
44+
```ts
45+
const article = await articlesQuery.client.fetch({ params: { id: 1 } });
46+
```

website/docs/api/query.mdx

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,82 @@ title: Query API
33
sidebar_position: 5
44
---
55

6-
Work in progress. This page is not finished yet.
6+
After configuring the builder, you can use it inside your React components with the hooks exposed in this page.
7+
8+
The query builder provides a set of hooks similar to Tanstack Query itself. Each hook can be passed the variables for that builder, as well as the options object that is passed to the underlying Tanstack Query hook.
9+
10+
Tanstack Query normally provides a set of hooks for various use cases. These are `useQuery`, `useInfiniteQuery`, `useMutation`, and `useQueries`.
11+
There are also some variants like `useSuspenseQuery` and `usePrefetchQuery`.
12+
13+
## Query hooks
14+
15+
### useQuery
16+
17+
```ts
18+
useQuery(vars, queryOptions): QueryResult;
19+
```
20+
21+
There are also `usePrefetchQuery` and `useSuspenseQuery` variants.
22+
23+
### useQueries
24+
25+
```ts
26+
useQueries(queries: Array<{ vars, options, mapKey }>, sharedVars, sharedOptions): QueriesResult;
27+
```
28+
29+
There is also `useSuspenseQueries` variant.
30+
31+
### useInfiniteQuery
32+
33+
```ts
34+
useInfiniteQuery(vars, queryOptions): InfiniteQueryResult;
35+
```
36+
37+
Infinite query methods are only available if you have configured the builder with `withPagination`.
38+
39+
There are also `usePrefetchInfiniteQuery` and `useSuspenseInfiniteQuery` variants.
40+
41+
### useIsFetching
42+
43+
```ts
44+
useIsFetching(vars, queryFilters): number;
45+
```
46+
47+
## Mutation hooks
48+
49+
Each builder can be used for query or mutation. It's up to the developer to decide how to use it.
50+
The underlying query/mutation functions are the same. Only the underlying Tanstack Query hooks, and options passed to them are different.
51+
52+
### useMutation
53+
54+
```ts
55+
useMutation(mutationOptions): UseMutationResult;
56+
```
57+
58+
### useMutationState
59+
60+
```ts
61+
useMutationState(vars, mutationFilters, select): Mutation | SelectedState;
62+
```
63+
64+
### useIsMutating
65+
66+
```ts
67+
useIsMutating(vars, mutationFilters): number;
68+
```
69+
70+
## Utilities
71+
72+
There are some utility functions that are not hooks, but can be used for more advanced use cases, such as calling underlying Tanstack Query hooks directly.
73+
74+
```ts
75+
getQueryFn(operationType: 'query' | 'queries' | 'infiniteQuery'): QueryFn;
76+
getQueryKeyHashFn(): QueryKeyHashFn;
77+
getQueryKey(vars): QueryKey;
78+
getQueryOptions(vars, queryOptions, operationType): QueryOptions;
79+
getInfiniteQueryOptions(vars, paginataionOptions): InfiniteQueryOptions;
80+
getMutationFn(queryClient, meta): MutationFn;
81+
getMutationKey(): MutationKey;
82+
getMutationOptions(queryClient, mutationOptions): MutationOptions;
83+
getMutationFilters(vars, mutationFilters): MutationFilters;
84+
```

0 commit comments

Comments
 (0)