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.

Improve query parsing to recognize GraphQL queries outside export statements

See original GitHub issue

Currently, Gatsby uses a combination of AST introspection and tagged template literal extraction to find and extract all of the graphql queries that pages can use. This works well if you have something that looks like this:

export const pageQuery = graphql`
  query Think {
     siteMetadata {
       title
    }
  }
`

The problem arises if you deviate from this at all. There are no warnings or errors, just no data passed to the component. This came up when working with ReasonML and Gatsby. Bucklescript can generate es6 code (though this opens up other issues like needing to compile node_modules) but what it creates is this (totally valid)

var pageQuery = graphql`
  query Think {
     siteMetadata {
       title
    }
  }
`

export { pageQuery }

Even though this is valid ES6, Gatsby can’t find the query because the ExportNamedDeclaration isn’t where the tagged template literal actually is found.

To further the issue, Gatsby no longer works with commonjs exports (https://github.com/gatsbyjs/gatsby/commit/41c5ce67b80484bb6417788db043ce578d0d9cd3) so a much simpler solution of exports.pageQuery = graphql will not work either.

@KyleAMathews I don’t know babel transforms well enough to immediately suggest a solution, but would be happy to help with some guidance here.

For anyone who runs into this, the current work around with something like Reason, is to generate pages outside of the pages folder, them import the components using a normal .js file where you place the query. This isn’t great at all, but at least it works.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
loudmouthcommented, Dec 5, 2018

I’m a bit intimidated by making a PR on the Gatsby AST parsing, but I just wanted to Plus 1 that it would be awesome to enable Gatsby GraphQL queries in Reason files!

0reactions
ctbuchacommented, Nov 1, 2018

From @jbaxleyiii 's advice, my current workaround is to use a regular ReactJS component and then just immediately pass the data down to the ReasonReact component.

import React from 'react'
import { graphql } from 'gatsby'
import MarkdownContent from './MarkdownContent.bs'

export default ({ data }) => { 
  console.log(data)
  const title = data.markdownRemark.frontmatter.title
  const content = data.markdownRemark.html
  return <MarkdownContent title={title} content={content} />
}

export const query = graphql`
  query BlogPostBySlug($slug: String!) {
    site {
      siteMetadata {
        title
      }
    }
    markdownRemark(fields: { slug: { eq: $slug } }) {
      id
      excerpt
      html
      frontmatter {
        title
        date(formatString: "MMMM DD, YYYY")
        author
      }
    }
  }
`
Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack'ing your GraphQL Documents
This makes it easy to place GraphQL documents outside of your view ... export const COMMENT_QUERY = gql` query Comment($repoName: String!)
Read more >
Five Common Problems in GraphQL Apps (And How to Fix ...
Schema duplication; Server/client data mismatch; Superfluous database calls; Poor performance; Boilerplate overdose. I'm willing to bet your app ...
Read more >
Support importing top-level GraphQL queries into page and ...
If either file contains a graphQL query, during the build process, ... and uses a default export it doesn't find, parse or run...
Read more >
How to abstract a graphQL query out of the component?
exports export default , etc.). Here's all that I've tried: Using webpack: /* eslint-disable no-unused-vars */ import withCSS from '@zeit ...
Read more >
Part 4: Query for Data with GraphQL - Gatsby
How do you get data back out of the data layer? You can write GraphQL queries inside of your components to pull out...
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