最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing variable into regex In Gatsby graphql query - Stack Overflow

programmeradmin0浏览0评论

I have the following query which is receiving the variable $tag. Currently I am filtering the results based on the value of its frontmatter.keywords. keywords is a ma separated string, so I need to use a regex to check for the inclusion of $tag within it, however I can't work out how to pass the variable into the regex. If I hardcode a value into the regex (as in the code below where I have hardcoded /example/, the filtering works. If I replace example with $tag I receive an Error:

GraphQLError: Variable "$tag" is never used in operation "TagQuery".

export const pageQuery = graphql`
  query TagQuery($tag: String) {
    allMarkdownRemark(
      limit: 100
      sort: { fields: [frontmatter___date], order: DESC }
      filter: { frontmatter: { keywords: { regex: "/example/" } } }
    ) {
      totalCount
      edges {
        node {
          fields {
            slug
          }
          excerpt
          frontmatter {
            title
            keywords
            date
          }
        }
      }
    }
  }
`;

How should I use $tag within the regex?

I'd actually prefer to take a different approach and add the tags as an array in gatsby-node.js, but there doesn't appear to be any way of filtering based on the value of the array.

I have the following query which is receiving the variable $tag. Currently I am filtering the results based on the value of its frontmatter.keywords. keywords is a ma separated string, so I need to use a regex to check for the inclusion of $tag within it, however I can't work out how to pass the variable into the regex. If I hardcode a value into the regex (as in the code below where I have hardcoded /example/, the filtering works. If I replace example with $tag I receive an Error:

GraphQLError: Variable "$tag" is never used in operation "TagQuery".

export const pageQuery = graphql`
  query TagQuery($tag: String) {
    allMarkdownRemark(
      limit: 100
      sort: { fields: [frontmatter___date], order: DESC }
      filter: { frontmatter: { keywords: { regex: "/example/" } } }
    ) {
      totalCount
      edges {
        node {
          fields {
            slug
          }
          excerpt
          frontmatter {
            title
            keywords
            date
          }
        }
      }
    }
  }
`;

How should I use $tag within the regex?

I'd actually prefer to take a different approach and add the tags as an array in gatsby-node.js, but there doesn't appear to be any way of filtering based on the value of the array.

Share Improve this question edited Oct 25, 2017 at 13:24 Undistraction asked Oct 25, 2017 at 13:03 UndistractionUndistraction 43.4k62 gold badges205 silver badges336 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I ended up splitting the tags into an array, then using the following to filter:

filter: { fields: { tags: { in: [$tag] } } }

Wherever you are passing $tag, just transform it to

const $tag = "\/".concat(tag).concat("\/")

But your error is specifically pointing to the fact that your query is not using the variable at all. So you'd also need to do

filter: { frontmatter: { keywords: { regex: $tag } } }
发布评论

评论列表(0)

  1. 暂无评论