- Pre-requisites:
- Install Docker
- Install Phantom Wallet
- Install the Hasura CLI
💡 NOTE: run all these steps in formfn-monorepo
- Copy Env vars:
- Run
cp packages/server/.env.local packages/server/.env
- Start Docker containers for server and DB: Run
yarn start-server.
- Make sure you give Docker permission to read your
Documentsfolder in System Preferences! Otherwise, you may run into errors likeError: EPERM: operation not permitted.
- Initial DB setup:
export HASURA_GRAPHQL_ADMIN_SECRET=771043a6-2176-4cd6-9831-591ceca0ad5zyarn server hasura-migrate && yarn server hasura-reloadyarn server hasura-console
- Generate Prisma (ORM) client:
- First, install
gsed - Then run
yarn server gen-prisma
- Build TypeScript modules:
- Run
yarn tsc.
- Wait for server to reload:
- Wait until you see
Server listening at http://0.0.0.0:4000in the Docker logs in your terminal - Then, run
yarn server hasura-reloadagain - NOTE: if you encounter problems, re-run the Docker containers (step 2)
- Pull latest exchange rates:
- Run
yarn update-local-exchange-rates
- Create an account via the frontend (Sign in and fill out modal)
- Add that account's address to the
Whitelisttable ("Insert Row") via Hasura console
- Ping the
#engchannel in Slack and ask someone for help to fill in the rest of the secrets as needed- Fill in
AUTHORITYandGUMDROP_CONFIG_AUTHORITY
- Fill in
yarn start-serverfromformfn-monorepo- If you make changes, just run
yarn swclocally (you don't need to re-build/re-run), and your changes will propagate to the container. We useswcfor development because it is much faster thantsc. Note thatswcis only for transpilation, i.e. generating JS files from TS files. It does not do any type checking, sotscis still important to use if you want to make sure your types are valid.
- Most things should be done automatically for you with post-merge Git hooks in place
- If a new package is installed, you'll need to re-build the Docker image by following the Running stuff instructions
- Pay close attention to the console messages from the post-merge steps! Most warnings/errors should be addressed in the below Troubleshooting section.
I was running into this error for a while when trying to run hasura migrate apply:
formfn-server/hasura-project $ hasura migrate apply --endpoint http://localhost:8080/
✔ formfn
INFO nothing to apply
Fixed by running hasura migrate delete --all and then re-adding back the migration files (i.e., git restore migrations/*). I think the problem was that Hasura thought the migrations were already applied (but they weren't).
You can also get around this by renaming the migration folders.
Sometimes after applying new DB changes, you may get an error like Inconsistent object: HTTP exception occurred while sending the request to http://host.docker.internal:4000/graphql.
The simplest solution is to re-run the steps in Running stuff and then reloading the remote schema in the Hasura console.
If you run into this issue, the most likely culprit is that there's something wrong with the yarn tsc step.
For now, this can likely be bypassed by installing Typescript locally
This most likely means that your hasura admin secret needs to be (re)exported To fix, run the export command again.
export HASURA_GRAPHQL_ADMIN_SECRET=<secret from docker-compose-dev.yaml>- Alternatively, add the above variable to your
.bashrcfor a more permanent solution
NOTE: this stuff is not really necessary, because we apply migrations in a GitHub action (main-deploy.yml). Leaving here because it's still kinda useful to see all the commands.
Mostly following https://hasura.io/docs/latest/graphql/core/migrations/migrations-setup.html
- Disable the console by setting
HASURA_GRAPHQL_ENABLE_CONSOLEto"false"indocker-compose.yaml. ALSO, set theHASURA_GRAPHQL_ADMIN_SECRETenv variable, otherwise all the commands below will ask for it - Install the Hasura CLI.
- Set up a project directory with
hasura init hasura-project --endpoint http://localhost:8080/. Then, runcd hasura-project - Create migration files
hasura migrate create "init" --from-server --database-name formfn.INFO Migrations files created name=init version=1640235527703
- Mark migration as applied
hasura migrate apply --version "1640235527703" --skip-execution --database-name formfn - Export Hasura metadata
hasura metadata export - Now, when making changes, run
hasura console - To apply changes in prod:
- Apply metadata
hasura metadata apply --endpoint https://graphql.formfn.com/ - Apply migrations
hasura migrate apply --all-databases --endpoint https://graphql.formfn.com/ - Reload metadata
hasura metadata reload --endpoint https://graphql.formfn.com/
- Apply metadata
- Remember to change endpoints of actions and events to use
api.formfn.cominstead oflocalhost(should really use environment variables for this though...)
NOTE: it's ok to enable the console on the prod server, because running hasura console doesn't work on there (see hasura/graphql-engine#2824). So as long as no data is modified using the console, which shouldn't happen anyways (we should modify the devdb and then apply migrations), it's fine.
- Squash migrations into single file
hasura migrate squash --name "image library" --from <start-migration-version> --database-name formfn - [Only necessary if you squash] Mark migration as applied
hasura migrate apply --version "<squash-migration-version>" --skip-execution --database-name formfn npx prisma introspect && npx prisma generate- Commit changes
- Pull changes on prod server
- Apply metadata
hasura metadata apply --endpoint https://graphql.formfn.com/ - Apply migrations
hasura migrate apply --endpoint https://graphql.formfn.com/ --version LAST_MIGRATION_VERSION - Reload metadata
hasura metadata reload --endpoint https://graphql.formfn.com/ docker buildanddocker-compose up(for Prisma). Otherwise webhook for action doesn't work? TODO: not sure why, figure it out.
- Follow these instructions
hasura metadata apply --endpoint https://graphqldev.formfunction.xyz/ --admin-secret SECREThasura migrate apply --endpoint https://graphqldev.formfunction.xyz/ --admin-secret SECRET- NOTE: if you are creating a second Hasura project for the same DB, you should run
hasura migrate apply --skip-execution --up all --endpoint ENDPOINT --admin-secret SECRETinstead
- NOTE: if you are creating a second Hasura project for the same DB, you should run
hasura metadata reload --endpoint https://graphqldev.formfunction.xyz/ --admin-secret SECRET
- See below for how to nuke the DB
cd hasura-projecthasura migrate delete --all --endpoint http://localhost:8080 --admin-secret SECRETgit checkout migrationshasura metadata clear --endpoint http://localhost:8080 --admin-secret SECREThasura metadata apply --endpoint http://localhost:8080 --admin-secret SECREThasura migrate apply --endpoint http://localhost:8080 --admin-secret SECREThasura metadata reload --endpoint http://localhost:8080 --admin-secret SECRET
formfn/formfn-server $ docker exec -it formfn-postgres bash
root@8201a2fb0709:/# psql -U postgres formfn
psql (12.7 (Debian 12.7-1.pgdg100+1))
Type "help" for help.
formfn=#
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
Then, run \d to check that there are no tables.
curl -X POST http://localhost:4000/intern/createLastBidPrice -d '{"mint": "PDvDtvhZy8gXJMJ9P7Aq2DE1pG8CLrj2npxR39LPPFA"}' --header "Content-Type: application/json" --header "check: fofu"
Using jest.config.ts messes with Docker
import type { Config } from "@jest/types";
// Sync object
const config: Config.InitialOptions = {
moduleDirectories: ["node_modules", "."],
verbose: true,
};
export default config;
Specifically, the moduleDirectories line makes it so that in the Docker image, the file structure is dist/src/src/... instead of just dist/src. Idk why, but using CL args instead of the config file fixes it.
We have Prisma + Hasura + Relay setup but the actual schema changes are generated through Hasura.
While you could add things by hand, you will need to also write up/down SQL migration besides Prisma schema and Hasura metadata files. Thus recommended way to do this is through the Hasura console.
Creating a table via Hasura console
1. First, go to the Data tab and select DB schema (public) on the left and click 'Create table'- UI for columns creation will pop up so you now could add all the columns you need (most of the options are common SQL things)
- After that you need to go to the Permissions tab of the created table. The best way to get some examples is to explore other tables' permissions, but most likely you will want to enable select/insert/delete for users and maybe some select for anonymous. (don't forget to update permissions if you add new columns to the table)
- That's it for Hasura UI, now your table is here but you need to generate all the code changes, see stuff below 👇
Generating code for the new table created via Hasura console
- First, you need to generate GQL stuff for server
yarn server gen-types - Then run
yarn fix-serverto generate Prisma changes (or justyarn server gen-prisma). - Now the frontend part - for basic GQL stuff you need to run
yarn frontend gen-graphql - And the final step is to generate Relay code via
yarn frontend relay - That's it! 🎉 Now you could start to use the new table in your code.
To run the server in vs code.
- Run Postgres: Select Run Task (From the Terminal Menu or from command panel) and select the task "Run Postgres"
- Run Hasura GraphQL Server: Select Run Task (From the Terminal Menu or from command panel) and select the task "Run Hasura GraphQL Engine"
- Launch the API: From the Run menu, Select "Start Debugging"
- Run Frontend: Select Run Task (From the Terminal Menu or from command panel) and select the task "Run Frontend"
You can now set breakpoints anywhere in the API and they will work!
Please be warned that closing VSCode or stopping the tasks does not actually stop the docker containers. You will need to stop them manually using the tasks "Stop Postgres" and "Stop Hasura GraphQL Engine"

