Retrieve content from 'content relationship' field in non-repeatable zone of slice

Hi there! I'm loving Prismic so far but have hit a roadblock with the GraphQuery API when trying to retrieve the content from a content relationship field in the non-repeatable zone of a slice. Hoping someone can help :pray: – I'm using Next.js if that makes any difference, and have successfully used the GraphQuery flow elsewhere on the project, this is just a more complex query!

So, I have this query below which works fine, but does not seem to provide any further information (or data) on the track content type that I am trying to link to (in the slice also called track). I had kind-of expected adding ...non-repeatFields to add this in.

const projectGraphQuery = `{
  project {
    ...projectFields
    body {
      ...on track {
        non-repeat {
          ...non-repeatFields
        }
      }
    }
  }
}`;

I've tried variations on the query, such as this one (below) – but I get 400 errors without much feedback.

const projectGraphQuery = `{
  project {
    ...projectFields
    body {
      ...on track {
        non-repeat {
          ...non-repeatFields
          track {
            ...trackFields
          }
        }
      }
    }
  }
}`;

I am sure this is just a problem with my understanding of the API. The question seems similar to others on the forum, but I don't seem to be able to find answers on how to integrate this with the new GraphQuery API (this post answers with FetchLinks I think?) or specifically in the non-repeat zone of a slice (I'm not sure if that makes a difference!).

Any help would be greatly appreciated :slightly_smiling_face:

Hello,@warecollective

Welcome to the Prismic forum and thanks for reaching out to us.

The Content Relationship field doesn't provide any content from the linked document unless you specify in the GraphQuery. Please follow the guidelines mentioned here.

I believe you need to change your graphQuery like this:

const projectGraphQuery = `{
  project {
    ...projectFields
    body {
      ...on track {
        non-repeat {
          ...non-repeatFields
          name_of_content_relationship_field {
             ... on linked_doc_type {
             ...linked_doc_type_Fields
           }
         }
        }
      }
    }
  }
}`;

Let me know after trying this.

Priyanka

Hi @Priyanka, thanks so much for your quick reply!

I managed to get it working after your comment with this:

const projectGraphQuery = `{
  project {
    ...projectFields
    body {
      ...on track {
        non-repeat {
          ...non-repeatFields
          track {
            ...on track {
              ...trackFields
            }
          }
        }
      }
    }
  }
}`;

I'll confess, I think one thing that was confusing me from the guidelines was the conventions around ellipses (...), spaces and underscores – which is embarrassing for me :sweat_smile:!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.