Skip to content

Are nested query keys serialised deterministically? #3052

Answered by markerikson
abhieshekumar asked this question in Q&A
Discussion options

You must be logged in to vote

Looking at the implementation of defaultSerializeQueryArgs, looks like we only go one level deep:

export const defaultSerializeQueryArgs: SerializeQueryArgs<any> = ({
  endpointName,
  queryArgs,
}) => {
  // Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
  return `${endpointName}(${JSON.stringify(queryArgs, (key, value) =>
    isPlainObject(value)
      ? Object.keys(value)
          .sort()
          .reduce<any>((acc, key) => {
            acc[key] = (value as any)[key]
            return acc
          }, {})
      : value
  )})`
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@phryneas
Comment options

@markerikson
Comment options

@abhieshekumar
Comment options

Answer selected by abhieshekumar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants