1. 17
    Use the readField helper to get the ID of items in read field policies
    1m 8s

Use the readField helper to get the ID of items in read field policies

Rares Matei
InstructorRares Matei
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

In the previous lesson, we were always returning true for whether a note is selected or not. In this lesson, we'll look at how to make that dynamic, based on the ID of the current note being read. For that, we'll explore the "readField" helper that gets passed in to our "read" field policy callback, and use it to read the ID property of the current note.

Instructor: [0:00] our read policy just says all notes are selected right now. Let's make it dynamic per note. I'll go up here and declare an array of selected note IDs. I'll say, for now, only the second note is selected. Notice how this isn't the Apollo cache ID, it's just the database ID of our note.

[0:18] Then, down here in our read policy, this function actually gets called with a bunch of things. It gets called with any existing value that's in the cache for this field, but it also gets called with some helpers. To get the ID of the current note we are reading from, we can use the readField helper to read the ID of the note.

[0:39] Now that we know the ID, we can check whether our selected note IDs array includes it. Now, if we go to the browser, we can see that only the second note is selected. If I click to open the full note, its selection status is still in sync with this note in the list. If we open the full note for some of these other notes, it's unselected, but we still can't toggle the selection status yet. It's read-only at the moment. We look at how to improve that in the next lesson.

egghead
egghead
~ 14 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