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.
git clone https://github.com/ptpaterson/netlify-faunadb-graphql-auth.git
cd netlify-faunadb-example
npm install
# -OR-
yarn
https://dashboard.fauna.com/accounts/register
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”
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”
You will save it in a .env
file in the next step. You won’t get a second chance to see it!
Create a .env
file in the project directory and store the server secret as
an environment variable:
# .env
FAUNADB_ADMIN_KEY="YOUR_SECRET_KEY_HERE"
NOTE: The
bootstrap
script will store another environment variable in this file calledFAUNADB_PUBLIC_KEY
.
# create database and fill with example data
npm run bootstrap
This bootstrap
package.json script will create a Fauna database called
auth-example
using scripts/create-database.js
and then fill it with
example data using scripts/create-example-data.js
.
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"
npm install netlify-cli -g
# serve development version
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
.