rtk-incubator / rtk-query

Data fetching and caching addon for Redux Toolkit
https://redux-toolkit.js.org/rtk-query/overview
MIT License
626 stars 31 forks source link

Don't add undefined params in query #171

Closed tevariou closed 3 years ago

tevariou commented 3 years ago

Description

I have a mutation with those arguments:

export type UpdateFooApiArg = {
  /** A unique value identifying this Foo. */
  id: string;
  foo: Foo;
  /** optionalBar param */
  optionalBar?: string;
};

When I want to perform an update with

  const [updateFoo] = useUpdateFooMutation();
/** optionalBar is not specified **/
updateFoo({
        id: foo.id,
        foo: foo,
      });

The resulting query is http://host/api/foos?optionalBar=undefined

I use rtk-codegen to generate my hooks:

updateFoo: build.mutation<UpdateFooApiResponse, UpdateFooApiArg>({
      query: (queryArg) => ({
        url: `/api/foos/${queryArg.id}/`,
        method: "PUT",
        body: queryArg.foo,
        params: { optionalBar: queryArg.optionalBar },
      }),
    }),
phryneas commented 3 years ago

This is solved in https://github.com/rtk-incubator/rtk-query/pull/146 but we haven't had a release since then.

You can install the most current build from https://ci.codesandbox.io/status/rtk-incubator/rtk-query/pr/1 and try that out if it solves the problem for you.

Little warning though: the entry point for the react hooks createApi has changed to @rtk-incubator/rtk-query/react