2

I'm trying to get a list of folders name in Gatsby, along with the name of the files located inside them.

Here are the 2 queries I can use :

query fetchDirectories {
  allDirectory(filter: {
    relativeDirectory: {
      regex: "/documentation/"
    }
  }) {
    edges {
      node {
        name
      }
    }
  }
}

and

query fetchFilesByDirectory($directory: String) {
  allFile(filter: {
    internal: {
      mediaType: {
        eq: "text/markdown"
      }
    }
    relativePath: {
      regex: $directory
    }
  }) {
    edges {
      node {
        name
      }
    }
  }
}

Separately, the queries are working, and I can get the good results.

In my code, I'd like to execute that second query for each directories returned by the first query.

Any idea on how to do it ?

Baiello
  • 23
  • 5
  • 5
    can you let me know if you found an answer ? I have the exact same query for the exact same usecase. – Sandeep Jan 24 '18 at 14:10

1 Answers1

0

Depending on your use case, if you're just trying to get all the MD files in /documentation/, this might be useful

{
  allFile(filter: {
    internal: {
      mediaType: {
        eq: "text/markdown"
      }
    }
    relativeDirectory: {
      regex: "/documentation/"
    }
  }) {
    edges {
      node {
        relativePath
        name
      }
    }
  }
}
Yaobin Then
  • 2,662
  • 1
  • 34
  • 54
  • hi @alvin - thanks for your reply. The challenge is that it is not one directory, its many and it needs to be fetched at build time. For example, I can add a new directory the next time I build gatsby, so it should now start considering that. – Sandeep Jan 27 '18 at 08:47