React Toolkit 查询 - 如何将 transformResponse 道具与 injectEndpoints() 一起使用

React Toolkit Query - How to use transformResponse props with injectEndpoints()

我有一个根 API,我在其中注入端点。但是当我尝试添加转换功能时,它不起作用。 From the documentation of transformResponse 我可以看到 transform 属性附加到查询对象。

这就是我正在尝试的

const departmentsApi = onboardApi.injectEndpoints({
    endpoints: builder => ({
        getDepartments: builder.query({
            query: params => ({
                url: `departments`,
                params,
                // Transform and normalize API response
            }),
            transformResponse: response => {
                 console.log('transform', response);
                 return response;
            },
            providesTags: result => {
                return result
                    ? [
                            ...result.items.map(({ id }) => ({ type: 'Department', id })),
                            { type: 'Department', id: 'LIST' },
                      ]
                    : [{ type: 'Department', id: 'LIST' }];
            },
        }),
    }),
    overrideExisting: false,
});

任何指导都会有所帮助。

资源

  1. Documentation for Injecting endpoints

您在上面链接了 Redux-Query 的文档,而不是 RTK-Query。 transform 不是东西。你可能想要 transformResponse:

getDepartments: builder.query({
    query: params => ({
        url: `departments`,
        params,
        // Transform and normalize API response
        transform: response => {
            console.log(response);
            return response;
        },
    }),
    transformResponse: (response) => response.some.deeply.nested.collection,
    providesTags: result => {
        return result
            ? [
                    ...result.items.map(({ id }) => ({ type: 'Department', id })),
                    { type: 'Department', id: 'LIST' },
              ]
            : [{ type: 'Department', id: 'LIST' }];
    },
}),