Query by Prismic content Fields

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 gives examples of how to query Prismic documents by all the various content fields available.

Note that it isn't possible to query documents by any of these fields:

  • Color
  • Embed
  • Group or any field inside of a group
  • Images
  • Key Text
  • Rich Text
  • Slices or any fields inside of a slice

Boolean

When querying by a Boolean field, you can use the where argument. In this example we are querying all documents of the type "Article". We are filtering by a Boolean field with the API ID of is_featured and only retrieving documents where its value is equal to true.

Copy
{
  prismic {
    allArticles(where: { is_featured: true }) {
      edges {
        node {
          title
          main_image
          is_featured
        }
      }
    }
  }
}

Content Relationship

When querying by a Content Relationship field, you also use the where argument. Then you provide the API ID of the linked document. Read the full documentation here: Query by Content Relationship / Link field.

Date

There are a number of ways to query/filter content based on the Date field. Read the full documentation here: Query by Date/Timestamp.

GeoPoint

To perform a GeoPoint query, you will need to use the where argument and append _near after the GeoPoint field's API ID. Then you need to specify three variables:

  1. dist - the distance from the point of reference (in km)
  2. lng - the longitude of the reference point
  3. lat - the latitude of the reference point

The returned results will be all of the locations within the defined circle.

Example

In the following example we are querying all Documents of the type "Store" where the GeoPoint field with the API ID of location is within 10 km of these coordinates:

  • latitude: 48.8574863
  • longitude: 2.3536034
Copy
{
  prismic {
    allStores(where: {location_near: {dist: 10, lat: 48.8574863, lng: 2.3536034}}) {
      edges {
        node {
          store_name
          location
        }
      }
    }
  }
}

For the Link field, you also use the where argument. Then specify what you need from each type of link. To learn more abut this, read the full documentation here: Query by Content Relationship / Link field.

Number

In this case, you use where argument and specify the API ID of the Number field. Then you can filter by the value of that Number field. In the example below, we are querying all Documents of the type "Product" which have a Number field with the API ID of price whose value is equal to 23.

Copy
{
  prismic {
    allProducts(where: { price: 23 }) {
      edges {
        node {
          product_name
          price
        }
      }
    }
  }
}

Select

For this query, use the where argument and specify the API ID of the Select field. Then you can filter by the text value of that field. In the example below, we are querying all Documents of the type "Blog_post" which have a Select field with the API ID of category whose value is equal to "Music".

Copy
{
  prismic {
    allBlog_posts(where: { category: "Music" }) {
      edges {
        node {
          post_title
          category
        }
      }
    }
  }
}

Timestamp

There are a number of ways to query/filter content based on the Timestamp field. Read the full documentation here: Query by Date/Timestamp.

UID

There is a dedicated argument to use when querying by the UID field. Read the full documentation here: Query by ID or UID.