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

[gatsby-source-medium] fetch users and publications #3623

Merged
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
2 changes: 2 additions & 0 deletions packages/gatsby-source-medium/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ plugins: [
},
];
```
###### Note
Remember that if you are fetching a user, prepend your username with `@`.

## How to query

Expand Down
32 changes: 23 additions & 9 deletions packages/gatsby-source-medium/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you first declare importableResources and then change the values? The way it was before I felt it was easier to grasp.

Otherwise it's looking great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked the way it was done too, but I struggled to find a good way to conditionally insert the values while also keeping importableResources in the correct scope. I'm sure there's a better way to do it (there always is), but I think this way is good enough for now at least to fix the issue with not being able to fetch users. Doing it this way at least I know it's going to work every time.

If I get time in the next few weeks I'd like to revisit it to find those better ways, and also create some unit tests since there aren't any yet.

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 => {
Expand Down