Gridsome source-filesystem add tag description

288 views Asked by At

I'm using @gridsome/source-filesystem with this config:

{
  use: '@gridsome/source-filesystem',
  options: {
    typeName: 'Post',
    path: 'content/posts/**/*.md',
    refs: {
      tags: {
        typeName: 'Tag',
        create: true
      },
      author: {
        typeName: 'Author',
        create: true
      }
    }
  },
}

Now I want to add description for one tag only, so I created a new doc in content/posts/my-tag.md:

---
title: Tag-Title
description:tag description
---

How can I connect this document to the allTags collection?

Or any other way (without @gridsome/source-filesystem for Tags, for example), to add description to exists node in collection?

1

There are 1 answers

4
nabeen On BEST ANSWER

If you want to just add allTags, you can create markdown for it.

in gridsome.config.js

...
    {
      use: '@gridsome/source-filesystem',
      options: {
        path: 'content/tags/**/*.md',
        typeName: 'Tag'
      },
    }
...

add file content/tags/my-tag.md

---
title: Tag-Title
description: tag description
---

you can explole

{
  allTag {
    edges {
      node {
        id
        title
        description
      }
    }
  }
}
{
  "data": {
    "allTag": {
      "edges": [
        {
          "node": {
            "id": "******", // random hash
            "title": "Tag-Title",
            "description": "tag description"
          }
        },
        {
          "node": {
            "id": "foo",
            "title": "foo",
            "description": ""
          }
        },
...

but, this is not able to connect to your Post.

or just added description to Tag, you can use addSchemaResolvers. in gridsome.server.js

module.exports = function(api) {
  api.loadSource(async ({ addSchemaResolvers }) => {
    addSchemaResolvers({
      Tag: {
        description: {
          type: "String",
          resolve(obj) {
            if (!obj.description) {
              // coding your logic
              return "set description";
            } else {
              return obj.description;
            }
          }
        }
      }
    });
  });
};