Uh oh!
There was an error while loading. Please reload this page.
Replies: 3 comments 4 replies
love it ❤️ . One question that remains is if we want to add this to the recommended ruleset, and if so, on which level ? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I would absolutely love for this to exist. I've just spent a bunch of time going through my repo and making sure we are using
I really want to lock this down via eslint so that someone can't come and undo my hard work. In lieu of a proper dedicated rule like the one proposed here, I did manage to create a pretty gnarly set of Here's what I came up with: // Selectors to find relevant react-query function callsconstcallAFunctionThatTakesQueryOptions='CallExpression[callee.name=/^(useQuery|useSuspenseQuery)$/]'constcallAMethodThatTakeQueryOptions='CallExpression[callee.property.name=/^(invalidateQueries|fetchQuery|removeQueries|getQueriesData)$/]'constcallAMethodThatTakesAQueryKey='CallExpression[callee.property.name=/^(getQueryData|setQueryData)$/]'// "Assertions" to enforce that the first argument is either `fooQueryOptions()` or `fooQueryOptions().queryKey`constfirstArgMustBeSomethingQueryOptions='[arguments.0.callee.name!=/.+QueryOptions/]'constfirstArgMustBeSomethingQueryOptionsDotQueryKey=':matches([arguments.0.object.callee.name!=/.+QueryOptions/],[arguments.0.property.name!="queryKey"])'// DRY-ing up the error messagesconstqueryOptionsDocs=`See: https://tanstack.com/query/v5/docs/framework/react/guides/query-options`// Glue it all togetherconstqueryOptionsRules=[{selector: `${callAFunctionThatTakesQueryOptions}${firstArgMustBeSomethingQueryOptions}`,message: `Generate the query options using a helper e.g. \`profileQueryOptions()\` ${queryOptionsDocs}`,},{selector: `${callAMethodThatTakeQueryOptions}${firstArgMustBeSomethingQueryOptions}`,message: `Generate the query options using a helper e.g. \`profileQueryOptions()\` ${queryOptionsDocs}`,},{selector: `${callAMethodThatTakesAQueryKey}${firstArgMustBeSomethingQueryOptionsDotQueryKey}`,message: `Look up the queryKey using a queryOptions helper e.g. \`profileQueryOptions().queryKey\` ${queryOptionsDocs}`,},]Like I said, it's pretty gnarly, and it has some rough edges. I haven't listed every single function (e.g. // this is not allowed by the above rules :(constquery=useQuery({
...groupOptions(1),select: (data)=>data.groupName,})It might be technically possible to cover every edge case with |
I created this plugin a few months ago about pretty much this same issue https://github.com/danielpza/eslint-plugin-react-query-options I'd be happy to send a PR adding the rules as a PoC if you are still planning to add it, or you could also use the code for reference. There's not a lot to it https://github.com/danielpza/eslint-plugin-react-query-options/blob/main/src/rules/use-query-no-inline-query.ts. Referenced from TkDodo/blog-comments#128 (reply in thread) |
Uh oh!
There was an error while loading. Please reload this page.
Separating
queryKeyandqueryFncan cause unexpected runtime issues when the same query key is accidentally used with more than onequeryFn.Rule Details
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Options
ignorePatternsAn array of strings used to ignore specific query key patterns:
{ "@tanstack/query/prefer-query-options": [ "error", { "ignorePatterns": { "startsWith": ["ignore-key"], "endsWith": ["ignore-key"], "includes": ["ignore-key"], } } ] }collectionsAn array of strings used to enforce the use of
queryOptionsin specific collections of queries:{ "@tanstack/query/prefer-query-options": [ "error", { "collections": ["fooQueries", "deeply.nested.queries"] } ] }When Not To Use It
If you do not want to enforce the use of
queryOptionsin your codebase, you will not need this rule.Attributes
All reactions