Skip to content

refactor(web): use query key factories for safer and more consistent queries#2447

Closed
dgdavid wants to merge 1 commit intomasterfrom
query-key-factories
Closed

refactor(web): use query key factories for safer and more consistent queries#2447
dgdavid wants to merge 1 commit intomasterfrom
query-key-factories

Conversation

@dgdavid
Copy link
Copy Markdown
Contributor

@dgdavid dgdavid commented Jun 7, 2025

Note

PR stil work in progress. Full description will be added once the PR is more polished.

Instead of writing query keys manually each time, this commit introduces
the use of query key factories. This helps prevent mistakes like typos
or mismatched keys, ensures consistency, and improves maintainability by
centralizing key definitions, making future changes easier.

This follows a recommended pattern from TanStack query blog:
https://tkdodo.eu/blog/effective-react-query-keys#use-query-key-factories

@coveralls
Copy link
Copy Markdown

coveralls commented Jun 7, 2025

Coverage Status

coverage: 64.205% (-0.05%) from 64.253%
when pulling 9fb4792 on query-key-factories
into 9407017 on master.

Comment on lines +28 to +35
const l10nKeys = {
all: () => ["l10n"] as const,
config: () => [...l10nKeys.all(), "config"] as const,
locales: () => [...l10nKeys.all(), "locales"] as const,
keymaps: () => [...l10nKeys.all(), "keymaps"] as const,
timezones: () => [...l10nKeys.all(), "timezones"] as const,
};

Copy link
Copy Markdown
Contributor Author

@dgdavid dgdavid Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, would be more readable something like

const queryKeys = {
  all:       () => ["l10n"] as const,
  config:    () => ["l10n", "config"] as const,
  locales:   () => ["l10n", "locales"] as const,
  keymaps:   () => ["l10n", "keymaps"] as const,
  timezones: () => ["l10n", "timezones"] as const
}

or even,

const queryKeys = {
  all:       ["l10n"] as const,
  config:    ["l10n", "config"] as const,
  locales:   ["l10n", "locales"] as const,
  keymaps:   ["l10n", "keymaps"] as const,
  timezones: ["l10n", "timezones"] as const
}

But I'd go with the proposed approach of always using functions, to stay consistent with the rest of the query keys in the codebase. Some keys take parameters, and defining them all as functions keeps the API uniform and easier to work with.

About using composed keys ([...queryKeys.all(), "config"]) instead of hardcoding them (["l10n", "config"]), it also follows TanStack Query’s recommended practices. While it may slightly impact readability, it enables a layered composition strategy that simplifies future changes, especially when modifying top-level segments or adding new levels of nesting.

Copy link
Copy Markdown
Contributor Author

@dgdavid dgdavid Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above comment becomes more evident when looking at more complex query keys such as those in the software, network, or storage domains. There, the benefits of consistent structure and layered composition are much easier to appreciate.

@dgdavid dgdavid force-pushed the query-key-factories branch from 2efa06d to d27f242 Compare August 25, 2025 12:01
…sistency

Instead of writing query keys manually each time, this commit introduces
the use of query key factories. This helps prevent mistakes like typos
or mismatched keys, ensures consistency, and improves maintainability by
centralizing key definitions, making future changes easier.

This follows a recommended pattern from the React Query community:
https://tkdodo.eu/blog/effective-react-query-keys#use-query-key-factories
@dgdavid dgdavid force-pushed the query-key-factories branch from d27f242 to 9fb4792 Compare August 25, 2025 12:04
@dgdavid
Copy link
Copy Markdown
Contributor Author

dgdavid commented Nov 18, 2025

Closing because base code will change a lot after ongoing changes in api-v2 branch.

@dgdavid dgdavid closed this Nov 18, 2025
@dgdavid dgdavid mentioned this pull request Nov 26, 2025
@dgdavid dgdavid deleted the query-key-factories branch January 19, 2026 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants