Mozaik

Modeling taxonomies

How to define a content type to model a taxonomy tree

Written by Peter Balazs

Last updated: 23/07/2018

Taxonomy trees can be used for handling navigation menus, product or post categories. We are planning to implement native taxonomy support in Mozaik, but you can already set up a content type that does the same job.

Let's say we have a lot of posts in our projects, and we want to create categories to make navigation easier for our users. Let's keep our category set up simple: we only want to have a category name and a list of sub-categories. For this we need to create a simple content type with the following fields:

  • Name, a single line text field input
  • Subcategories: a Document reference field, that accepts multiple Category document

And also we need a Post content type for our posts.

We can describe it by the Mozaik SDL:

type Category implements SimpleContentType {
  name: SinglelineText
  subcategories: [Category]
}
type Post implements SimpleContentType {
  title: SinglelineText @config(isTitle: true)
  postContent: RichText
  categories: [Category]
}

Once we have our content types and we added a few categories and posts we can start retrieving data through the API:

Getting the taxonomy list

query GetTaxonomyList {
  getCategoryDocumentList {
    items {
      id
      slug
      name
      subcategories {
        id
        slug
        name
      }
    }
  }
}

Getting all documents by a category

query GetPostsByCategory {
  getPostDocument(
    filter: {
      categories_some: {
        slug_is: "category-slug"
      }
    }
  ) {
  pagination {
    page
    pageCount
  }
  items {
    id
    title
    postContent {
      html
    }
  }
}