Query by Content Relationship / Link field

Prismic no longer recommends the gatsby-source-prismic-graphql plugin

With recent changes to Gatsby, Prismic no longer recommends the gatsby-source-prismic-graphql plugin that this documentation uses. Read more about the future of Prismic and Gatsby. We highly recommend using the gatsby-source-prismic instead. The documentation for this plugin can be found in the plugin's github README.

We will leave this documentation here for now, but will change it in the future when we determine the best approach for Prismic & Gatsby.

This page shows you how to filter documents by a Content Relationship or Link field

It must be a top-level field

When using the Content Relationship or Link field as arguments, they have to be at the top level of a Custom Type. It's not possible use them as an arguments if the field is inside a Group field or a Slice.

The GraphQL Query

When querying by a Content Relationship or Link field, you will need to use the where argument as shown in these examples.

Here is an example that shows how to query all documents of the type Post whose Content Relationship or Link field has the API ID of "newlink" has the value of "XmZAAhMAACAARcwc". This value is the ID of the linked document.

You must use the document ID

This last value can only be the ID of a given document. If you use a different value, for example: a UID field, the query won't work.

In this example, we will return the linked document using a union type (in this case it's a document of the type Page) Here you can read more about how to retrieve linked document content.

Copy
{
  prismic {
    allPosts(where: {newlink: "XmZAAhMAACAARcwc"}) {
      edges {
        node {
          newlink {
            _linkType
            ... on PRISMIC_Page {
              _meta {
                id
              }
            }
          }
        }
      }
    }
  }
}