Retrieve Link field content

This page will show you how to retrieve the content from a Link field.

Start by defining the Link field you want to retrieve.

In the following example, we are querying for all the documents of the type "blog_post". In this case, the API ID of the Link field is page_link. We are also specifying the __typename field so that we can see the field names we need to use in the next step of this process.

Copy
query{
  allBlog_posts{
    edges{
      node{
        page_link{
          __typename
        }
      }
    }
  }
}

Next we need to add a Union type in order to specify which fields we need for each link type. The Union type will look something like this:

... on _ExternalLink

There are three different kinds of links:

External link

This is a link to the web in the form of a full website url, a phone link, or an email link. The union type for this is specified as _ExternalLink.

Media item link

This is a link to an item from your Prismic repository's Media Library. The union type for this is specified as _FileLink.

Document link

This is a link to document in your Prismic repository. The union type for this depends on the name of the linked document. Learn more about this on the Fetch Linked Items page.

Here is an example that will give you different fields depending on the type of link. In this case you could get content from an External link, Media link, or a Document link to a "page" type.

Copy
query{
  allBlog_posts{
    edges{
      node{
        page_link{
          __typename
          ... on _ExternalLink{
            url
          }
          ... on _FileLink{
            name
            url
            size
          }
          ... on Page{
            title
            description
            _meta{
              uid
            }
          }
        }
      }
    }
  }
}