Unverified Commit 2b817af1 authored by Allen Hai's avatar Allen Hai Committed by GitHub

Update fauna example (#19806)

This PR removes the `next.config.js` file with an `env` key and instead modifies the setup script to create a `.env.local` file with the provided credentials. 

I also made some changes to the Deploy Button to ask for the environment variables when creating a new project. 
parent dc4beade
NEXT_PUBLIC_FAUNADB_SECRET=<replace-with-generated-client-secret>
NEXT_PUBLIC_FAUNADB_GRAPHQL_ENDPOINT=https://graphql.fauna.com/graphql
\ No newline at end of file
...@@ -6,7 +6,7 @@ This simple Guestbook SPA example shows you how to use [FaunaDB's GraphQL endpoi ...@@ -6,7 +6,7 @@ This simple Guestbook SPA example shows you how to use [FaunaDB's GraphQL endpoi
Deploy the example using [Vercel](https://vercel.com): Deploy the example using [Vercel](https://vercel.com):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-graphql-faunadb) [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fhello-world&env=NEXT_PUBLIC_FAUNADB_SECRET,NEXT_PUBLIC_FAUNADB_GRAPHQL_ENDPOINT&envDescription=Client%20secret%20and%20GraphQL%20endpoint%20needed%20for%20communicating%20with%20the%20live%20Fauna%20database&project-name=my-nextjs-guestbook&repository-name=my-nextjs-guestbook&demo-title=Next.js%20Fauna%20Guestbook%20App&demo-description=A%20simple%20guestbook%20application%20built%20with%20Next.js%20and%20Fauna&demo-url=https%3A%2F%2Fnextjs-guestbook.vercel.app%2F)
## Why FaunaDB ## Why FaunaDB
...@@ -21,6 +21,8 @@ To use a live FaunaDB database, create a database at [dashboard.fauna.com](https ...@@ -21,6 +21,8 @@ To use a live FaunaDB database, create a database at [dashboard.fauna.com](https
The database can then be set up with the delivered setup by running: The database can then be set up with the delivered setup by running:
``` ```
npm run setup
# or
yarn setup yarn setup
``` ```
...@@ -30,7 +32,7 @@ This script will ask for the admin token. Once you provide it with a valid token ...@@ -30,7 +32,7 @@ This script will ask for the admin token. Once you provide it with a valid token
- **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 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. - **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. 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.
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: 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:
...@@ -56,4 +58,4 @@ Your app should be up and running on [http://localhost:3000](http://localhost:30 ...@@ -56,4 +58,4 @@ Your app should be up and running on [http://localhost:3000](http://localhost:30
### Deploy ### Deploy
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fhello-world&env=NEXT_PUBLIC_FAUNADB_SECRET,NEXT_PUBLIC_FAUNADB_GRAPHQL_ENDPOINT&envDescription=Client%20secret%20and%20GraphQL%20endpoint%20needed%20for%20communicating%20with%20the%20live%20Fauna%20database&project-name=my-nextjs-guestbook&repository-name=my-nextjs-guestbook&demo-title=Next.js%20Fauna%20Guestbook%20App&demo-description=A%20simple%20guestbook%20application%20built%20with%20Next.js%20and%20Fauna&demo-url=https%3A%2F%2Fnextjs-guestbook.vercel.app%2F)
...@@ -25,7 +25,7 @@ export default function GuestbookEntry(props) { ...@@ -25,7 +25,7 @@ export default function GuestbookEntry(props) {
> >
<img <img
className={guestbookEntryUserDetailAvatarImg.className} className={guestbookEntryUserDetailAvatarImg.className}
src={`https://avatars.io/twitter/${props.twitter_handle}/`} src={`https://twitter-avatar.now.sh/${props.twitter_handle}/`}
/> />
</a> </a>
</div> </div>
......
...@@ -35,18 +35,21 @@ export const useGuestbookEntries = () => { ...@@ -35,18 +35,21 @@ export const useGuestbookEntries = () => {
} }
}` }`
const size = 100 const size = 100
const { data, error } = useFetch(process.env.faunaDbGraphQlEndpoint, { const { data, error } = useFetch(
method: 'POST', process.env.NEXT_PUBLIC_FAUNADB_GRAPHQL_ENDPOINT,
headers: { {
Authorization: `Bearer ${process.env.faunaDbSecret}`, method: 'POST',
'Content-type': 'application/json', headers: {
Accept: 'application/json', Authorization: `Bearer ${process.env.NEXT_PUBLIC_FAUNADB_SECRET}`,
}, 'Content-type': 'application/json',
body: JSON.stringify({ Accept: 'application/json',
query, },
variables: { size }, body: JSON.stringify({
}), query,
}) variables: { size },
}),
}
)
return { return {
data: getData(data), data: getData(data),
...@@ -83,10 +86,10 @@ export const createGuestbookEntry = async (twitterHandle, story) => { ...@@ -83,10 +86,10 @@ export const createGuestbookEntry = async (twitterHandle, story) => {
} }
}` }`
const res = await fetch(process.env.faunaDbGraphQlEndpoint, { const res = await fetch(process.env.NEXT_PUBLIC_FAUNADB_GRAPHQL_ENDPOINT, {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: `Bearer ${process.env.faunaDbSecret}`, Authorization: `Bearer ${process.env.NEXT_PUBLIC_FAUNADB_SECRET}`,
'Content-type': 'application/json', 'Content-type': 'application/json',
Accept: 'application/json', Accept: 'application/json',
}, },
......
module.exports = {
env: {
/**
|--------------------------------------------------
| Set the client secret for your database here.
|
| Learn more about managing roles and creating private keys:
| https://docs.fauna.com/fauna/current/security/
|--------------------------------------------------
*/
faunaDbSecret: '< GRAPHQL_SECRET >',
faunaDbGraphQlEndpoint: 'https://graphql.fauna.com/graphql',
},
}
...@@ -90,10 +90,28 @@ readline.question(`Please provide the FaunaDB admin key\n`, (adminKey) => { ...@@ -90,10 +90,28 @@ readline.question(`Please provide the FaunaDB admin key\n`, (adminKey) => {
) )
.then((res) => { .then((res) => {
console.log('3. Created key to use in client') console.log('3. Created key to use in client')
console.log( const envFile =
'Replace the < GRAPHQL_SECRET > placehold in next.config.js with:' 'NEXT_PUBLIC_FAUNADB_SECRET=' +
) res.secret +
console.log(res.secret) '\n' +
'NEXT_PUBLIC_FAUNADB_GRAPHQL_ENDPOINT=https://graphql.fauna.com/graphql'
fs.writeFile('.env.local', envFile, (err) => {
if (err) {
console.error(
'Failed to create .env.local file. Copy the .env.local.example file and provide the secret shown below:'
)
console.log(res.secret)
} else {
console.log('4. Created .env.local file with secret\n')
fs.readFile('.env.local', (err, data) => {
if (!err) {
console.log('.env.local:\n')
console.log(data.toString('utf-8'))
}
})
}
})
}) })
}) })
.catch((err) => { .catch((err) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment