Skip to content

How to Serialize Query Args??? Bug? #3640

@rwilliams3088

Description

@rwilliams3088

I would like to be able to supply things like Date objects as parameters to my endpoints (vs requiring the user to serialize things beforehand, which is tedious and susceptible to error).

So, for instance, I may have an endpoint like so:

export interface GetScheduleVariables {
  schedule: ScheduleIdentifier;
}

export const MyApi = createApi({
  reducerPath: 'myApi',
  baseQuery: fetchBaseQuery({ baseUrl }), 
  endpoints: (builder) => ({
    getSchedule: builder.query<GetSchedule, GetScheduleVariables>({
      // attempt to make RTK Query happy about the date...
      serializeQueryArgs: (params) => {
        return JSON.stringify({
          schedule: {
            code: params.queryArgs.schedule.code,
            date: params.queryArgs.schedule.date.toISOString()
          }
        });
      },
      query: (params) => ({
        ...
      })
    }),
  })
});

The request itself executes fine - but the console is filled with errors like this:

    A non-serializable value was detected in the state, in the path: myApi.queries.{"schedule":{"code":"5ea5d2ec-59f6-4ed1-a9d2-fed92e532c73","date":"2023-07-27T00:00:00.000Z"},"filter":{}}.originalArgs.schedule.date. Value: Date Wed Jul 26 2023 17:00:00 GMT-0700 (Pacific Daylight Time)

    Take a look at the reducer(s) handling this action type: myApi/executeQuery/pending.

    (See https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state)

So, despite the SerializeQueryArgs configuration, the Date object is hitting the store... Have I implemented my SerializeQueryArgs function incorrectly? Or is there something else I can try to serialize the arguments as part of my endpoint configuration?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions