README.md 3.4 KB
Newer Older
F
fauna-brecht 已提交
1 2 3 4
# FaunaDB Graphql Starter Example -- The FaunaDB Guestbook

This simple Guestbook SPA example shows you how to use [FaunaDB's GraphQL endpoint](https://docs.fauna.com/fauna/current/api/graphql/) in your Next.js project. [[Live demo](https://with-graphql-faunadb.now.sh/)].

5 6 7 8 9 10
## Deploy your own

Deploy the example using [ZEIT Now](https://zeit.co/now):

[![Deploy with ZEIT Now](https://zeit.co/button)](https://zeit.co/new/project?template=https://github.com/zeit/next.js/tree/canary/examples/with-graphql-faunadb)

F
fauna-brecht 已提交
11 12 13 14 15 16
## Why FaunaDB

By importing a `.gql` or `.graphql` schema into FaunaDB ([see our sample schema file](./schema.gql)), FaunaDB will generate required Indexes and GraphQL resolvers for you -- hands free 👐 ([some limitations exist](https://docs.fauna.com/fauna/current/api/graphql/#limitations)).

## How to use

L
Luis Alvarez D 已提交
17
You can start with this template [using `create-next-app`](#using-create-next-app) or by [downloading the repository manually](#download-manually).
F
fauna-brecht 已提交
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
To use a live FaunaDB database, create a database at [dashboard.fauna.com](https://dashboard.fauna.com/) and generate a server token by going to the **Security** tab on the left and then click **New Key**. Give the new key a name and select the 'Server' Role. Copy the token since the setup script will ask for it. Do not use it in the frontend, it has superpowers which you don't want to give to your users.

The database can then be set up with the delivered setup by running:

```
yarn setup
```

This script will ask for the server token. Once you provide it with a valid token, this is what the script automatically does for you:

- **Import the GraphQL schema**, by importing a GraphQL schema in FaunaDB, FaunaDB automatically sets up collections and indexes to support your queries. This is now done for you with this script but can also be done from the [dashboard.fauna.com](https://dashboard.fauna.com/) UI by going to the GraphQL tab
- **Create a role suitable for the Client**, FaunaDB has a security system that allows you to define which resources can be accessed for a specific token. That's how we limit our clients powers, feel free to look at the scripts/setup.js script to see how we make roles and tokens.
- **Create a token for that role** which is printed, this is the token to be used in the frontend.

At the end, the newly generated client token will be printed and should be used to replace the '< GRAPHQL_SECRET >' placeholder in the next.config.js config.
F
fauna-brecht 已提交
34

L
Luis Alvarez D 已提交
35
### Using `create-next-app`
F
fauna-brecht 已提交
36

37
Execute [`create-next-app`](https://github.com/zeit/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
F
fauna-brecht 已提交
38 39

```
40
npm init next-app --example with-graphql-faunadb with-graphql-faunadb
L
Luis Alvarez D 已提交
41 42
# or
yarn create next-app --example with-graphql-faunadb with-graphql-faunadb
F
fauna-brecht 已提交
43 44 45 46
```

### Download manually

L
Luis Alvarez D 已提交
47
Download the example:
F
fauna-brecht 已提交
48 49 50 51 52 53 54 55

```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-graphql-faunadb
cd with-graphql-faunadb
```

### Run locally

L
Luis Alvarez D 已提交
56
Install packages, then run the development server:
F
fauna-brecht 已提交
57 58 59

```bash
npm install
L
Luis Alvarez D 已提交
60 61 62 63
npm run dev
# or
yarn
yarn dev
F
fauna-brecht 已提交
64 65 66 67 68 69
```

Make sure to leave us a guestbook message in our [live demo](https://with-graphql-faunadb.now.sh/)! 😉

### Deploy

70
Deploy it to the cloud with [now](https://zeit.co/now)! [Install now](https://zeit.co/download) on your development machine before proceeding:
F
fauna-brecht 已提交
71 72 73 74

```bash
now
```