-
Notifications
You must be signed in to change notification settings - Fork 15
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
fix: [DHIS2] Type generic T = QueryResult to useDataQuery #1297
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me @eirikhaugstulen! (At least as a temporary solution until we strongly type the query result)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @eirikhaugstulen, this is super useful and looks good to me! My one concern is that, as @JoakimSM mentioned, this might be a temporary fix until we have strong types for query responses (generated from the new OpenAPI spec) but that could still be a little way off. If we do get to that point this will no longer be a generic, so it will be a breaking change for the app-runtime
consumer. I think that's fine though (a codemod could automatically strip the generic type on upgrade) and worth it if we can get better type safety in the interim.
Let's let someone from @dhis2/team-platform comment here as well before we merge
Should we also fix the JsonMap default type so that it supports nested objects...? |
TypeScript novice here.... I'm just wondering if we were to fix the |
for this point, I don't totally get why it will be a breaking change, can't this still be a generic when we have strong types? .. the client would still be using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM .. just a suggestion + question to clarify things a bit for me
@@ -40,15 +40,16 @@ export interface ExecuteHookResult<ReturnType> { | |||
data?: ReturnType | |||
} | |||
|
|||
export interface QueryState { | |||
export interface QueryState<TQueryResult> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would there be a benefit to restrict TQueryResult
a bit to extend an interface, i.e.
export interface QueryState<TQueryResult> { | |
interface IQueryResult { | |
id: string; // properties that are always in a response. | |
} | |
export interface QueryState<TQueryResult extends IQueryResponse> { | |
called: boolean | |
loading: boolean | |
fetching: boolean | |
error?: FetchError | |
data?: TQueryResult | |
} |
the concrete API responses - when they're added - should then adhere to IQueryResult
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that some extensibility can be done to this, and I thought about making something similar, although I was a bit unsure as to how me make this generic enough. What happens if the user is filtering out some of these with the filter-property for example?
I think we should definitively do something about the JSONMap-type that we return. As it stands now, it is more annoying to the end user than helpful - at least that's my experience using it. I would argue that as a first iteration, we should type what we know about the return and anything other than that should be any or unknown. We know we return an object with strings as keys, and that's pretty much it. We should also encourage the user to pass in a Therefore, in my opinion, the data returned from the hook should be Edit: |
Btw. is it only me that think |
@eirikhaugstulen looking at this PR again - do you think we need to address any of the pending questions before merging? I think it's a big improvement as it is (we need it in a new app using TS) and we can address the rest later, if there is a need. |
ok, if no one objects then I will merge this one (since it's needed and already had few approvals) then we can discuss the pending questions and enhancements from @Birkbjo PR afterwards. |
fbcbaa9
to
f8bb081
Compare
## [3.9.2](v3.9.1...v3.9.2) (2023-05-02) ### Bug Fixes * [DHIS2] Type generic T = QueryResult to useDataQuery ([#1297](#1297)) ([7c5c083](7c5c083))
🎉 This PR is included in version 3.9.2 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
* fix: added generic T = QueryResult * fix: changed to correct import * fix: prettier * fix: changed name of T * fix: prettier
## [3.9.2](v3.9.1...v3.9.2) (2023-05-02) ### Bug Fixes * [DHIS2] Type generic T = QueryResult to useDataQuery ([#1297](#1297)) ([7c5c083](7c5c083))
# [3.10.0-alpha.3](v3.10.0-alpha.2...v3.10.0-alpha.3) (2023-08-22) ### Bug Fixes * **connection-status:** responsiveness to online events [LIBS-497] ([#1348](#1348)) ([91a3d4d](91a3d4d)) * **types:** add generic result type to oncomplete param ([#1350](#1350)) ([a069603](a069603)) * [DHIS2] Type generic T = QueryResult to useDataQuery ([#1297](#1297)) ([7c5c083](7c5c083)) * account for daylight savings time [LIBS-490] ([06eaa5d](06eaa5d)) * account for daylight savings time [LIBS-490] [#1345](#1345) ([fb00533](fb00533)) * add test for when time zones are the same [LIBS-490] ([7911f8b](7911f8b))
🎉 This PR is included in version 3.10.0-alpha.3 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Hey all,
We have a use-case in capture that I think multiple projects need as well:
We need to be able to type the
data
-object returned from theuseDataQuery
-hook. This is because the JsonMap type that is currently returned would error on almost any nested object - without giving any type safety.I have written a quick example in this PR, which I expect would be very wrong, but it gives you an idea of what would be a solution for us. To summarize, I've just added a generic T and mapped this to the QueryResult type. For the end user, typing is still optional.
This would allow us to type the data returned like this:
Open for any suggestions and discussions!