GithubHelp home page GithubHelp logo

ptpaterson / netlify-faunadb-graphql-auth Goto Github PK

View Code? Open in Web Editor NEW
57.0 2.0 13.0 1.63 MB

Netlify functions example with faunadb, graphql, and authorization

JavaScript 89.33% HTML 0.97% CSS 9.56% Shell 0.13%
faunadb http-cookies auth apollo netlify

netlify-faunadb-graphql-auth's Introduction

Netlify + FaunaDB + GraphQL + Auth    

Example of using FaunaDB with Netlify functions.

This example shows how to use HTTP only cookies for auth with FaunaDB's native Graphql API.

About this application

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!

Easy setup with Netlify dev

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.

Setup

1. Clone down the repository

git clone https://github.com/ptpaterson/netlify-faunadb-graphql-auth.git

2. Enter the repo directory

cd netlify-faunadb-example

3. Install the dependencies

npm install
# -OR-
yarn

4. Sign up for a FaunaDB account

https://dashboard.fauna.com/accounts/register

5. 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”

6. 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”

7. Copy the database access key

You will save it in a .env file in the next step. You won’t get a second chance to see it!

8. Bootstrap the database

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 called FAUNADB_PUBLIC_KEY.

9. Bootstrap your FaunaDB collection and indexes

# 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"

10. Get netlify-cli

npm install netlify-cli -g

11. Start developing!

# serve development version
netlify dev

Updating the Schema

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.

netlify-faunadb-graphql-auth's People

Contributors

ptpaterson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

netlify-faunadb-graphql-auth's Issues

Add error handling

Most errors are silent. This is particularly surprising when login fails!

Use `instrospectSchema`

It is possible to use introspectSchema.

It seems that the lambda is returning early before graphql server handler can act, when I try to use async/await. It worked when using Promises instead.

how latency increased by a lot because my caching of schema is not working -- it's fetching remote schema on every request. This really increased the latency of the functions.

This latency issue should be resolved before merging introspection.

The location of `faunaSchema.graphql` is incorrectly listed in the README

Readme has the wrong location. Can either update the README, or update the file location.

For the former,

Upload the schema at [`functions/graphql/faunaSchema.graphql`](https://github.com/ptpaterson/netlify-faunadb-graphql-auth/blob/master/functions/graphql/faunaSchema.graphql)

should be

Upload the schema at [`scripts/faunaSchema.graphql`](https://github.com/ptpaterson/netlify-faunadb-graphql-auth/blob/master/scripts/faunaSchema.graphql)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.