Fetching Many Items by Key with GraphQL DataLoaders

Jacob Paris
InstructorJacob Paris
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

One-to-many relationships are a prime candidate for scaling issues. In this lesson we'll add a new loader to fetch all the comments for a given post ID.

Instructor: [0:01] Let's add a new loader to fetch all of the comments for a given post ID. Select All from comments, where post ID is in the keys. Then we want to replace each key, each post ID, with an array of all the comments for that post.

[0:23] Under the post resolvers, add one for comments, which uses the new comments by post idLoader. Return comments by post idLoader.load post ID.

[0:35] Next, we'll add the comments field to the post schema, which returns an array of comments, and then the comment type, which has an ID, text, and an author field. We'll also need to resolve the author field on the comment type which we can just copy paste from the post type.

[0:52] Let's try querying the posts. We'll get all their comments and each ID, text, author ID, and name. No comments for the first few, looks like five comments on this one. Two comments here. All the information is right where it's supposed to be.

egghead
egghead
~ 8 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today