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

[UI v2] poc: Re-organize variable queries and mutations using queryOptions factory pattern #16155

Closed
wants to merge 3 commits into from

Conversation

devinvillarosa
Copy link
Contributor

@devinvillarosa devinvillarosa commented Nov 29, 2024

What does this PR do and why?

⚠️ Proof of concept ⚠️
This PR expands on the current variables query key factory pattern, but moves it using queryOptions. Additionally, while using queryOptions, this moves queryKeys to be associated with queryFn.

Separating QueryKey from QueryFunction was a mistake

Blog for a good read on why we should associate the two together

Notable changes:

  1. Adds @tanstack/eslint-plugin-query as a dev dependency. This eslint rule checks if variables passed in the queryFn is also passed as a queryKey
  2. Abstract queryFn and mutationFn into its own function wrapper. Purpose is to separate concerns from APIs and queries 🧅. Additionally most @TanStack docs follow a similar pattern
  3. 🏗️ Re-organize the query key structure (similar to how the blog post ☝️ suggests)
    Before
    Screenshot 2024-11-29 at 8 53 50 AM
    After
    Screenshot 2024-11-28 at 2 55 08 PM
  4. Update mutation docs now that UX callbacks has been updated from a previous PR [UI v2] Move delete variable mutation to variable hooks module #16141

Checklist

  • This pull request references any related issue by including "closes <link to issue>"
    • If no issue exists and your change is not a small fix, please create an issue first.
  • If this pull request adds new functionality, it includes unit tests that cover the changes
  • If this pull request removes docs files, it includes redirect settings in mint.json.
  • If this pull request adds functions or classes, it includes helpful docstrings.

Related to #15512

@devinvillarosa devinvillarosa added development Tech debt, refactors, CI, tests, and other related work. ui Related to the Prefect web interface labels Nov 29, 2024
@github-actions github-actions bot added the enhancement An improvement of an existing feature label Nov 29, 2024
@devinvillarosa devinvillarosa marked this pull request as ready for review November 29, 2024 17:07
@devinvillarosa devinvillarosa marked this pull request as draft November 29, 2024 18:02
@devinvillarosa devinvillarosa force-pushed the query-options-variables branch 3 times, most recently from 4fa2cf9 to 6a82e26 Compare November 29, 2024 18:26
@devinvillarosa devinvillarosa marked this pull request as ready for review November 29, 2024 18:30
placeholderData: keepPreviousData,
}),
counts: () => [...variableQueries.all(), "count"] as const,
count: (variables?: Variables) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated the argument here so that it is specifically looking for a variables type to be passed.
Changes are reflected down in L143 and L191

Copy link
Member

@desertaxle desertaxle left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time to think through organization structures for this module! Right now, I don't think that a query factory like the one shown in the linked blog post is what I would choose. I would prefer to have a query key factories and query options factories separate but closely located in the same module, but I'm happy to discuss further!

@@ -11,82 +11,91 @@ import {

type UseVariablesOptions =
components["schemas"]["Body_read_variables_variables_filter_post"];
type Variables = UseVariablesOptions["variables"];
Copy link
Member

Choose a reason for hiding this comment

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

We can use the existing schema from the type generation to avoid coupling between the types.

Suggested change
type Variables = UseVariablesOptions["variables"];
type VariablesFilter = components["schemas"]["VariableFilter"];

ui-v2/eslint.config.js Outdated Show resolved Hide resolved
ui-v2/src/hooks/variables.ts Outdated Show resolved Hide resolved
Comment on lines 91 to 96
return queryOptions({
queryKey: [...variableQueries.counts(), body] as const,
queryFn: () => fetchVariableCountRequest(body),
staleTime: 1000,
placeholderData: keepPreviousData,
});
Copy link
Member

Choose a reason for hiding this comment

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

We want to ensure that we aren't including the body in the query key variables is undefined to avoid making duplicate variable count calls when the variables page first mounts.

staleTime: 1000,
placeholderData: keepPreviousData,
});
const variableQueries = {
Copy link
Member

Choose a reason for hiding this comment

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

It strikes me as odd that some of these functions return a query key while others return a full queryOptions object. The heterogenous return types make me think these should be grouped differently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya, might need to go back to the drawing board on this one.
It is a bit strange that the objects are inconsistent, but I do agree with the blog on how the Dev experience much better when they're coupled together. 🤔

@devinvillarosa devinvillarosa marked this pull request as draft November 29, 2024 21:34
@devinvillarosa
Copy link
Contributor Author

Thanks for taking the time to think through organization structures for this module! Right now, I don't think that a query factory like the one shown in the linked blog post is what I would choose. I would prefer to have a query key factories and query options factories separate but closely located in the same module, but I'm happy to discuss further!

np! it's just a proof of concept.

Not too attached to any decisions.
But I think I'll close this PR and just add the eslint rule for tanstack query. IMO its pretty useful to have

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development Tech debt, refactors, CI, tests, and other related work. enhancement An improvement of an existing feature ui Related to the Prefect web interface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants