简体   繁体   English

以正则表达式为变量的 Gatsby graphql

[英]Gatsby graphql with regex as variable

I would like to use regex with graphql query variable.我想将正则表达式与 graphql 查询变量一起使用。

This does't return results:这不会返回结果:

export const query = graphql`
  query(
    $episodes: String!
    ) {
    episodes: allMarkdownRemark(
      filter: { fields: { slug: { regex: $episodes } } }
    ) {
      edges {
        node {
          id
        }
      }
    }
  }
`;

However, this would work:但是,这会起作用:

export const query = graphql`
  query() {
    episodes: allMarkdownRemark(
      filter: { fields: { slug: { regex: "/episodes/travel/" } } }
    ) {
      edges {
        node {
          id
        }
      }
    }
  }
`;

what's wrong?怎么了?

Passing regex via query arguments should work, see the screenshot below.通过查询参数传递正则表达式应该可以工作,请参见下面的屏幕截图。 Make sure you're passing in the regex as string, not the actual regex.确保您将正则表达式作为字符串传递,而不是实际的正则表达式。 Also, you'll need to escape the middle slash:此外,您需要转义中间斜线:

    context: {
-     episodes: /episodes\/traveller/             <-- doesn't work
+     episodes: /episodes\/traveller/.toString()  <-- works
or    episodes: "/episodes\\/traveller/"          <-- also works
    }

Try it out in one of the graphiQL embed in this page此页面中嵌入的其中一个 graphiQL 中尝试一下在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM