Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
## Install the source plugin

Enter the following into your terminal in the root directory of your Gatsby project:

```
npm install gatsby-source-faunadb
```

or, if you are using Yarn:

```
yarn add gatsby-source-faunadb
```

## Configure your project

To connect your Gatsby project to your Fauna database, add the following to the `/gatsby-config.js` file in your project directory:

```js
module.exports = {
//...
Expand All @@ -25,6 +29,11 @@ module.exports = {
// The secret for the key you're using to connect to your Fauna database.
// You can generate on of these in the "Security" tab of your Fauna Console.
secret: "___YOUR_FAUNADB_SECRET___",
// Where your database instance exists.
// You can see the region in the top right of your DB overview from the Fauna Console.
// For reference, this property addresses this issue:
// https://forums.fauna.com/t/unauthorized-when-using-valid-secret-key/2306
domain: "db.us.fauna.com",
// The name of the index you want to query
// You can create an index in the "Indexes" tab of your Fauna Console.
index: `animalsByType`,
Expand All @@ -34,19 +43,21 @@ module.exports = {
// This is the name under which your data will appear in Gatsby GraphQL queries
// The following will create queries called `allBird` and `bird`.
type: "bird",
// If you need to limit the number of documents returned, you can specify a
// If you need to limit the number of documents returned, you can specify a
// maximum number to read.
size: 100
size: 100,
},
},
//...
],
}
```

If you'd like to include multiple indexes in your Gatsby project you can add the above section many times, once for each index.
You can also include the same index multiple times with different arguments and types.

## Query the data

Your data will appear in your Gatesby project under the `type` name that you gave in `./gatsby-config.js`.
For example, for the config above you can query:

Expand All @@ -62,21 +73,24 @@ query MyQuery {
```

or, to fetch a single document:

```graphql
query MyQuery {
bird(name: {eq: "Flamingo"}) {
bird(name: { eq: "Flamingo" }) {
name
type
_id
_ts
}
}
```
Note the Fauna document ID and timestamp are also available in the returned type, as `_id` and `_ts` respectively.

Note the Fauna document ID and timestamp are also available in the returned type, as `_id` and `_ts` respectively.

## Example
Given the following data in a Fauna collection:

Given the following data in a Fauna collection:

```json
[
{ "name": "Pallas's cat", "type": "cat" },
Expand All @@ -92,6 +106,7 @@ Given the following data in a Fauna collection:
![Screenshot of index and results in the Fauna Console](./example-data.png)

We can use the following configuration in `/gatsby-config.js`:

```js
module.exports = {
plugins: [
Expand All @@ -108,6 +123,7 @@ module.exports = {
```

This will make our data available to query as `allAnimal` in Gatesby GraphQL:

```graphql
query MyQuery {
allAnimal {
Expand All @@ -120,6 +136,7 @@ query MyQuery {
```

Result:

```json
{
"data": {
Expand Down Expand Up @@ -151,13 +168,12 @@ Result:
}
```


## Troubleshooting

A common issue may be that the key secret you're using doesn't have enough access to return the data. If this is the case, you'll see the following message when running `gatsby develop`:

```
ERROR
ERROR

[PermissionDenied: permission denied] {
name: 'PermissionDenied',
Expand All @@ -171,9 +187,8 @@ A common issue may be that the key secret you're using doesn't have enough acces

If you see this, make sure you're using the correct secret and that the associated key has the correct permissions. **Your key will need permission to read all indexes that you're using _and_ all the collections that those indexes use.**


## Get involved

Thank you for using `gatsby-source-fauna` and I hope you find it useful. If you stumble upon any issues, please consider <a href="https://github.com/paulcuth/gatsby-source-faunadb/issues">logging the issue</a> or <a href="https://github.com/paulcuth/gatsby-source-faunadb/pulls">opening a pull request</a>. All contributions very welcome.

💛<a href="https://twitter.com/paulcuth">paulcuth</a>.
💛<a href="https://twitter.com/paulcuth">paulcuth</a>.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gatsby-source-faunadb",
"description": "A Gatsby source plugin to fetch documents from Fauna DB.",
"version": "1.0.0",
"version": "1.0.1",
"author": "Paul Cuthbertson <[email protected]>",
"repository": {
"type": "git",
Expand Down
7 changes: 5 additions & 2 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ exports.sourceNodes = async (
options
) => {
const { createNode } = actions;
const { secret, type, index, arguments: args = [] } = options;
const { secret, domain, type, index, arguments: args = [] } = options;

const client = new faunadb.Client({ secret });
const client = new faunadb.Client({
secret,
domain
});

try {
const size =
Expand Down