Replies: 2 comments 1 reply
-
love it ❤️ . One question that remains is if we want to add this to the recommended ruleset, and if so, on which level ? |
Beta Was this translation helpful? Give feedback.
-
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 calls
const callAFunctionThatTakesQueryOptions = 'CallExpression[callee.name=/^(useQuery|useSuspenseQuery)$/]'
const callAMethodThatTakeQueryOptions =
'CallExpression[callee.property.name=/^(invalidateQueries|fetchQuery|removeQueries|getQueriesData)$/]'
const callAMethodThatTakesAQueryKey = 'CallExpression[callee.property.name=/^(getQueryData|setQueryData)$/]'
// "Assertions" to enforce that the first argument is either `fooQueryOptions()` or `fooQueryOptions().queryKey`
const firstArgMustBeSomethingQueryOptions = '[arguments.0.callee.name!=/.+QueryOptions/]'
const firstArgMustBeSomethingQueryOptionsDotQueryKey =
':matches([arguments.0.object.callee.name!=/.+QueryOptions/],[arguments.0.property.name!="queryKey"])'
// DRY-ing up the error messages
const queryOptionsDocs = `
See: https://tanstack.com/query/v5/docs/framework/react/guides/query-options`
// Glue it all together
const queryOptionsRules = [
{
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 :(
const query = useQuery({
...groupOptions(1),
select: (data) => data.groupName,
}) It might be technically possible to cover every edge case with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Separating
queryKey
andqueryFn
can 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
ignorePatterns
An array of strings used to ignore specific query key patterns:
collections
An array of strings used to enforce the use of
queryOptions
in specific collections of queries:When Not To Use It
If you do not want to enforce the use of
queryOptions
in your codebase, you will not need this rule.Attributes
Beta Was this translation helpful? Give feedback.
All reactions