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

Fix Prisma Client error when running dev server w/out DB #524

Merged
merged 3 commits into from
May 11, 2020

Conversation

thedavidprice
Copy link
Contributor

@thedavidprice thedavidprice commented May 9, 2020

Fixes #483

By default, the Prisma Client would always run when using yarn rw dev. In the case of running yarn rw dev --app web, this would cause an error. This adds a check before running Prisma Client requiring both 1) the CLI command option including 'db' and 2) API directory existing.

Given that this command has 3 options, which can be used independently, I’m not sure if #490 was the best way forward -- the command yarn rw dev web would have started the web dev server only. The Prisma Client error would have still been an issue, however. Note: I did leave the api/ directory checks and also added it to the Prisma Client startup.

Any combination of commands or an individual command will now work, e.g. yarn rw dev api web will run the api and web servers without error.

@peterp I think it's fine as is, but it's not clear to me why 'db' exists as a separate option from 'api'. (And 'db' is a bit of a misnomer since it's running prisma generate.) But I guess if the two were combined, it wouldn't be possible for someone to swap the DB client in their own App, correct?

@cannikin for redwoodjs/redwoodjs.com#125 you might want to use the yarn rw dev --app web command. (Note: --app not required.) Or if you want to delete the API directory and just run yarn rw dev, I'd suggest you still explain what the command is doing behind the scenes -- they can optionally choose which dev servers to run. fwiw...

@thedavidprice
Copy link
Contributor Author

@peterp I've removed await generatePrismaClient, which I think is the correct way forward as it separates running the API dev-server from dependency on Prisma (e.g. in the case of an App not using a DB).

Current Behavior

If a DB is being used and the file graphql.js implements Prisma Client, an error may result when only the API dev-server is run. This seems appropriate, however (see more below).

Without the Prisma Client implementation in graphql.js, there are no errors for any configuration of commands.

with import { db }

  • if yarn prisma generate has not been run (therefore the Prisma Client does not exist) and
  • if api/src/functions/graphql.js includes import { db } from 'src/lib/db'

then

  • yarn rw dev api will throw an Error due to Prisma Client missing
  • yarn rw dev db api runs correctly because db command generates Prisma Client
  • yarn rw dev web runs correctly because no API
  • yarn rw dev runs correctly because db command generates Prisma Client

without import { db }

  • if yarn prisma generate has not been run (therefore the Prisma Client does not exist) andand
  • if import { db } from 'src/lib/db' is removed from api/src/functions/graphql.js (because there is no DB)

then everything runs correctly.

@thedavidprice thedavidprice requested a review from peterp May 10, 2020 06:16
@peterp
Copy link
Contributor

peterp commented May 10, 2020

I've removed await generatePrismaClient, which I think is the correct way forward as it separates running the API dev-server from dependency on Prisma (e.g. in the case of an App not using a DB).

The reason why it's separated is because we cannot start the dev-server if the database client isn't generated because it would throw an unhandled exception.

Basically that code was added to enable this workflow:

yarn create redwood-app path/to/app
cd path/to/app
yarn rw dev

If that's still possible then I think this can be removed, but I would love it if our golden path was the first consideration.

I think how you had it originally was fine, which was to only generate the database client if app included api (default).

@peterp peterp force-pushed the master branch 6 times, most recently from 711c520 to 2341368 Compare May 10, 2020 10:07
@thedavidprice
Copy link
Contributor Author

Yes, it runs correctly.

The tutorial commands:

yarn create redwood-app path/to/app
cd path/to/app
yarn rw dev

Successfully runs:

$ yarn rw dev
yarn run v1.22.4
$ /Users/price/Repos/xx-delete/node_modules/.bin/rw dev
$ /Users/price/Repos/xx-delete/node_modules/.bin/dev-server
$ /Users/price/Repos/xx-delete/node_modules/.bin/prisma generate --watch
$ /Users/price/Repos/xx-delete/node_modules/.bin/webpack-dev-server --config ../node_modules/@redwoodjs/core/config/webpack.development.js
19:27:31  db |
19:27:31  db | Watching... /Users/price/Repos/xx-delete/api/prisma/schema.prisma
19:27:31  db |
19:27:31  db | ✔ Generated Prisma Client to ./../node_modules/@prisma/client in 61ms
19:27:31  db |
19:27:31 web | ℹ 「wds」: Project is running at http://localhost:8910/
19:27:31 web | ℹ 「wds」: webpack output is served from /
19:27:31 web | ℹ 「wds」: Content not from webpack is served from /Users/price/Repos/xx-delete/web
19:27:31 web | ℹ 「wds」: 404s will fallback to /index.html
19:27:32 api | Listening on http://localhost:8911
19:27:32 api | Watching /Users/price/Repos/xx-delete/api
19:27:32 api |
19:27:32 api | Now serving
19:27:32 api |
19:27:32 api | ► http://localhost:8911/graphql/

Note there is no longer a ✔ Generating the Prisma client... that runs prior to the db, web, and API commands.

@thedavidprice thedavidprice requested review from peterp and removed request for peterp May 11, 2020 02:35
@peterp
Copy link
Contributor

peterp commented May 11, 2020

Nice! I guess Prisma's DB service is now working!

@thedavidprice thedavidprice merged commit 5180478 into master May 11, 2020
@thedavidprice thedavidprice deleted the dsp-fix-dev-server-prisma-error-when-no-API branch May 11, 2020 18:58
@thedavidprice thedavidprice added this to the next release milestone May 11, 2020
@jtoar
Copy link
Contributor

jtoar commented Jun 4, 2020

I'm seeing this issue come up again with new apps:

yarn create redwood-app path/to/app
cd path/to/app
yarn rw dev
...
22:31:09 api | Running at 'http://localhost:8911'
22:31:09 api | Watching files in '/home/dominic/projects/redwood/redwoodblog/api/src/functions'
22:31:11 api | /home/dominic/projects/redwood/redwoodblog/node_modules/.prisma/client/index.js:3
22:31:11 api |     throw new Error(
22:31:11 api |     ^
22:31:11 api | 
22:31:11 api | Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
22:31:11 api | In case this error is unexpected for you, please report it in https://github.com/prisma/prisma-client-js/issues/390.

@thedavidprice @peterp Is this the case for either of you guys too?

@jtoar jtoar mentioned this pull request Jun 4, 2020
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.

Check for existence of api directory when running dev and skip api and db tasks if it doesn't exist
3 participants