Skip to content

Commit

Permalink
add warning about query that is not getting executed if it's exported…
Browse files Browse the repository at this point in the history
… from file that is page/template or layout
  • Loading branch information
pieh committed Feb 2, 2018
1 parent 8798f08 commit d8085f0
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions packages/gatsby/src/internal-plugins/query-runner/query-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,37 @@ const { boundActionCreators } = require(`../../redux/actions`)
const queryCompiler = require(`./query-compiler`).default
const queue = require(`./query-queue`)
const normalize = require(`normalize-path`)
const report = require(`gatsby-cli/lib/reporter`)

exports.extractQueries = () => {
const state = store.getState()
const pagesAndLayouts = [...state.pages, ...state.layouts]
const components = _.uniq(pagesAndLayouts.map(p => p.component))
const components = _.uniq(pagesAndLayouts.map(p => normalize(p.component)))
const queryCompilerPromise = queryCompiler().then(queries => {
components.forEach(component => {
const query = queries.get(normalize(component))
boundActionCreators.replaceComponentQuery({
query: query && query.text,
componentPath: component,
})
let queryWillNotRun = false

queries.forEach((query, component) => {
if (_.includes(components, component)) {
boundActionCreators.replaceComponentQuery({
query: query && query.text,
componentPath: component,
})
} else {
report.warn(
`GraphQL query in component "${component}" will not be run!`
)
queryWillNotRun = true
}
})

if (queryWillNotRun) {
report.log(report.stripIndent`
Queries are only executed for Page or Layout components. Instead of a query, co-locate a GraphQL fragment and compose that fragment into the query (or other
fragment) of the top-level page or layout that renders this component. For more
info on fragments and composition see: http://graphql.org/learn/queries/#fragments
`)
}

return
})

Expand Down

0 comments on commit d8085f0

Please sign in to comment.