Query related document on single document query

Hi,

currently I'm testing to fetch data on the server side with Next.JS and the following code:

export async function getServerSideProps({ query }) {
  const client = Client();

  const report = await client.getByUID("report", query.slug, { lang: "de-de" });

  return {
    props: {
      report
    }
  };
}

In the content model I've also integrated a repeater field with a content relationship. Now I want to query also the content of the documents from the content relationship field.

How can I do this?

Hey Marco!

Thanks for posting this question. There are a few ways to do this:

  • You could you GraphQuery (which has a similar syntax to GraphQL) to fetch content from linked documents
  • You could add a forEach loop after your first query to iterate over the repeater field and query each document
  • Or — and this is what I recommend — you can add a fetchLinks option to your query to fetch the content that you need

If you'd like to use fetchLinks, there are some instructions here:

Let me know if you have any questions about fetchLinks. And, otherwise, If you'd like more info on GraphQuery or using a foreach loop, let me know :slight_smile:

Best,
Sam

Hi @samlittlefair,

I already tried to use the fetchLinks option, but did it wrong. Now it's working.

Thanks for your help!