Query basics


Query all documents of a given type (Singletons and Repeatable)

Here's how you can query all the documents of a given type.

All documents are pulled from your repository and created as prismic${contentTypeName} and allPrismic${contentTypeName}, where ${contentTypeName} is the API ID of your document's type.

For example, if you have Page as one of your content types, you will be able to query all the existing docs by doing the following:

  • allPrismic${contentTypeName}
  • prismic${contentTypeName}
Copy
query MyQuery {
  allPrismicPage {
    edges {
      node {
        uid
      }
    }
  }
}
Copy
query MyQuery {
  prismicPage {
    uid
  }
}

Query by ID or UID

If you need to filter out the response by using the ID or UID of the documents, you can add the corresponding value to the filter argument, like in the example below.

Here we're just filtering out a document o the type Page with the UID of friday-news:

Copy
query MyQuery {
  allPrismicPage(filter: {uid: {eq: "friday-news"}}) {
    edges {
      node {
        uid
      }
    }
  }
}

Retrieve the response

Retrieving the response from a page query is pretty straight forward; you need to access the data from the page's props to render it in your components, then save the document object in a variable; we usually name it document.

In this example, we're retrieving a Title field from the allPrismicPage query:

Copy
import React from 'react'
import { graphql } from 'gatsby'
import { RichText } from 'prismic-reactjs'

export const PageTemplate = ({ data }) => {
  if (!data) return null
  const document = data.allPrismicPage.edges[0].node.data

  return (
    <div>
      <RichText render={document.example_title.raw} />
    </div>
  )
}

export const query = graphql`
  query MyPage {
    allPrismicPage {
      edges {
        node {
          example_title {
            raw
          }
        }
      }
    }
  }
`

export default PageTemplate

Arguments: filter, sort, limit & skip

To learn more about Arguments: filter, sort, limit & skip refer to the dedicated article.


Was this article helpful?
Not really
Yes, Thanks

Can't find what you're looking for? Get in touch with us on our Community Forum.