Skip to content

Commit

Permalink
fix(gatsby): Improve error message when calling useStaticQuery withou…
Browse files Browse the repository at this point in the history
…t graphql (#23189)
  • Loading branch information
blainekasten authored Apr 21, 2020
1 parent 59992b7 commit 6a079fb
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/gatsby/cache-dir/gatsby-browser-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ const useStaticQuery = query => {
)
}
const context = React.useContext(StaticQueryContext)

// query is a stringified number like `3303882` when wrapped with graphql, If a user forgets
// to wrap the query in a grqphql, then casting it to a Number results in `NaN` allowing us to
// catch the misuse of the API and give proper direction
if (isNaN(Number(query))) {
throw new Error(`useStaticQuery was called with a string but expects to be called using \`graphql\`. Try this:
import { useStaticQuery, graphql } from 'gatsby';
useStaticQuery(graphql\`${query}\`);
`)
}

if (context[query] && context[query].data) {
return context[query].data
} else {
Expand Down

0 comments on commit 6a079fb

Please sign in to comment.