Customizing Cache Keys

By default, when creating a cache entry, RTK Query will take the query arguments, sort object keys where applicable, stringify the result, and concatenate it with the endpoint name. This creates a cache key based on the combination of arguments + endpoint name (ignoring object key order), such that calling any given endpoint with the same arguments will result in the same cache key.

Thus, calling useGetPostQuery({id:1, user:'moon'}) after useGetPostQuery({user:'moon', id:1}) will not cause a refetch.

Sometimes, you may want to map different queries to the same cache key. To do that, you can define your own mapping function for the cache keys with serializeQueryArgs:

export const postApi = createApi({ reducerPath: "postApi", baseQuery: fetchBaseQuery({ baseUrl: "https://webcodingcenter.com/shared/" }), endpoints: (builder) => ({ getPost: builder.query({ query: (id) => `get_post.php?id=${id}` }) }), serializeQueryArgs: q => (q.endpointName+Math.floor(q.queryArgs/3)) // <-- define this function to customize cache keys });