Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support arrays and objects as query keys #85

Open
khalibloo opened this issue Dec 8, 2022 · 4 comments
Open

Support arrays and objects as query keys #85

khalibloo opened this issue Dec 8, 2022 · 4 comments
Labels
✨ enhancement New feature or request

Comments

@khalibloo
Copy link

Sometimes, a string just isn't enough to reliably represent a query key. In a case where you have many params in a request, such as filters, and sorting options, it becomes cumbersome to have to convert it all into a string, while also ensuring that the order of the concatenation is consistently preserved.

React query allows arrays of strings and objects as query keys. It would be nice to have that convenience in Rakkas as well.

@cyco130
Copy link
Member

cyco130 commented Dec 10, 2022

I'm working on a full react-query integration to make this and all other nice features available in Rakkas. Until then you can use, e.g., useQuery(JSON.stringify(["my", "array", "key"]), () => ...) or create your own wrapper function that serializes the key.

@nitedani
Copy link
Member

nitedani commented Mar 23, 2023

I'm working on a full react-query integration to make this and all other nice features available in Rakkas. Until then you can use, e.g., useQuery(JSON.stringify(["my", "array", "key"]), () => ...) or create your own wrapper function that serializes the key.

This won't invalidate useQuery(JSON.stringify(["items", "123"])) if I call client.invalidateQueries(JSON.stringify(["items"])) will it?

@cyco130
Copy link
Member

cyco130 commented Mar 23, 2023

This won't invalidate useQuery(JSON.stringify(["items", "123"])) if I call client.invalidateQueries(JSON.stringify(["items"])) will it?

No but this will:

client.invalidateQueries((k) => k.startsWith(`["items"`))

as a workaround for now.

Please bear with me as I experiment with various APIs for plugins. Real react-query integration will almost certainly be the first plugin.

@nitedani
Copy link
Member

Take your time, all is good :)
Here is the workaround I use for now:

export const createQueryKey = (key: (string | number)[]) => JSON.stringify(key);
export const invalidateQuery = (
  client: QueryClient,
  key: (string | number)[]
) => {
  client.invalidateQueries((_key) => {
    const isJson = _key.startsWith("[") && _key.endsWith("]");
    if (!isJson) return false;
    try {
      const parsed = JSON.parse(_key);
      if (!Array.isArray(parsed)) return false;
      return key.every((k, i) => k === parsed[i]);
    } catch (error) {
      return false;
    }
  });
};

@cyco130 cyco130 added the ✨ enhancement New feature or request label May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants