Gatsby should render query, gets error instead
See original GitHub issueDescription
This is what I’m doing
import React from 'react';
import { graphql, useStaticQuery } from 'gatsby';
export default () => {
const data = graphql(`
query {
allFile {
edges {
node {
name
publicURL
}
}
}
}
`)
console.log('data', data);
return (
<React.Fragment>
<h2>Images</h2>
{data.allFile.edges.map(edge => <div><img alt={edge.node.name} src={edge.node.publicURL} />{edge.node.name}</div>)}
</React.Fragment>
)
}
This query renders fine on http://localhost:8000/___graphql
Steps to reproduce
- Create a new gatsby project with
gatsby new
- add components
pages/images.js
, with the following content:
import React from 'react';
import { graphql, useStaticQuery } from 'gatsby';
export default () => {
const data = graphql(`
query {
allFile {
edges {
node {
name
publicURL
}
}
}
}
`)
console.log('data', data);
return (
<React.Fragment>
<h2>Images</h2>
{data.allFile.edges.map(edge => <div><img alt={edge.node.name} src={edge.node.publicURL} />{edge.node.name}</div>)}
</React.Fragment>
)
}
See error output:
Error: It appears like Gatsby is misconfigured. Gatsby related `graphql` calls are supposed to only be evaluated at compile-time, and then compiled away. Unfortunately, something went wrong and the query was left in the compiled code.
Unless your site has a complex or custom babel/Gatsby configuration this is likely a bug in Gatsby.
Expected result
What should happen?
Actual result
What happened.
Environment
Run gatsby info --clipboard
in your project directory and paste the output here.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Troubleshooting Common Errors - Gatsby
If the error is describing an Unknown field 'X' on type 'Query' , the content type you are trying to source is likely...
Read more >GatsbyJS having issues rendering GraphQL queries from ...
I'm dynamically creating pages with a gatsby-node.js file. When I try to access one of the dynamically created slugs, I get an error...
Read more >Data fetching with Gatsby and GraphQL - LogRocket Blog
Use GraphQL to fetch data from a Gatsby configuration and different sources including the file system, external APIs, databases, and CMSs.
Read more >Rendering Engines Fail Error : r/gatsbyjs - Reddit
0 and npm install then ran gatsby clean and then build, still getting the same error. Now I will try to uninstall all...
Read more >Blogging with Gatsby & MDX - React Training
MDX is a superset of Markdown that lets you import and render ... The error goes away because GraphQL can now query for...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You need to use the
useStaticQuery
. So your code should be:Thank you for opening this, @softchris
We’re marking this issue as answered and closing it for now but please feel free to reopen this and comment if you would like to continue this discussion. We hope we managed to help and thank you for using Gatsby! 💜