Skip to content
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

add tRPC #614

Merged
merged 48 commits into from Sep 27, 2021
Merged

add tRPC #614

merged 48 commits into from Sep 27, 2021

Conversation

KATT
Copy link
Contributor

@KATT KATT commented Sep 9, 2021

This PR adds tRPC which automatically infers the types from your API-function into the frontend.

As a reference I rewrote /bookings + /availability-pages

  • /bookings-page and Shell to use trpc.useQuery() rather than custom fetch hook and getServerSideProps
  • The way to rewrite is basically to cut+pasted getServerSideProps into a function in routes

Benefits

  • Makes first load of "skeleton" edge-cachable so it feels more snappy
  • When navigating between pages it won't require a slow full refetch
  • Will be less flaky and fetch errors are automatically retried
  • Automatic type safety all the way from DB to frontend when doing react-query calls
  • "Refetch on focus" - so when re-opening the page it will automatically refresh all underlying data

Extra fixes on this branch

  • Fixed callbackUrl on login so it redirects to the right page after signing in

How to test it

  • Checkout this branch
  • Run yarn dx to start

Next steps

#615 depends on this branch (will make everything lightning fast ⚡️)

More info

for more info on tRPC have a look at https://trpc.io or for a deep-dive look at https://www.youtube.com/watch?v=-_GZJ3xwYOw

feel free to ping me on Slack - maybe I should make a loom?

@KATT KATT added the 🚧 wip / in the making This is currently being worked on label Sep 9, 2021
@vercel
Copy link

vercel bot commented Sep 9, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/cal/calendso/G7paS6JjV9W3rAsQsMoutExWi7sQ
✅ Preview: https://calendso-git-feature-trpc-cal.vercel.app

@vercel vercel bot temporarily deployed to Preview – staging September 9, 2021 08:18 Inactive
@vercel vercel bot temporarily deployed to Preview – calendso September 9, 2021 08:18 Inactive
@vercel vercel bot temporarily deployed to Preview – calendso September 9, 2021 08:20 Inactive
@vercel vercel bot temporarily deployed to Preview – staging September 9, 2021 08:20 Inactive
@vercel vercel bot temporarily deployed to Preview – calendso September 9, 2021 08:24 Inactive
@vercel vercel bot temporarily deployed to Preview – staging September 9, 2021 08:24 Inactive
@KATT KATT self-assigned this Sep 9, 2021
@vercel vercel bot temporarily deployed to Preview – calendso September 9, 2021 08:45 Inactive
@vercel vercel bot temporarily deployed to Preview – staging September 9, 2021 08:45 Inactive
@vercel vercel bot temporarily deployed to Preview – calendso September 9, 2021 08:46 Inactive
@vercel vercel bot temporarily deployed to Preview – staging September 9, 2021 08:46 Inactive
@vercel vercel bot temporarily deployed to Preview – staging September 9, 2021 09:05 Inactive
@vercel vercel bot temporarily deployed to Preview – calendso September 9, 2021 09:05 Inactive
This reverts commit e6f2d25.
@vercel vercel bot temporarily deployed to Preview – calendso September 9, 2021 09:22 Inactive
@vercel vercel bot temporarily deployed to Preview – staging September 9, 2021 09:22 Inactive
pages/[user].tsx Outdated Show resolved Hide resolved
# Conflicts:
#	pages/[user].tsx
#	prisma/schema.prisma
#	tsconfig.json
#	yarn.lock
@vercel vercel bot temporarily deployed to Preview – staging September 14, 2021 09:07 Inactive
@vercel vercel bot temporarily deployed to Preview – calendso September 14, 2021 09:07 Inactive
@vercel vercel bot temporarily deployed to Preview September 24, 2021 10:12 Inactive
@vercel vercel bot temporarily deployed to Preview September 24, 2021 10:41 Inactive
# Conflicts:
#	lib/app-providers.tsx
#	pages/_app.tsx
#	pages/availability/index.tsx
#	pages/bookings/index.tsx
@KATT KATT marked this pull request as draft September 24, 2021 11:35
@KATT

This comment has been minimized.

@vercel vercel bot temporarily deployed to Preview September 24, 2021 11:36 Inactive
@KATT KATT marked this pull request as ready for review September 24, 2021 11:44
@vercel vercel bot temporarily deployed to Preview September 24, 2021 11:45 Inactive
@vercel vercel bot temporarily deployed to Preview September 24, 2021 12:53 Inactive
@vercel vercel bot temporarily deployed to Preview September 24, 2021 15:04 Inactive
Copy link
Contributor

@baileypumfleet baileypumfleet left a comment

Choose a reason for hiding this comment

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

Haven't tested this locally but the code looks great :)

const bookings = bookingsQuery.reverse().map((booking) => {
return {
...booking,
startTime: booking.startTime.toISOString(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary (tRPC has issues dealing with Date/Dayjs objects?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just copy-pasted what was already in getServerSideProps.

In tRPC we could actually just use a Date-object if we add superjson.

Ref: https://trpc.io/docs/data-transformers

},
});
})
.query("me", {
Copy link
Contributor

Choose a reason for hiding this comment

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

How should we go about splitting these into files?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same way we do on the app/root router to separate viewer in it's own file

https://github.com/calendso/calendso/blob/fb10bea04a0747e8abe005591b2780939c2da676/server/routers/_app.ts#L13-L24

One can either do

export const myRouter = createRouter()
  .merge("sub.", subRouter); // <-- prefixes all procedures with `sub.`

Or

export const myRouter = createRouter()
  .merge(subRouter); // <-- does not prefix any procedures

And this works on the viewerRouter too, so if that file got insanely big or if we wanted to do distinct parts separately, they could be handled in their own file

# Conflicts:
#	lib/app-providers.tsx
#	yarn.lock
@vercel vercel bot temporarily deployed to Preview September 27, 2021 14:40 Inactive
@vercel vercel bot temporarily deployed to Preview September 27, 2021 14:43 Inactive
@vercel vercel bot temporarily deployed to Preview September 27, 2021 14:46 Inactive
@baileypumfleet baileypumfleet merged commit 3430065 into main Sep 27, 2021
@baileypumfleet baileypumfleet deleted the feature/trpc branch September 27, 2021 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
♻️ autoupdate tells kodiak to keep this branch up-to-date
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants