Skip to content

Commit

Permalink
Use build consistently instead of builder
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Jan 17, 2025
1 parent 62b4cad commit 7a2ee50
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 51 deletions.
12 changes: 6 additions & 6 deletions docs/rtk-query/api/createApi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import type { Pokemon } from './types'
export const pokemonApi = createApi({
reducerPath: 'pokemonApi',
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getPokemonByName: builder.query<Pokemon, string>({
endpoints: (build) => ({
getPokemonByName: build.query<Pokemon, string>({
query: (name) => `pokemon/${name}`,
}),
}),
Expand Down Expand Up @@ -148,7 +148,7 @@ See [Endpoint Definition Parameters](#endpoint-definition-parameters) for detail
#### Query endpoint definition
Query endpoints (defined with `builder.query()`) are used to cache data fetched from the server.
Query endpoints (defined with `build.query()`) are used to cache data fetched from the server.
You must specify either a `query` field (which will use the API's `baseQuery` to make a request), or a `queryFn` function with your own async logic. All other fields are optional.
Expand Down Expand Up @@ -226,9 +226,9 @@ export type QueryDefinition<
#### Infinite Query endpoint definition
Infinite query endpoints (defined with `builder.infiniteQuery()`) are used to cache multi-page data sets from the server. They have all the same callbacks and options as standard query endpoints, but also require an additional [`infiniteQueryOptions`](#infinitequeryoptions) field to specify how to calculate the unique parameters to fetch each page.
Infinite query endpoints (defined with `build.infiniteQuery()`) are used to cache multi-page data sets from the server. They have all the same callbacks and options as standard query endpoints, but also require an additional [`infiniteQueryOptions`](#infinitequeryoptions) field to specify how to calculate the unique parameters to fetch each page.
For infinite query endpoints, there is a separation between the "query arg" used for the cache key, and the "page param" used to fetch a specific page. For example, a Pokemon API endpoint might have a string query arg like `"fire"` as the query arg, but use a page number as the param to determine which page to fetch out of the results. This means the page param is what will be passed to your `query` or `queryFn` methods.
For infinite query endpoints, there is a separation between the "query arg" used for the cache key, and the "page param" used to fetch a specific page. For example, a Pokemon API endpoint might have a string query arg like `"fire"` , but use a page number as the param to determine which page to fetch out of the results. This means the page param is what will be passed to your `query` or `queryFn` methods.
```ts title="Infinite Query endpoint definition" no-transpile
export type PageParamFunction<DataType, PageParam> = (
Expand Down Expand Up @@ -284,7 +284,7 @@ export type InfiniteQueryDefinition<
#### Mutation endpoint definition
Mutation endpoints (defined with `builder.mutation()`) are used to send updates to the server, and force invalidation and refetching of query endpoints.
Mutation endpoints (defined with build.mutation()`) are used to send updates to the server, and force invalidation and refetching of query endpoints.

As with queries, you must specify either the `query` option or the `queryFn` async method.

Expand Down
22 changes: 11 additions & 11 deletions packages/toolkit/src/query/core/buildMiddleware/queryLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ export type MutationLifecycleApi<
* baseQuery: fetchBaseQuery({ baseUrl: 'https://dummyjson.com' }),
* reducerPath: 'postsApi',
* tagTypes: ['Posts'],
* endpoints: (builder) => ({
* getPosts: builder.query<PostsApiResponse, void>({
* endpoints: (build) => ({
* getPosts: build.query<PostsApiResponse, void>({
* query: () => `/posts`,
* }),
*
* getPostById: builder.query<Post, QueryArgument>({
* getPostById: build.query<Post, QueryArgument>({
* query: (postId) => `/posts/${postId}`,
* }),
* }),
Expand Down Expand Up @@ -283,8 +283,8 @@ export type MutationLifecycleApi<
* }
*
* export const extendedApiSlice = baseApiSlice.injectEndpoints({
* endpoints: (builder) => ({
* getPostsByUserId: builder.query<PostsApiResponse, QueryArgument>({
* endpoints: (build) => ({
* getPostsByUserId: build.query<PostsApiResponse, QueryArgument>({
* query: (userId) => `/posts/user/${userId}`,
*
* onQueryStarted: updatePostOnFulfilled,
Expand Down Expand Up @@ -346,12 +346,12 @@ export type TypedQueryOnQueryStarted<
* baseQuery: fetchBaseQuery({ baseUrl: 'https://dummyjson.com' }),
* reducerPath: 'postsApi',
* tagTypes: ['Posts'],
* endpoints: (builder) => ({
* getPosts: builder.query<PostsApiResponse, void>({
* endpoints: (build) => ({
* getPosts: build.query<PostsApiResponse, void>({
* query: () => `/posts`,
* }),
*
* getPostById: builder.query<Post, number>({
* getPostById: build.query<Post, number>({
* query: (postId) => `/posts/${postId}`,
* }),
* }),
Expand All @@ -377,8 +377,8 @@ export type TypedQueryOnQueryStarted<
* }
*
* export const extendedApiSlice = baseApiSlice.injectEndpoints({
* endpoints: (builder) => ({
* addPost: builder.mutation<Post, Omit<QueryArgument, 'id'>>({
* endpoints: (build) => ({
* addPost: build.mutation<Post, Omit<QueryArgument, 'id'>>({
* query: (body) => ({
* url: `posts/add`,
* method: 'POST',
Expand All @@ -388,7 +388,7 @@ export type TypedQueryOnQueryStarted<
* onQueryStarted: updatePostOnFulfilled,
* }),
*
* updatePost: builder.mutation<Post, QueryArgument>({
* updatePost: build.mutation<Post, QueryArgument>({
* query: ({ id, ...patch }) => ({
* url: `post/${id}`,
* method: 'PATCH',
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ export interface InfiniteQueryExtraOptions<
*
* ```ts
* // codeblock-meta title="infiniteQueryOptions example"
* getInfinitePokemonWithMax: builder.infiniteQuery<
* getInfinitePokemonWithMax: build.infiniteQuery<
Pokemon[],
string,
number
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/tests/infiniteQueries.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe('Infinite queries', () => {

const pokemonApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getInfinitePokemon: builder.infiniteQuery<Pokemon[], string, number>({
endpoints: (build) => ({
getInfinitePokemon: build.infiniteQuery<Pokemon[], string, number>({
infiniteQueryOptions: {
initialPageParam: 0,
getNextPageParam: (
Expand Down
59 changes: 28 additions & 31 deletions packages/toolkit/src/query/tests/infiniteQueries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ describe('Infinite queries', () => {

const pokemonApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getInfinitePokemon: builder.infiniteQuery<Pokemon[], string, number>({
endpoints: (build) => ({
getInfinitePokemon: build.infiniteQuery<Pokemon[], string, number>({
infiniteQueryOptions: {
initialPageParam: 0,
getNextPageParam: (
lastPage,
allPages,
// Page param type should be `number`
lastPageParam,
allPageParams,
) => lastPageParam + 1,
Expand All @@ -61,34 +60,32 @@ describe('Infinite queries', () => {
return `https://example.com/listItems?page=${pageParam}`
},
}),
getInfinitePokemonWithMax: builder.infiniteQuery<
Pokemon[],
string,
number
>({
infiniteQueryOptions: {
initialPageParam: 0,
maxPages: 3,
getNextPageParam: (
lastPage,
allPages,
lastPageParam,
allPageParams,
) => lastPageParam + 1,
getPreviousPageParam: (
firstPage,
allPages,
firstPageParam,
allPageParams,
) => {
return firstPageParam > 0 ? firstPageParam - 1 : undefined
getInfinitePokemonWithMax: build.infiniteQuery<Pokemon[], string, number>(
{
infiniteQueryOptions: {
initialPageParam: 0,
maxPages: 3,
getNextPageParam: (
lastPage,
allPages,
lastPageParam,
allPageParams,
) => lastPageParam + 1,
getPreviousPageParam: (
firstPage,
allPages,
firstPageParam,
allPageParams,
) => {
return firstPageParam > 0 ? firstPageParam - 1 : undefined
},
},
query(pageParam) {
return `https://example.com/listItems?page=${pageParam}`
},
},
query(pageParam) {
return `https://example.com/listItems?page=${pageParam}`
},
}),
counters: builder.query<{ id: string; counter: number }, string>({
),
counters: build.query<{ id: string; counter: number }, string>({
queryFn: async (arg) => {
if (!(arg in counters)) {
counters[arg] = 0
Expand Down Expand Up @@ -486,8 +483,8 @@ describe('Infinite queries', () => {
test('can fetch pages with refetchOnMountOrArgChange active', async () => {
const pokemonApiWithRefetch = createApi({
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getInfinitePokemon: builder.infiniteQuery<Pokemon[], string, number>({
endpoints: (build) => ({
getInfinitePokemon: build.infiniteQuery<Pokemon[], string, number>({
infiniteQueryOptions: {
initialPageParam: 0,
getNextPageParam: (
Expand Down

0 comments on commit 7a2ee50

Please sign in to comment.