Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gatsby graphql with regex as variable

Tags:

graphql

gatsby

I would like to use regex with graphql query variable.

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?

like image 554
Dejell Avatar asked Oct 16 '25 05:10

Dejell


1 Answers

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 enter image description here

like image 59
Derek Nguyen Avatar answered Oct 18 '25 03:10

Derek Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!