Notes

Common GraphQL queries for Gatsby

Edit on GitHub

Gatsby
2 minutes

NOTES:

  • can’t do string interpolation or use props in a static query

Get all files in a specific folder

You can do that with sourceInstanceName on file

1{
2  allFile(filter: { sourceInstanceName: { eq: "notes" } }) {
3    nodes {
4      sourceInstanceName
5      name
6    }
7  }
8}

notes is the folder in the above query, and you’ll get the names of all files in that folder. Without that filter, you’ll get something like this

1{
2  allFile {
3    nodes {
4      sourceInstanceName
5      name
6    }
7  }
8}
 1{
 2  "data": {
 3    "allFile": {
 4      "nodes": [
 5        {
 6          "sourceInstanceName": "images",
 7          "name": "Banner-Aamnah-LinkedIn"
 8        },
 9        {
10          "sourceInstanceName": "images",
11          "name": "Logo-white"
12        },
13        {
14          "sourceInstanceName": "images",
15          "name": "Logo"
16        },
17        {
18          "sourceInstanceName": "images",
19          "name": "gateron-switch-3pin-5pin"
20        },
21        {
22          "sourceInstanceName": "images",
23          "name": "email-notification-settings"
24        },
25        {
26          "sourceInstanceName": "images",
27          "name": "netlify-contact-submission-response"
28        },
29        {
30          "sourceInstanceName": "images",
31          "name": "CherryMX-Switch-Guide"
32        },
33        {
34          "sourceInstanceName": "pages",
35          "name": "404"
36        },
37        {
38          "sourceInstanceName": "pages",
39          "name": "blog"
40        },
41        {
42          "sourceInstanceName": "pages",
43          "name": "index"
44        },
45        {
46          "sourceInstanceName": "blog",
47          "name": "first-mechanical-keyboard"
48        },
49        {
50          "sourceInstanceName": "blog",
51          "name": "setup-gatsby-mdx-blog"
52        },
53        {
54          "sourceInstanceName": "blog",
55          "name": "ukulele-notes"
56        },
57        {
58          "sourceInstanceName": "blog",
59          "name": "14_days_social_isolation"
60        },
61        {
62          "sourceInstanceName": "notes",
63          "name": "gatsby-netlify-contact-form"
64        },
65        {
66          "sourceInstanceName": "notes",
67          "name": "gatsby_graphql_queries"
68        }
69      ]
70    }
71  }
72}

It picks up the stuff in images, blog, notes and pages directories..