Skip to content

Commit fe3f520

Browse files
Update Fauna example with new guestbook (#27295)
Based on #26708 - Renames FaunaDB -> Fauna - Moves to Tailwind instead of bespoke styling - Use SWR instead of hand-rolled fetch wrapper - List/Create API for Guestbook - Preps for setting correct environment variables from Vercel integration
1 parent 63aeddb commit fe3f520

37 files changed

+621
-1118
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FAUNADB_SECRET=
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ yarn-error.log*
3232

3333
# vercel
3434
.vercel
35+
36+
.idea

examples/with-fauna/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
[![Deploy with Vercel](https://vercel.com/button)](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)!
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function ErrorMessage({ children }) {
2+
return (
3+
<p className="flex items-center text-sm font-bold text-red-800 dark:text-red-400">
4+
<svg
5+
xmlns="http://www.w3.org/2000/svg"
6+
viewBox="0 0 20 20"
7+
fill="currentColor"
8+
className="mr-2 h-4 w-4"
9+
>
10+
<path
11+
fillRule="evenodd"
12+
d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z"
13+
clipRule="evenodd"
14+
/>
15+
</svg>
16+
{children}
17+
</p>
18+
)
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default function LoadingSpinner() {
2+
return (
3+
<svg
4+
className="animate-spin h-5 w-5 text-gray-900 dark:text-gray-100"
5+
xmlns="http://www.w3.org/2000/svg"
6+
fill="none"
7+
viewBox="0 0 24 24"
8+
>
9+
<circle
10+
className="opacity-25"
11+
cx="12"
12+
cy="12"
13+
r="10"
14+
stroke="currentColor"
15+
strokeWidth="4"
16+
/>
17+
<path
18+
className="opacity-75"
19+
fill="currentColor"
20+
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
21+
/>
22+
</svg>
23+
)
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function SuccessMessage({ children }) {
2+
return (
3+
<p className="flex items-center text-sm font-bold text-green-700 dark:text-green-400">
4+
<svg
5+
xmlns="http://www.w3.org/2000/svg"
6+
viewBox="0 0 20 20"
7+
fill="currentColor"
8+
className="mr-2 h-4 w-4"
9+
>
10+
<path
11+
fillRule="evenodd"
12+
d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
13+
clipRule="evenodd"
14+
/>
15+
</svg>
16+
{children}
17+
</p>
18+
)
19+
}

examples/with-fauna/jsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"@/components/*": ["components/*"],
6+
"@/lib/*": ["lib/*"]
7+
}
8+
}
9+
}

examples/with-fauna/lib/fauna.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { GraphQLClient, gql } from 'graphql-request'
2+
3+
const FAUNADB_GRAPHQL_ENDPOINT = 'https://graphql.fauna.com/graphql'
4+
const FAUNADB_SECRET = process.env.FAUNADB_SECRET
5+
6+
const graphQLClient = new GraphQLClient(FAUNADB_GRAPHQL_ENDPOINT, {
7+
headers: {
8+
authorization: `Bearer ${FAUNADB_SECRET}`,
9+
},
10+
})
11+
12+
export const listGuestbookEntries = () => {
13+
const query = gql`
14+
query Entries($size: Int) {
15+
entries(_size: $size) {
16+
data {
17+
_id
18+
_ts
19+
name
20+
message
21+
createdAt
22+
}
23+
}
24+
}
25+
`
26+
27+
return graphQLClient
28+
.request(query, { size: 999 })
29+
.then(({ entries: { data } }) => data)
30+
}
31+
32+
export const createGuestbookEntry = (newEntry) => {
33+
const mutation = gql`
34+
mutation CreateGuestbookEntry($input: GuestbookEntryInput!) {
35+
createGuestbookEntry(data: $input) {
36+
_id
37+
_ts
38+
name
39+
message
40+
createdAt
41+
}
42+
}
43+
`
44+
45+
return graphQLClient.request(mutation, { input: newEntry })
46+
}

examples/with-fauna/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "with-fauna",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"description": "Guestbook example with Next.js and Fauna.",
6+
"scripts": {
7+
"dev": "next",
8+
"build": "yarn setup && next build",
9+
"start": "next start",
10+
"setup": "node ./scripts/setup.js"
11+
},
12+
"dependencies": {
13+
"classnames": "2.3.1",
14+
"date-fns": "2.23.0",
15+
"faunadb": "4.3.0",
16+
"graphql": "15.5.1",
17+
"graphql-request": "3.5.0",
18+
"next": "latest",
19+
"react": "^17.0.2",
20+
"react-dom": "^17.0.2",
21+
"request": "2.88.2",
22+
"swr": "0.5.6"
23+
},
24+
"devDependencies": {
25+
"autoprefixer": "10.3.1",
26+
"postcss": "8.3.6",
27+
"prettier": "2.3.2",
28+
"tailwindcss": "2.2.7",
29+
"stream-to-promise": "3.0.0"
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { listGuestbookEntries, createGuestbookEntry } from '@/lib/fauna'
2+
3+
export default async function handler(req, res) {
4+
const handlers = {
5+
GET: async () => {
6+
const entries = await listGuestbookEntries()
7+
8+
res.json(entries)
9+
},
10+
11+
POST: async () => {
12+
const {
13+
body: { name, message },
14+
} = req
15+
const created = await createGuestbookEntry({
16+
name,
17+
message,
18+
createdAt: new Date(),
19+
})
20+
21+
res.json(created)
22+
},
23+
}
24+
25+
if (!handlers[req.method]) {
26+
return res.status(405).end()
27+
}
28+
29+
await handlers[req.method]()
30+
}

0 commit comments

Comments
 (0)