Example of using FaunaDB with Netlify functions.
This example shows how to use HTTP only cookies for auth with FaunaDB's native Graphql API.
This application is using React for the frontend, Netlify Functions for API calls, and FaunaDB as the backing database, utilizing the Graphql API with Apollo. This project is bundled with Parcel.
The todo editing is enabled by Draft.js
This project is primarily derived from netlify-faunadb-example
It has been updated to be better and more responsive stylings!
Netlify Dev works with parcel-bundler
out of the box!
(it also works with create-react-app
)
So there is no need to install netlify-lambda
and set up function proxies.
- Clone down the repository
git clone https://github.com/ptpaterson/netlify-faunadb-graphql-auth.git
- Enter the repo directory
cd netlify-faunadb-example
- Install the dependencies
npm install
# -OR-
yarn
- Sign up for a FaunaDB account
https://dashboard.fauna.com/accounts/register
- Create a master database. It will become the parent database for the app database generated by the script provided. In the Fauna Cloud Console:
- Click “New Database”
- Enter any name, e.g. “Netlify”, as the “Database Name”
- Click “Save”
- Create a database access key
In the Fauna Cloud Console:
- Click “Security” in the left navigation
- Click “New Key”
- Make sure that the “Database” field is set to “Netlify” (or whatever you named it)
- Make sure that the “Role” field is set to “Admin”
- Enter any name, e.g. “Netlify Admin key” as the “Key Name”
- Click “Save”
- Copy the database access key’s secret
Save the secret somewhere safe; you won’t get a second chance to see it.
- Set your database access secret for running the bootstrapping scripts
Either set the environment variables directly in your terminal, or use a
.env
file. The scripts will pick up anything in a.env
file.
In your terminal, run the following command:
# bash
export FAUNADB_ADMIN_KEY="YourFaunaDBSecretHere"
NOTE: Make sure to replace
YourFaunaDBSecretHere
with the value of the secret that you copied in the previous step.
NOTE: We'll set a different variable for FAUNADB_PUBLIC_KEY in a bit, which is the important key to actually run the app!
- Bootstrap your FaunaDB collection and indexes
node scripts/create-database.js
The bootstrap-db
script will output some important information. Copy down the created keys shown to you in the output. If you lose the keys, you can generate them by hand or run the bootstrap command again.
Output should be similar to:
Creating your FaunaDB Database...
1) Create database "auth-example"
+ Created Database "auth-example"
2) Creating temporary key
+ Created Key "temp admin key for auth-example"
3) Uploading Graphql Schema...
o GraphQL schema imported
4) Update generated User Defined Functions...
o Updated Function "login"
o Updated Function "logout"
o Updated Function "me"
o Updated Function "user_create_todo"
5) Create custom roles...
+ Created Role "public"
+ Created Key "Public key for auth-example"
! Public client key: fnADmZcVSKFYEykYVRyVaQ5WN9RQ3OEmEMtweMWk
+ Created Role "user"
-Deleted Key "temp admin key for auth-example"
Fauna Database schema has been created
Claim your fauna database with "netlify addons:auth fauna"
- Upload Graphql Schema
In the Fauna Cloud Console:
- Click “GraphQL” in the left navigation
- Click “IMPORT SCHEMA”
- Upload the schema at
scripts/faunaSchema.graphql
- (Optional) Add example data
Set the FAUNADB_ADMIN_KEY
variable (or save to .env
file).
Then run
node scripts/create-example-data.js
Or, use the GraphQL Playground now available in the cloud console.
- Get
netlify-cli
npm install netlify-cli -g
- Run the project
Either set the environment variables directly in your terminal, or use a
.env
file. The scripts will pick up anything in a.env
file.
In your terminal, run the following command:
# bash
export FAUNADB_PUBLIC_KEY="YourFaunaDBPublicSecret"
netlify dev
This project uses apollo-server-lambda
for the lambda function. Lambdas are not a great solution for constantly polling remote schemas. I've also had trouble with async lambda's in general. Because of this, any time a new schema is uploaded to FaunaDB, the SDL should be downloaded and placed as a string in functions/graphql/remoteSchema.js
.