Find usages of model or block

Is it possible to find all records where a certain model or block is used?

When making changes to the rendering of some model or block I want to be able to find examples of where it is used to avoid I’m breaking anything.

Right now I use this workflow which is kind of cumbersome:

  1. Run a script I’ve written that downloads all content via the Content Management API
  2. Use ag to search all content for the ID of the model/block
  3. Use jq to extract the .attributes.slug to be able to visit the page where the block is used

I know of the Used in X fields dropdown in the admin UI but that doesn’t help me if the block is used in a BlogPage but I don’t know which BlogPage it is…

1 Like

@gabriel1,

I am not sure if this is exactly what you mean, but we can show you all the fields that a model/block is used in: https://www.datocms.com/docs/content-management-api/resources/field/referencing?language=http

This is the same data you get in the UI when you click on the “Used in X fields” link under a block or model name:


Is that kinda what you mean, or am I misunderstanding?

Hmm… if you’re trying to find “records that contain block of type myBlock”, you can turn on GraphQL Deep Filtering for the field that contains the block (a MCF or structured text, usually) and do a query like:

query MyQuery {
  allPosts(first:500, filter: {content: {blocks: {containsAny: {imageBlock: "true"}}}}) {
    content {
      blocks {
        __typename
      }
    }
  }
}

That will find you all the posts that contain an imageBlock:

Does that help…?