Query all your documents

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 will explain how to query all the documents from your Prismic repository. The complexity of your query(ies) depends on how many Custom Types and documents you have in your repository.

The _allDocuments operation

By definition the Gatsby GraphQL API will require you to define the fields that you want to retrieve.

With the _allDocuments query field, the API will return all the documents in your repository. Since each custom type has different fields, you need to define which fields you want to retrieve for a specific Custom Type. The only exception is the metadata which is the same for all documents regardless of the type.

Let's first look at how to retrieve the metadata.

Retrieve the metadata

If you want to have access to the metadata, you can directly retrieve it using the _meta field. In the example below we will retrieve the ID, UID, and the type of all the documents.

Copy
query Everything {
  prismic {
    _allDocuments {
      edges {
        node {
          _meta {
            id
            uid
            type
          }
        }
      }
    }
  }
}

Retrieve content fields

Every Custom Type in your repository might have different fields, so we will need to use a switch to define which fields to retrieve depending on the type.

The example below shows how to retrieve the "site_title" & "tagline" fields from the Homepage documents, the "title" & "blog_text" fields from all Blog Post documents, and the "name" & "avatar" fields from all the Author documents. It also retrieves some metadata from all documents.

Copy
query Everything {
  prismic {
    _allDocuments {
      edges {
        node {
          _meta {
            id
            uid
            type
          }
          ... on PRISMIC_Homepage {
            site_title
            tagline
          }
          ... on PRISMIC_Blog_post {
            title
            blog_text
          }
          ... on PRISMIC_Author {
            name
            avatar
          }
        }
      }
    }
  }
}

Note about Multi-language

If your Prismic repository has content in more than one language, then this query will return documents in all your languages. Check out the How to query by language page to learn how to retrieve documents for a single language only.

Pagination

It's worth noting that the Gatsby GraphQL API will automatically paginate your results to 20 items. If you have more than 20 documents that you are trying to retrieve, then you will need to run more than one query to retrieve it all. Check out our page about Pagination to learn more.