Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between file and allFile in the GraphQL query in Gatsby?

Tags:

graphql

gatsby

In my GraphiQL explorer, it appears that I can query file or allFile, but I don't really understand what the difference between the two is. In fact, every query appears to be "duplicated" in this manner. Can someone explain, or point me to some documentation that explains the difference and when I should use one over the other? GraphQL query

like image 588
Ashwin Avatar asked Sep 12 '25 09:09

Ashwin


1 Answers

allFile (notice the all prefix) stands for all GraphQL assets of file node type that Gatsby has inferred using the gatsby-source-filesystem, while file is an isolated node that would need to be filtered to get the specific object.

In other words, allFile is exposing all files in your project (at least the ones that Gatsby is aware of), which will give you an array of nodes while file is only pointing to a specific GraphQL node. This single and isolated node normally needs to be filtered to get them among the rest. The same reasoning applies to allMdx and mdx, allSite and site, etc.

For example, using allMdx example (node created by gatsby-plugin-mdx): let's say you have a blog project where you store your posts in MDX files. In your gatsby-node.js you will query for allMdx node in order to get all posts, then, you will loop through the results and create dynamic pages for each post (using createPage method). In the post template (the template that each specific post will use), instead of querying for allMdx, you will need to use mdx because it's pointing specifically to a single mdx node. Of course, in your template, you can still use allMdx, for it's not the optimal GraphQL node since you have a specific mdx that will be your single post, filtered by some unique value like the slug, the id, etc.

like image 75
Ferran Buireu Avatar answered Sep 16 '25 08:09

Ferran Buireu