Skip to content

Latest commit

 

History

History

protocol-api

Protocol API

Summary

This is the nexus of the project where the state of the app is stored. This contains two important components:

Prisma

Prisma is open-source ORM. In this project. It's uses postgres as data source; Prisma manage and create the govrn Database cluster for you. In addtion, Prisma has a performant query engine that is implemented in Rust.

You can find all configuration under the schema.prisma file. Schema Docs

Prisma makes it easier for 2 main jobs:

Database

This Diagram shows the general relationships among all tables in postgres DB. tables-diagram

Express

Express creates a GraphQL server with many useful middlewares that manage session & permissions. Most other applications in Govrn monorepo is dependent on it. In many application, you are going to need PROTOCOL_URL for GraphQL: PROTOCOL_URL="http://localhost:4000/graphql"

You can find more about the configurations and middleware under main.ts file.

Postgres

Install postgresql@13 on your OS. It's required by Prisma to operate. Here's a gist for macOS.

  • Install postgresql@13 binaries.

  • Define a new PostgreSQL user account using createuser:

    createuser --interactive --pwprompt

  • To integrate postgresql into the project, you need the DATABASE_URL that references the Database Cluster on your OS. DATABASE_URL=postgresql://<username>:<password>@localhost:5432/govrn

  • Finally, run yarn nx run protocol-api:migrate. This will create PostgreSQL database cluster called govrn Your database is now in sync with prisma schema @ apps/protocol-api/prisma/schema.prisma.

  • You can access the govrn database cluster using psql govrn or using pgcli for auto-completion.

Nx Plugin

This App is generated using @nrwl/node:application. Check all targets at project.json

Commands

  • yarn nx run protocol-api:serve: start a GraphQL server at http://localhost:4000/graphql.
  • yarn nx run protocol-api:generate: runs prisma generate command, generating artifacts (e.g. PrismaClient).1
  • yarn nx run protocol-api:migrate: runs prisma migrate command, migrating and creating the database if it does not exist.1
  • yarn nx run contract-sync-job:lint

.env

  • PROTOCOL_COOKIE_SECRET: secret A string which will be used as single key. You can use any string, ex: PROTOCOL_COOKIE_SECRET="truculent Falafel"
  • CORS_ORIGIN: A comma separated list of allowed origins. Locally it would be localhost:3000.
  • KEVIN_MALONE_TOKEN: Authorization token for GraphQL requests.2
  • LINEAR_REDIRECT_URI: The redirect URI used by the frontend when authenticating a user via oAuth. It should look similar to http://localhost:4000/linear/oauth
  • LINEAR_CLIENT_ID: The client id of our linear app
  • LINEAR_CLIENT_SECRET: The client secret of our linear app
  • DISCORD_REDIRECT_URI: e.g. http://localhost:4000/discord/oauth for local development.
  • DISCORD_CLIENT_ID: The client ID of Discord app (Docs)
  • DISCORD_CLIENT_SECRET: Can be generated by creating an app on the Discord app docs (Docs)
  • PROTOCOL_FRONTEND: The url for our protocol frontend. It should look similar to http://localhost:3000
  • LINEAR_JOB_TOKEN: Authorization for GraphQL requests for linear sync job

Footnotes

  1. uses @nx-tools/nx-prisma builder that provides a wrapper around the Prisma CLI. 2

  2. As example it used at contract-sync-job app, check CONTRACT_SYNC_TOKEN env