Populate data for content relationship

Hi,

I have a slice called SpeakersSlice. In its repeatable zone, I have a Content Relationship type that points to the Speaker model. So basically, I have a list of speakers (content relationship) in the SpeakersSlice.

{
    "slice_type": "speaker_slice",
    "slice_label": null,
    "version": "sktwi1xtmkfgx",
    "variation": "default-slice",
    "primary": {
        "title": [
            {
                "type": "heading2",
                "text": "Test title",
                "spans": []
            }
        ]
    },
    "items": [
        {
            "speaker": {
                "id": "YhTQvhEAACgA",
                "type": "speaker",
                "tags": [],
                "slug": "cas",
                "lang": "en-ca",
                "first_publication_date": "2022-02-22T12:02:12+0000",
                "last_publication_date": "2022-02-22T12:43:41+0000",
                "link_type": "Document",
                "isBroken": false
            }
        },
        {
            "speaker": {
                "id": "YhTQvhEAACg",
                "type": "speaker",
                "tags": [],
                "slug": "cas",
                "lang": "en-ca",
                "first_publication_date": "2022-02-22T12:02:12+0000",
                "last_publication_date": "2022-02-22T12:43:41+0000",
                "link_type": "Document",
                "isBroken": false
            }
        },
    ]
}

I can see that I got the speakers-data in the items array. However, it only returns me the ids of the speaker. Is there any way that I get the full data of a speaker?

Do I need to perform a second request (from a page component) to get speakers by ids? And pass them to the slice via context? But that data shouldn't be available for all the slices, therefore, passing it via the slice context is not a good option.

1 Like

Hello @dinhhuy.trinh, thanks for reaching out.

You'll need to perform a GraphQuery option to do selective fetching from the Content relationship fields. Here's the documentation:

Hi, do you have any example code with "@prismicio/client": "^6.3.0"? Does it work like this?

 client.getSingle<PrismicDocument>("pageHome", { 
        ref,
        graphQuery: `pageHome{body {...on speaker_slice {title}}}`
     })

I would use a separate constant to make it more readable. Something like this:

const graphQuery = `{
  pageHome {
    body {
      ...on speaker_slice  {
         title
       }
     }
   }
}`

const homeDoc = client.getByUID('blog_post', uid, { graphQuery });