gatsbyjs / gatsby

The best React-based framework with performance, scalability and security built in.
https://www.gatsbyjs.com
MIT License
55.27k stars 10.31k forks source link

Feature Request: better error message if templates in /pages/ and get an error #24125

Closed muescha closed 3 years ago

muescha commented 4 years ago

Summary

if templates are placed "accidently" in /pages then they run twice, one time from gatsby-node.js and one more time "unexpected" with the automatic /pages step

Basic example

see Issue:

 ERROR #85920  GRAPHQL

There was an error in your GraphQL query:

Variable "$skip" of required type "Int!" was not provided.

   8 |
>  9 | query IndexQuery($skip: Int!, $limit: Int!) {
     |                  ^
  10 |   site {
  11 |     siteMetadata {

File path: /Users/muescha/Work/gatsby/github/issue-24010/kinnikumegane/myblog/src/pages/index.jsx
Url path: /
Plugin: none

You might have a typo in the variable name "$skip" or you didn't provide the variable via context to this page query. Have a look at the docs to learn how to add data to context:

https://www.gatsbyjs.org/docs/page-query/#how-to-add-query-variables-to-a-page-query

If you're not using a page query but a StaticQuery / useStaticQuery you see this error because they currently don't support variables. To learn more about the limitations of StaticQuery / useStaticQuery, please visit these docs:

https://www.gatsbyjs.org/docs/static-query/
https://www.gatsbyjs.org/docs/use-static-query/

Improvements

possible improvements:

1) Context

print also the context variables when a graphql error is printed, then a user see if he have just a typo or all other variables are missing.

2) Hints in error text

add a hint to the error message about automatic running the pages when the file is placed in /pages/

3) Add warnings

add a check if a file from /pages/ is called from createPage( in gatsby-node.js (or in a similar lifecycle step other than the automatic pages step) and print a warning - maybe as an example:

warn The template /src/pages/index.jsx` is called from `createPage` in `gatsby-node.js` but also called one time more from the xxx livecycle

there exists a similar check for the templates:

warn The GraphQL query in the non-page component "/www/src/templates/tags.js" will not be run.

Motivation

For new users it is hard to find out why the variables are missing in this case

Related Issues:

LekoArts commented 4 years ago

Yeah, makes sense to improve this. Thanks for the issue!

muescha commented 4 years ago

related issue with same problem (template used from src/pages: