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
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
-
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
npm run bootstrap-db
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 "netlify-auth..." + Created Database "aut_example". 2) Establish keys..." + Created "Admin key for auth_example" o Using FAUNADB_APP_ADMIN_KEY: fnADl6mmyxACE_l3awMn6E0-hVWMJfsj9YEGMLcP + Created "Server key for auth_example" o Using FAUNADB_SERVER_KEY: fnADl6mm1hACEyjnsgULIdYdY8OO0wFnb6fGIcjG 3) Create "User"... + Created new Collection "User". + Created Index "unique_User_email". 4) Create "Todo"... + Created Collection "Todo" + Created Index "all_todos" + Created Index "todo_owner_by_user" 5) Create User Defined Functions... + Created UDF "login" + Created UDF "logout" + Created UDF "me" + Created UDF "user_create_todo" 6) Create custom roles... + Created Role "public" + Created new public client key. o Using FAUNADB_PUBLIC_KEY: fnADl6mnYoACE0tFKEmd6TVXhyuwdm8jVV9Zqalm + Created Role "user" 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
functions/graphql/faunaSchema.graphql
-
(Optional) Add example data
Set the
FAUNADB_SERVER_KEY
variable (or save to.env
file).Then run
npm run bootstrap-example-data
Or, use the GraphQL Playground now available in the cloud console.
-
Get
netlify-cli
npm install netlify-cli -g
-
Run the project
netlify dev
This project uses apollo-server-lambda
for the lambda function. Lambdas are not a great solution for fetching remote schemas. 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
.