diff --git a/packages/gatsby-source-medium/README.md b/packages/gatsby-source-medium/README.md index e15fed0489ff0..5e83f681eb4ea 100644 --- a/packages/gatsby-source-medium/README.md +++ b/packages/gatsby-source-medium/README.md @@ -23,6 +23,8 @@ plugins: [ }, ]; ``` +###### Note +Remember that if you are fetching a user, prepend your username with `@`. ## How to query diff --git a/packages/gatsby-source-medium/src/gatsby-node.js b/packages/gatsby-source-medium/src/gatsby-node.js index 7d94e52c31093..99703f51c4e5b 100644 --- a/packages/gatsby-source-medium/src/gatsby-node.js +++ b/packages/gatsby-source-medium/src/gatsby-node.js @@ -32,15 +32,29 @@ exports.sourceNodes = async ({ boundActionCreators }, { username }) => { const result = await fetch(username) const json = JSON.parse(strip(result.data)) - const { posts } = json.payload - const collectionKeys = Object.keys(json.payload.references.Collection) - const userKeys = Object.keys(json.payload.references.User) - - const importableResources = [ - userKeys.map(key => json.payload.references.User[key]), - posts, - collectionKeys.map(key => json.payload.references.Collection[key]), - ] + let importableResources = [] + let posts = {} // because `posts` needs to be in a scope accessible by `links` below + + const users = Object.keys(json.payload.references.User) + .map(key => json.payload.references.User[key]) + importableResources = importableResources.concat(users) + + if (json.payload.posts) { + posts = json.payload.posts + importableResources = importableResources.concat(posts) + } + + if (json.payload.references.Post) { + posts = Object.keys(json.payload.references.Post) + .map(key => json.payload.references.Post[key]) + importableResources = importableResources.concat(posts) + } + + if (json.payload.references.Collection) { + const collections = Object.keys(json.payload.references.Collection) + .map(key => json.payload.references.Collection[key]) + importableResources = importableResources.concat(collections) + } const resources = Array.prototype.concat(...importableResources) resources.map(resource => {