A Better Way To Build With Fauna
How exactly does Fauna GQL Upload (FGU) solve this problem? It uses Fauna's GraphQL import endpoint to upload your schema when you enter a command in your terminal. Let's say you have a schema:
type User { name: String! email: String! age: Int! }
You can then upload this schema by running:
npm run fauna
And then do this whenever you make changes to your schema. I'm also planning on adding a watch feature so that any change you make will be automatically uploaded, making the workflow even simpler.
Okay, but what about UDFs and indexes? You can upload those in the same command, just like roles, access providers, and domain data. You can even generate types based on your schema.
I'm sold! How do I use it?
Installation
To start using FGU you need to install it along with the JS driver for Fauna. This is because FGU doesn't include the JS driver itself. Doing this, allows you to choose which version of the driver to use when writing UDFs.
npm i fauna-gql-upload faunadb --save-dev
Folder structure
project │── fauna │ │── functions │ │ │ my-function.ts │ │── indexes │ │ │ my-index.ts │ │ schema.gql │── src │ │ index.html │ package.json │ .fauna.json
functions
indexes
providers
data
roles
Configuration
While FGU works without a config file, it can be useful to add one when your naming conventions or folder structure doesn't fit what FGU expects.
{ "fnsDir": "db/resolvers", "indexDir": "db/indices", "codegen": { "outputFile": "src/graphql/types.ts" } }
Add an admin key
Don't have an existing database?
If you don't have a database already, you need to follow these steps:
- Run
fauna create-database <database-name>
to create the database - Run
fauna create-key <database-name> admin
to create the key - Copy the key from the output.
The output of this command will contain your admin key. Copy it.
Do you have an existing database?
If you already have a database, you need to follow these steps:
- Run
fauna create-key <database-name> admin
to create the key - Copy the value found in the
secret
property of the output.
Add the secret to a .env
file
FGU_SECRET=<YOUR_SECRET_KEY>
Add a script
{ "scripts": { "fauna": "fgu" } }
You would then use it like this:
npm run fauna