How to query data of Content Relation field in Slice

I have a document called game and a document called asset. Also there is a slice called AssetsGrid (assets_grid) which has a content relation field to asset in the repeatable zone. So the whole structure is roughly this:

{
  "games": [
    {
      "uid": "string",
      "data": {
        "slices": [
          {
            "slice_type": "assets_grid",
            "items": [
              {
                "asset": {
                  "type": "asset",
                  "slug": "string",
                  "id": "string",
                  "link_type": "Document"
                }
              }
            ]
          }
        ]
      }
    }
  ]
}

On a page that renders game how do I query asset data for my slice?

My current query is:

const games = await client.getAllByType<GameDocument>('game', {
  orderings: [{ field: 'my.game.rankNumber', direction: 'asc' }],
});

Hello @mlshv42

Thanks for reaching out to us.

To retrieve content from a linked document, you need to either use fetchLinks or graphQuery.
Since fetchLinks can not retrieve the field from the slice, so you need to use graphQuery.

Let me know if you have any further questions.

Thanks,
Priyanka

@Priyanka Could you provide code sample? Because I get an error when I try to follow examples from the docs

this is the queries i've tried:

  await client.getAllByType<GameDocument>('game', {
    orderings: [{ field: 'my.game.rankNumber', direction: 'asc' }],
    graphQuery: `
    {
      game {
        body {
          ...on assets_grid {
            repeat {
              ...repeatFields
            }
          }
        }
      }
    }
    `,
  });
    await client.getByUID<GameDocument>('game', params?.uid as string, {
        graphQuery: `
          {
            game {
              body {
                ...on assets_grid {
                  repeat {
                    ...repeatFields
                  }
                }
              }
            }
          }
      `,
      })

Ok, now this is ridiculous. The documentation is completely out of date, and this was addressed months ago.

Please fix the docs and stop sending people to read them if you aware that they are out of date.

I managed to find a solution by reading other threads like this or this.

@Priyanka I've found your reply in a thread similar to this one, and it has example with correct API using slices and variation fields, but in the docs there are no mentions of those. Instead of it there is a query example with body (which is unable to find) and repeat fields.

For anyone desperately looking for a working example just like me, my query which works as expected is:

await client.getByUID<GameDocument>('game', params?.uid as string, {
  graphQuery: `
    {
      game {
        ...gameFields
        slices {
          ...on assets_grid {
            variation {
              ...on default {
                items {
                  asset {
                    ...on asset {
                      ...assetFields
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  `,
})
1 Like

Hello @mlshv42

I am glad that you found the solution. You haven't mentioned complete detail in the query that it's a query for the SM slices, or else I would share this thread with you.

Our documentation team is working on a documentation update, and it will be available soon.

Thanks for your feedback.

Priyanka