Open clemens-msupply opened 4 months ago
Related #2528
One issue - We can't easily navigate from a hook to it's usages or back...
For example if I'm trying to find where useRepack is used. And click find references I get this...
I then have to search for useRepack
Then see that it's part of get
so do a new search...
Why not just directly import the useRepack
Hook?
E.g.
One level to jump from the component to the hook...
If you need to use multiple things under the API isn't that just multiple imports? Seems like a small price to pay for easier code navigation...
With this approach you can still have all the folder structure for organisation without having to go through the extra layers unless you want too?
I think for @CarlosNZ suggestion of combining the tanstack logic with the draft state needed for a component, it might make sense to put that hook next to the component it's needed for. Obviously if it's needed it more than one place it could be different, but I would have thought that draft state components are probably mostly specific to a particular component?
My suggestions here... https://github.com/msupply-foundation/open-msupply/compare/api-hook-refactor
Refinement, moving milestones
The suggestion
Currently, it takes quite a bit of boilerblate till you can start using an API, e.g. for patient api:
1) write graphql query and generate api 2) add generated endpoint to the
api.ts
file 3) add key to theusePatientApi()
utils hook 4) add tanquery hook that calls the endpoitn fromapi.ts
using the key fromusePatientApi()
5) add the tanquery hook toindex.ts
file and to the Document and to theusePatient()
Ideally there should only be the first step, i.e. define your query, genereate the API and you are ready to go.
Thats what tanstack says about it now: https://tanstack.com/query/v4/docs/framework/react/graphql https://the-guild.dev/graphql/codegen/docs/guides/react-vue
Another one to consider: https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-react-query This one hasn't any usage examples, so a bit hard to say if it does what we want...
If that doesn't work for use we should at least simplify steps 2-5 to a minimum.
Refinement 20-06
We have an example now in the stock api, and in assets.
How do we move forward ?
Can slowly, as we touch the area, refactor to use simpler hook design. this can be staged, perhaps making navigation to the hook easier, as per @jmbrunskill suggestion making hook navigation more direct with click through, and then expand further functionality to use the simplified hooks (i.e. url parameters, row selection, etc..). Change to be summarised here in this issue
James also suggested there must be a way to see what the existing 'best' patterns are, maybe through wiki pointing to the code. And perhaps this issue can be closed at that stage