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

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.