Skip to content

Support serverless deployment#11

Merged
cpojer merged 2 commits into
nkzw-tech:mainfrom
yangshun:serverless
Nov 2, 2025
Merged

Support serverless deployment#11
cpojer merged 2 commits into
nkzw-tech:mainfrom
yangshun:serverless

Conversation

@yangshun

@yangshun yangshun commented Oct 26, 2025

Copy link
Copy Markdown
Contributor

Main changes necessary to support deployment to Vercel:

  1. Put local devserver code within process.env.npm_lifecycle_event === 'dev' block so they don't run in a serverless function environment
  2. Add engineType = "client" to schema.prisma as this mode doesn't require the engine binaries, but you'll need to use a pg driver adapter. Docs: https://www.prisma.io/docs/orm/prisma-client/deployment/serverless/deploy-to-vercel
  3. Use tsbuild to build the app into a single file. Without building, there are weird TypeScript errors within @pothos-core when Vercel tries to run the raw entry file. Select dist as the output directory in the Vercel project
  4. Add dmmf: getDatamodel() to prisma options when initializing the Pothos SchemaBuilder. I believe this is needed because of the engineType = "client" change
  5. Add pnpm dev:setup as a postinstall command so that the generated files will be present
  6. As of now, Vercel does not support Node 24 yet, so lower the requirement to Node 22

While this PR works, it contains quite a bit of changes and you might have opinions on how they're done, so feel free to use it as a reference and implement it the way you like.

@yangshun yangshun changed the title Demo: how to support deployment to Vercel Demo: support serverless deployment (e.g. Vercel) Oct 26, 2025
Comment thread src/graphql/builder.tsx
],
prisma: {
client: prisma,
dmmf: getDatamodel(),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I believe this is safe to add regardless of deployment runtime. cc @hayes correct me if I'm wrong

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What does this do?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

According to https://pothos-graphql.dev/docs/plugins/prisma/setup#set-up-the-builder:

This give pothos information about your tables, relations, and indexes to help it generate optimal queries at runtime. This used to be attached to the prisma client, but has been removed in most runtimes/modes to reduce bundle size.

Comment thread src/prisma/prisma.tsx
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from './prisma-client/client.ts';

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread src/prisma/schema.prisma
importFileExtension = "ts"
output = "./prisma-client"
provider = "prisma-client"
engineType = "client"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread src/index.tsx Outdated
Comment on lines 47 to 57
if (process.env.npm_lifecycle_event === 'dev') {
try {
await prisma.$connect();
} catch (error) {
// eslint-disable-next-line no-console
console.error(
`${styleText(['red', 'bold'], 'Prisma Database Connection Error')}\n`,
error,
);
process.exit(1);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Interesting! This is actually purely for prod since you want to fail fast when the db is not available in any other hosting scenario.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see. Lemme move this out.

@cpojer

cpojer commented Oct 28, 2025

Copy link
Copy Markdown
Member

This is pretty great! I'm inclined to merge this (and would appreciate any details about deployment in the README too, since it's something people have asked for).

I'm a bit scared about switching to the new Prisma engine since when it first launched it was super broken. I have not migrated any projects to it yet, but I assume it is stable enough for new projects?

@yangshun

yangshun commented Oct 28, 2025

Copy link
Copy Markdown
Contributor Author

This is pretty great! I'm inclined to merge this (and would appreciate any details about deployment in the README too, since it's something people have asked for).

Sure, will add some guidance around deployment in the README

I'm a bit scared about switching to the new Prisma engine since when it first launched it was super broken. I have not migrated any projects to it yet, but I assume it is stable enough for new projects?

I haven't used the new engine for any serious projects yet, but it works for this repo at least (tested live on Vercel) 🤣

@yangshun
yangshun marked this pull request as draft October 28, 2025 02:38
@yangshun
yangshun marked this pull request as ready for review November 2, 2025 02:50
@yangshun yangshun changed the title Demo: support serverless deployment (e.g. Vercel) Support serverless deployment Nov 2, 2025
@yangshun

yangshun commented Nov 2, 2025

Copy link
Copy Markdown
Contributor Author

Updated the PR

@cpojer cpojer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Awesome, thank you so much!

@cpojer
cpojer merged commit abd8da8 into nkzw-tech:main Nov 2, 2025
1 check passed
@yangshun
yangshun deleted the serverless branch November 2, 2025 11:55
@cpojer

cpojer commented Nov 2, 2025

Copy link
Copy Markdown
Member

I separated the app server from dev server here: 3a37572

@yangshun

yangshun commented Nov 2, 2025

Copy link
Copy Markdown
Contributor Author

I separated the app server from dev server here: 3a37572

Got it, it works well. I tested by deploying the latest commit here (you need to log in first)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants