|
| 1 | +# Fauna GraphQL Guestbook Starter |
| 2 | + |
| 3 | +This Guestbook Single-Page Application (SPA) example shows you how to use [Fauna's GraphQL endpoint](https://docs.fauna.com/fauna/current/api/graphql/) in your Next.js project. |
| 4 | + |
| 5 | +## Deploy your own |
| 6 | + |
| 7 | +Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example): |
| 8 | + |
| 9 | +[](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-fauna&project-name=fauna-nextjs-guestbook&repository-name=fauna-nextjs-guestbook&demo-title=Next.js%20Fauna%20Guestbook%20App&demo-description=A%20simple%20guestbook%20application%20built%20with%20Next.js%20and%20Fauna&integration-ids=oac_Erlbqm8Teb1y4WhioE3r2utY) |
| 10 | + |
| 11 | +## Why Fauna |
| 12 | + |
| 13 | +By importing a `.gql` or `.graphql` schema into Fauna ([see our sample schema file](./schema.gql)), Fauna will generate required Indexes and GraphQL resolvers for you -- hands free 👐 ([some limitations exist](https://docs.fauna.com/fauna/current/api/graphql/#limitations)). |
| 14 | + |
| 15 | +## How to use |
| 16 | + |
| 17 | +Execute [`create-next-app`](https://github.com/vercel/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: |
| 18 | + |
| 19 | +``` |
| 20 | +npx create-next-app --example with-fauna with-fauna-app |
| 21 | +# or |
| 22 | +yarn create next-app --example with-fauna with-fauna-app |
| 23 | +``` |
| 24 | + |
| 25 | +You can start with this template [using `create-next-app`](#using-create-next-app) or by [downloading the repository manually](#download-manually). |
| 26 | + |
| 27 | +To use a live Fauna database, create a database at [dashboard.fauna.com](https://dashboard.fauna.com/) and generate an admin token by going to the **Security** tab on the left and then click **New Key**. Give the new key a name and select the 'Admin' 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. |
| 28 | + |
| 29 | +The database can then be set up with the delivered setup by running: |
| 30 | + |
| 31 | +``` |
| 32 | +npm run setup |
| 33 | +# or using yarn |
| 34 | +yarn setup |
| 35 | +``` |
| 36 | + |
| 37 | +This script will ask for the admin token. Once you provide it with a valid token, this is what the script automatically does for you: |
| 38 | + |
| 39 | +- **Import the GraphQL schema**, by importing a GraphQL schema in Fauna, Fauna 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 |
| 40 | +- **Create an index and UDF**, in order to implement custom sorting (by createdAt field), we need to create a GraphQL resolver that uses [UDF](https://docs.fauna.com/fauna/current/api/graphql/functions?lang=javascript) based on sorting index. |
| 41 | +- **Create a role suitable for the Client**, Fauna 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. |
| 42 | +- **Create a token for that role**, this is the token to be used in the app. |
| 43 | + |
| 44 | +At the end, a `.env.local` [file](https://nextjs.org/docs/basic-features/environment-variables) will be created for you with the newly generated client token assigned to an environment variable. |
| 45 | + |
| 46 | +### Run locally |
| 47 | + |
| 48 | +Install packages, set up if needed, then run the development server: |
| 49 | + |
| 50 | +```bash |
| 51 | +npm install |
| 52 | +# if you haven't run setup yet |
| 53 | +npm run setup |
| 54 | +npm run dev |
| 55 | +# or using yarn |
| 56 | +yarn |
| 57 | +# if you haven't run setup yet |
| 58 | +yarn setup |
| 59 | +yarn dev |
| 60 | +``` |
| 61 | + |
| 62 | +Your app should be up and running on [http://localhost:3000](http://localhost:3000)! |
0 commit comments