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

Add tanstack query #6

Merged
merged 11 commits into from
Oct 28, 2022
Merged

Add tanstack query #6

merged 11 commits into from
Oct 28, 2022

Conversation

Kongkille
Copy link

@Kongkille Kongkille commented Oct 27, 2022

Issue

https://linear.app/electricitymaps/issue/ELE-1315/set-up-tanstack-query-to-fetch-data-from-the-app-backend

Description

This sets up simple authenticated routes with tanstack query.

Some notes

  • Refetch interval is pretty basic for now, just querying every 5 minutes. This means we're probably hitting it a bit harder than we really need to. Ideally we would invalidate queries when we get new flowtraced data but we don't really know that unless we set up some kind of job for that.
  • I started adding some types, it's very incomplete and I don't know best practices so feel free to change those
  • I installed tiny-invariant as I saw some people use it as a nice way of checking for undefined values, especially in cases where it really shouldn't be null.

@Kongkille Kongkille marked this pull request as ready for review October 27, 2022 14:07
Copy link

@tonypls tonypls left a comment

Choose a reason for hiding this comment

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

Big things!!!

This is how I've structured the queries in the past and it's been really nice to have minimal query config in the components.

There's a little bit of error handling added in that is mostly for illustration purposes.

If we called more than two apis I would suggest making a api class that has the error handling then we could pass ()=>api.get(path) into useQuery but that's maybe overkill for the limited api's we call.

web/src/api/helpers.ts Show resolved Hide resolved
web/src/api/getState.ts Outdated Show resolved Hide resolved
web/src/api/getState.ts Outdated Show resolved Hide resolved
web/src/pages/Map.tsx Outdated Show resolved Hide resolved
web/src/types.ts Show resolved Hide resolved
Copy link
Member

@madsnedergaard madsnedergaard left a comment

Choose a reason for hiding this comment

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

Nice! Some comments :)

web/src/api/helpers.ts Show resolved Hide resolved
web/src/api/helpers.ts Show resolved Hide resolved
web/src/main.tsx Outdated Show resolved Hide resolved
web/src/api/helpers.ts Outdated Show resolved Hide resolved
web/src/pages/ZonePanel.tsx Outdated Show resolved Hide resolved
web/src/types.ts Show resolved Hide resolved
web/src/types.ts Show resolved Hide resolved
web/package.json Show resolved Hide resolved
@Kongkille
Copy link
Author

FYI I added tanstack developer tools as well

Copy link

@tonypls tonypls left a comment

Choose a reason for hiding this comment

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

I think this is great start and anything we are still discussing we can incrementally improve as we build

@Kongkille Kongkille merged commit 83a7691 into master Oct 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants