Example Actix Web server with Prisma (ORM) and GraphQL
The code is organized around a feature folder architecture, with GraphQL operations such as queries, mutations, and subscriptions, inside of each folder. Put whatever other logic is needed inside as well.
- Create a database. We will use Postgres as an example, with a username and
password of
postgresand a database name ofprisma_rust_client_example.
sudo -u postgres psql -c 'create database prisma_rust_client_example;'- Add the database URL to
prisma/schema.prisma(referenced as simplyschema.prismahereafter).
...
datasource database {
provider = "postgresql" // If you decide to use a different database, change the `provider` here
url = "postgresql://postgres:postgres@localhost:5432/prisma_rust_client_example"
}
...-
Run the following commands to initialize Prisma and set up the database:
cargo prisma generate: This will create thesrc/prisma.rsfile which is code generated from reading theschema.prismafilecargo prisma migrate reset: This will seed the database and create the necessary tables- (Optional)
npx prisma studio: This brings up Prisma Studio, a GUI where you can add and remove rows in the database without needing to do it in the code itself. This requires having NodeJS installed on your machine, otherwisenpxwill fail.
-
Run
cargo runto start the server. -
Navigate to
http://localhost:8000to see GraphiQL where you can test out GraphQL operations.