Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Migrates to using react-router 6.4 and react-query to manage asynchronous state #22

Merged
merged 36 commits into from
Nov 8, 2022

Conversation

devraj
Copy link
Member

@devraj devraj commented Oct 30, 2022

Our current approach has been to use autorest or otc to generate the client with great naming convention and then use Context or props to manage the state of the data. The approach is similar to what the team had been doing earlier (where they had made their own hooks to manage states). The problem becomes rather large, very quickly with problems like:

  • Caching... (possibly the hardest thing to do in programming)
  • Deduping multiple requests for the same data into a single request
  • Updating "out of date" data in the background
  • Too many states to manage the interfaces

What started as an investigation in #21 to find a replacement for OTC as the project seems to be abandoned and finding orval as a suitable replacement turned into outlining how we manage state properly.

React Router 6.4 adds data loading, and orval is able to generate an API client that is compatible with react-query, the following pull request provides a refactored template of how to query the API using react-query and react-router while still maintaining beautifully readable code.

The key points of these changes are:

  • react-router to trigger loading queries when the routes are accessed
  • react-query to store and manage the asynchronous state
  • orval to generate the API client
  • restructures where the api client is stored
  • removes unwanted dependencies
  • thoroughly documents the entire pattern

this outlines how we are going to drop the use of autorest and otc and head towards
generating clients using orval and then use react-query and react-router's new loading
features to maintain application state.
@devraj devraj self-assigned this Oct 30, 2022
@devraj devraj added the breaking this will change everything again label Oct 30, 2022
drops the use of otc and autorest api calls, moving towards using react-query
and orval to generate the api client that will be used by react-query
sets out the paradigm for end to end testing using playwright including
a github action
react-router 6.4 completely changes the way we configure the routes and introduces loaders
and actions, this is also paired with react-query and it's developer tools, we will conduct
the exercise of migrating to the new paradigm and make the two libraries work with each other
orval can read from a web server but can't ignore tls warnings, which is raised
with the development server, the endpoint is now configured to read straight from
the docker container which is running http not https
we've decided to move to snake casing for package names, inspired by all that
is published on npm.

for components the files are snake cased with the components in pascal case,
this also sings to our unix way of life
this commit gets the editing of a user profile working with react-query and
react-router, there are still a few questions around the use of the useGetUserById
hook as opposed to useLoaderData which is what the examples show.

note that we use orval to generate the client that provides these hooks, it will
be a matter of cross referencing the documentation and making our official
note as to how to use the three frameworks together
note that the forms have no validation at the moment, still to figure out how
to do this with the react-router v6 way.

i am still debating if this is the right way of doing things, and if we should
be able to reuse the edit form, however depending on the ui this can be
different per application
delete event has been wired up, it differs from the example where by the id
has been passed in the url, and the sample does not require to do this

post this commit we will merge the branch in and leave notes on the ticket
to explore the edge cases and then document what we require
@devraj devraj marked this pull request as ready for review November 8, 2022 02:19
@devraj devraj merged commit e1c6d58 into master Nov 8, 2022
@devraj devraj deleted the react-query branch November 8, 2022 02:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking this will change everything again
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant