-
Notifications
You must be signed in to change notification settings - Fork 79
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 animal uses api to act on single resource #3396
base: integration
Are you sure you want to change the base?
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.
But wasn't it a very deliberate decision on our part with the animal endpoints to 1) move more data processing to the server (particularly to models) and 2) start returning more bespoke data for the frontend (other example being types count endpoint)? Why are we going back on that so soon?
it could get really inconsistent with what is returned
hard to write consistent APIs
Could you elaborate on what would become inconsistent?
From the perspective of consuming the query within webapp, I loved having this lookup table-like structure that was designed for this React Select use case. And of course I sooo appreciate a single network request instead of two requests for resources that have to then be merged on the frontend (historically a very problematic LF pattern...)
packages/webapp/src/containers/Animals/AddAnimals/useAnimalOptions.ts
Outdated
Show resolved
Hide resolved
There is definitely value in bespoke endpoints for specific purpose! Count is a query param, and also
For example if all get requests acting on a resource return the resources then it is trivial to set up the api and we could probably even abstract away all enum tables to just be
Why not let RTK do it like our other FE code: https://redux-toolkit.js.org/rtk-query/usage/customizing-queries#normalizing-data-with-createentityadapter
I agree! I didn't know it causes too many problems for an enum like this but I would love to improve it by using just keys on enum tables as the foreign key (see |
return { | ||
default_type_id: animalType.id, | ||
uses: typeUseRelationships | ||
.filter((typeUse) => typeUse.default_type_id === animalType.id) |
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.
Essentially we remove all of the controller logic and only have to add in this filter, and the all uses option below.
@Duncan-Brain It's still hard for me feel really enthusiastic about 220 additional lines of code (almost all boilerplate) and basically identical logic running in the client (hook) instead of on the server (animalUseModel.js), to create the same result. Is it also the first time we've put a straight It just... already worked before and the design was in the spirit of what we decided on in the Spring 🤷 But I don't feel strongly enough to put my foot down, and I particularly don't think -- given the fact that the original endpoint was working and that the data involved is not large -- that a migration or any more redesign is warranted for this given the other tasks we have to complete. So I'll give it neutral thumb; please merge as is if you guys prefer it. To address your specific comments though:
Doesn't look like it within the scope of this release which is all I was thinking (would prefer not over-anticipate). Ditto with an abstracted enum
Maybe the error was in giving this data structure the plain
I think I like that transformation pattern in general, although it doesn't address the core issue of moving logic frontend --> backend. However, this particular data structure is not the same structure as a normalized Redux entity because we'd want the lookup keys to be |
@kathyavini no worries -- I don't expect it to be merged -- we were just fixing logic to avoid a type error by type casting a string from a string key to make it a number -- and so removing the additional logic and following the reusable code pattern seemed to make more sense to me. If Sayaka doesn't like it that's okay ! I am down to explore what should be moved from frontend to backend but I am not sure that is good practice to make an endpoint for a react select or everything unless it is something the backend is particularly good at like counting or filtering.
I am also very anti-lines of code haha -- but boilerplate I don't mind at all because if enough of it builds up someone will OOP the model, OOP the controller, OOP the test, and its gone! To me I think the core problem for me was yes most about the get endpoint yes. But exploring this idea further it feels code smelly not REST-ful not that we must be strict about it. |
Sorry for jumping in late! When I first created this API, I was unsure whether it was common practice to create a controller for a join table. Sorry to bring up a completely different idea, but it seems more natural to achieve what I wanted by joining |
@SayakaOno I think you make a great point that since I think it makes sense to just I am interested in flat vs nested data. The problems I remember seeing on frontend with keeping store updated was when things were nested like tasks had nested management plans and management plans had nested tasks and keeping those two in sync was a problem. Anyways if you think its valuable to keep the new TLDR: If I am dumb for even thinking about this please close PR 😂 |
@Duncan-Brain My preferred approach would be For the nesting structure, I think we could invalid the API to trigger a refetch, so it should be fine with RTK query...? I'm interested in how we could properly create an API that returns pre-cooked data! |
Possible related other solution suggested by Anto is to replace enum ids with enum key on backend so that frontend doesn't use enum ids. I like that solution since it is a half step towards the enum key table. Even if my second half is never implemented, that suggestion is a solid way to make frontend id agnostic moving forward for most other endpoints. So that would be cool! So for this discussion I think what that would look like is that the
If we find time after priorities we could test one of those? |
Description
Originally branched from: #3391
Uses the normal endpoint structure of returning a single unformatted resource for animal use and animal type use relationships.
If we start formatting data on the backend
Other options than formatting to specific use cases and to use less resources on the frontend:
nomination
andnomination_type
table) as key is provided in every row without joining to get it.Jira link:
Type of change
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Checklist: