Resolving Linked Content in Gatsby GraphQL

While you are sourcing and querying content in gatsby, you are likely to come across a scenario where you need to retrieve a content item and its related content. In Agility CMS, Linked Content fields are used to related content to one another in various ways.

When querying a Post, for example, you may want to also retrieve the details for the Category. On your Post GraphQL node, you may notice a category property, however, it will only contain a contentID reference, not the entire node representing the Category.

You'll need to resolve this Linked Content when you need it.

How to Resolve Linked Content

In the agilitycms-gatsby-starter, we resolve Linked Content by using Gatsby Resolvers

Resolvers are added to your gatsby-node.js in your site, and they allow you to add a new field to your content node which will handle resolving your Linked Content reference.

This means you are telling GraphQL, when you query a specific property on a node, it will actually run a function to go and get your Linked Content and return it.

An example of this can be found here in our starter site:

const agility = require("./src/agility/utils")

//CREATE RESOLVERS *******************************************************************************************
exports.createResolvers = args => {
  const {
  } = args

  // here we use resolvers to resolve the data we need for linked content
  const resolvers = {
    // on the 'agilityPost' node type
    agilityPost: {
      // get the sitemap node that represents this item ( i.e. /blog/my-blog-post )
      sitemapNode: agility.getDynamicPageItemSitemapNode(),

      // get the category
      linkedContent_agilityCategory: agility.getLinkedContentItem({
        type: "agilityCategory",
        linkedContentFieldName: "category",
2 out of 2 found this helpful



Please sign in to leave a comment.