Gatsby: Different GraphQL query in development and production

Created on 10 Mar 2019  ·  1Comment  ·  Source: gatsbyjs/gatsby

Is there a way to modify the GraphQL queries in a page component depending on whether the site is running in development or production mode?

question or discussion

Most helpful comment

You can't directly manipulate query text, but you can pass different context depending if you are in dev or in prod mode.

If this is referring to issue described in https://github.com/gatsbyjs/gatsby/issues/10844#issuecomment-471349943

programatically create blog listing, so you can pass context to query

createPage({
  path: "/blog",
  template: <some_template>,
  context: {
    draftBlacklist: process.env.NODE_ENV === `production` ? [true] : []
  }
})

and then in query:

query BlogListing($draftBlacklist: [String!]!) {
  allMarkdownRemark(filter: {frontmatter: {draft: {nin: $draftBlacklist}}}) {
    edges {
      node {
        frontmatter {
          title
          draft
        }
      }
    }
  }

Which should show all nodes in dev, and filter out all nodes with draft set to true in production. There might be some caching issue (if you run build after develop, we will not rerun query atm). But if you use netlify (or other kinds of CI/CD) for production builds this should work

>All comments

You can't directly manipulate query text, but you can pass different context depending if you are in dev or in prod mode.

If this is referring to issue described in https://github.com/gatsbyjs/gatsby/issues/10844#issuecomment-471349943

programatically create blog listing, so you can pass context to query

createPage({
  path: "/blog",
  template: <some_template>,
  context: {
    draftBlacklist: process.env.NODE_ENV === `production` ? [true] : []
  }
})

and then in query:

query BlogListing($draftBlacklist: [String!]!) {
  allMarkdownRemark(filter: {frontmatter: {draft: {nin: $draftBlacklist}}}) {
    edges {
      node {
        frontmatter {
          title
          draft
        }
      }
    }
  }

Which should show all nodes in dev, and filter out all nodes with draft set to true in production. There might be some caching issue (if you run build after develop, we will not rerun query atm). But if you use netlify (or other kinds of CI/CD) for production builds this should work

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Oppenheimer1 picture Oppenheimer1  ·  3Comments

timbrandin picture timbrandin  ·  3Comments

kalinchernev picture kalinchernev  ·  3Comments

dustinhorton picture dustinhorton  ·  3Comments

hobochild picture hobochild  ·  3Comments