A GraphQL backend using nexus/schema, apollo-server, and prisma 2
An example GraphQL Backend using Prisma2, Apollo Server, nexus/schema, and using SQLite.
This will be the first workflow one has to go through to initially setup the repo for further development.
After cloning the repo:
Decide what data source you want to use. If you want to use SQLite leave the prisma/schema.prisma
file as is. However if you wish to change the data source refer to https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/data-sources
An example data source for postgresql
would be:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Make sure to create a .env
file located at prisma/.env
that includes DATABASE_URL=some_database_url
.
For SQLite this URL will be a file path, for example DATABASE_URL=file:./dev.db
.
Once you've set up your data source:
npm i
to install depsnpm run migration:run
to create database.env
in the root of the project and add an APP_SECRET=app_secret_goes_here
to itnpm run prisma:seed
to seed initial datapostinstall
script ran, if it didn't run npm run generate
This is the workflow one would go though to change the data model from it's current state.
After changing the prisma/schema.prisma
file:
npm run migration:save
to create the migration filenpm run migration:run
to run the migration and alter the DBnpm run generate
to generate the new prisma clientThis workflow enables you to locally view and edit the database with a GUI.
After at least workflow 1 is completed successfully and running all migrations:
npm run prisma:studio
to open prisma studio and view/edit dataThis is the workflow one uses to start the GraphQL server and connect it to the database.
To start the server for local development after at least workflow 1 is completed successfully:
npm run start:dev
to start the server in watch modeTo start the dist server after at least workflow 1 is completed successfully:
npm run build
to compile the typescript and put it in distnpm run start
to start the serverThis workflow details how to re-generate the TypeScript definitions and GraphQL schema SDL after changing the schema from it's current state.
After at least worflows 1 and 4 are completed successfully:
src/schema.ts
npm run generate
to generate new GraphQL types and schemaAfter at least workflows 1 and 4 are completed successfully:
npm run build
to generate the dist codenpm run start
to start the dist codeAll GraphQL Queries and Mutations except for login
and singup
are protected by authentication.
To login after at least workflows 1 and 4 are completed successfully:
login
mutation with email
and password
Authorization: Bearer <token_here>
, if you're using the GraphQL playground it expects an object with the format { 'Authorization': 'Bearer <token_here>' }
To signup after at least workflows 1 and 4 are completed successfully:
signup
mutation with at least email
and password
Authorization: Bearer <token_here>
, if you're using the GraphQL playground it expects an object with the format { 'Authorization': 'Bearer <token_here>' }
prisma/schema.prisma
- acts as the DB schema that also includes the connection details. Editing this and then creating and running migrations is the primary way to change the DB.prisma/migrations
- contains the code and documentation for all of the migrations the DB needs to be up to date.prisma/seed.ts
- contains the code to seed the DB with test data.src/schema.ts
- contains the code describing the GraphQL schema, it contains Entity definitions, Query and Mutation definitions, along with the resolvers for all fields. It uses nexus/schema and nexus-plugin-prisma to integrate the types and resolvers that prisma provides.src/context.ts
- sets up the apollo context object so you can use prisma queries and mutations inside of resolvers.src/server.ts
- sets up the apollo server and runs itsrc/generated/nexus.ts
- contains the auto-generated TypeScript types from nexus/schemaschema.graphql
- contains the auto-generated GraphQL SDL from nexus/schemasrc/permissions/index.ts
- contains the definition of the rules that control permissions, and applies those rules to fields on the GraphQL schemasrc/utils/auth/password.ts
- utilities to hash and compare passwordssrc/utils/auth/token.ts
- utilities to sign and verify JWT tokenssrc/utils/auth/getUserId.ts
- utility to take an incoming auth header and return the verified userId from the JWTHeroku deployment using Postgres as the database.
Procfile
to the root of the project:release: npm run migration:run:up
web: npm run start
Will run migrations after the app builds and then start the web server.
Config Vars
section in the app settings
APP_SECRET
(required)DATABASE_URL
(required)GRAPHQL_INTROSPECTION
(optional)GRAPHQL_PLAYGROUND
(optional)NODE_MODULES_CACHE
:false
to the Config Vars
, this is because the prisma client should not be cachedweb
process in the resources
tab of the app./prisma/dev.db
, and re-running npm run migration:run
and npm run prisma:seed
.node_modules
using rm -rf node_modules
when at the root of the projectnpm i