Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for createRemoteFileNode #4330

Merged
merged 3 commits into from
Mar 2, 2018
Merged
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
57 changes: 56 additions & 1 deletion packages/gatsby-source-filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,61 @@ exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {

### createRemoteFileNode

When building source plugins for remote data sources such as headless CMSs, their data will often link to files stored remotely that are often convenient to download so you can work with locally.

The `createRemoteFileNode` helper makes it easy to download remote files and add them to your site's GraphQL schema.

```javascript
TO DO
createRemoteFileNode({
// The source url of the remote file
url: `https://example.com/a-file.jpg`,

// The redux store which is passed to all Node APIs.
store,

// Gatsby's cache which the helper uses to check if the file has been downloaded already. It's passed to all Node APIs.
cache,

// The boundActionCreator used to create nodes
createNode,

// OPTIONAL
// Adds htaccess authentication to the download request if passed in.
auth: { user: `USER`, password: `PASSWORD` },
})
```

#### Example usage

The following example is pulled from [gatsby-source-wordpress](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-wordpress). Downloaded files are created as `File` nodes and then linked to the WordPress Media node, so it can be queried both as a regular `File` node and from the `localFile` field in the Media node.

```javascript
const { createRemoteFileNode } = require(`gatsby-source-filesystem`);

exports.downloadMediaFiles = ({ nodes, store, cache, createNode, _auth }) => {
nodes.map(async node => {
let fileNode
// Ensures we are only processing Media Files
// `wordpress__wp_media` is the media file type name for Wordpress
if (node.__type === `wordpress__wp_media`) {
try {
fileNode = await createRemoteFileNode({
url: node.source_url,
store,
cache,
createNode,
auth: _auth,
})
} catch (e) {
// Ignore
}
}

// Adds a field `localFile` to the node
// ___NODE appendix tells Gatsby that this field will link to another node
if (fileNode) {
node.localFile___NODE = fileNode.id
}
})
};
```