Gatsby: [1.0] Question: why can you use graphql in templates?

Created on 11 Jun 2017  Â·  6Comments  Â·  Source: gatsbyjs/gatsby

The 1.0 boilerplate has this in template pages:

export const pageQuery = graphql`
  query MyQuery($slug: String!) {
    ..
  }
`

But linters will complain here:

'graphql' is not defined.

True enough, when you try to do see what graphql is, it's not defined:

console.log(graphql)
// => undefined

How does this sorcery work?

question or discussion

Most helpful comment

Several people have asked about this — perhaps we just need to make sure this is covered in the tutorial + GraphQL docs so people aren't confused? I remember being confused for similar reasons back in the day when I first used Relay :-)

All 6 comments

The sources are processed with Babylon in file-parser.js and the queries are extracted from the AST in query-compiler.js

yeah the down and dirty reason it works is that the queries are _removed_ from the source code before they are run.

Several people have asked about this — perhaps we just need to make sure this is covered in the tutorial + GraphQL docs so people aren't confused? I remember being confused for similar reasons back in the day when I first used Relay :-)

Seems like what you really need is an eslint plugin so people aren't having to make a bunch of comments or remove their linting to support Gatsby without a bunch of errors everywhere.

I just ran into this question while setting up ESLint, so I took a crack at expanding the tutorial to explain why this doesn't explode in #2965. Someone who understands this better should probably review pretty closely, though, because I only _sorta_ get how this works. 😅

Closing this as there's another issue about including a custom gatsby eslint plugin https://github.com/gatsbyjs/gatsby/issues/2446#issuecomment-336491690

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rossPatton picture rossPatton  Â·  3Comments

andykais picture andykais  Â·  3Comments

dustinhorton picture dustinhorton  Â·  3Comments

totsteps picture totsteps  Â·  3Comments

hobochild picture hobochild  Â·  3Comments