Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch graphql errors from the graphql function and pretty-print errors #15212

Closed
KyleAMathews opened this issue Jun 28, 2019 · 1 comment · Fixed by #15227
Closed

Catch graphql errors from the graphql function and pretty-print errors #15212

KyleAMathews opened this issue Jun 28, 2019 · 1 comment · Fixed by #15227

Comments

@KyleAMathews
Copy link
Contributor

Description

If in your gatsby-node.js you send a bad graphql query to the graphql function (this is commonly done to query data to create pages), you won't get any error. Instead you'll generally get an error that you can't access expected data from the graphql result e.g.

Screenshot 2019-06-28 07 28 22

Ideally we'd catch errors return from the graphql function and create structured errors for common problems.

Steps to reproduce

Create a site add add the following code to your gatsby-node.js:

exports.createPages = ({ graphql, actions }) => {
  return graphql(
    `
      query HomePageQuery {
        site {
          # Typo here
          siteMetadat {
            description
          }
        }
      }
    `
  ).then(result => {
    const doesthisexist = result.data.siteMetadat
  })
}

If you run gatsby build, it'll give an error like the following:

Screenshot 2019-06-28 07 35 27

It should instead show that there was an error with your graphql query including what file/line number it happened in and the GraphQL error message.

Category

Categorize the error. Is it from GraphQL, Webpack, or something else?

GraphQL

Grade

Give the error a grade according to the key at the bottom of this issue.

0

@KyleAMathews
Copy link
Contributor Author

We could also validate queries in Relay Compiler (as I suggested here: #1595)

That'd give us richer error messages potentially.

KyleAMathews added a commit that referenced this issue Jun 28, 2019
KyleAMathews added a commit that referenced this issue Jun 29, 2019
…sed in gatsby-node.js (#15227)

* Show structured errors for graphql errors from the graphql function used in gatsby-node.js

Fixes #15212

* Update packages/gatsby/src/bootstrap/index.js

Co-Authored-By: Mike Allanson <michael.allanson@gmail.com>

* Filter out null errors & mark error as from graphql function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant