question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gatsby should render query, gets error instead

See original GitHub issue

Description

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

  1. Create a new gatsby project with gatsby new
  2. 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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
herecydevcommented, Apr 14, 2020

You need to use the useStaticQuery. So your code should be:

import React from 'react';

import { graphql, useStaticQuery } from 'gatsby';

export default () => {
  const data = useStaticQuery(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>
  )
}
1reaction
vladarcommented, Apr 14, 2020

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! 💜

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found