Gatsby: 开发和生产中的不同GraphQL查询

创建于 2019-03-10  ·  1评论  ·  资料来源: gatsbyjs/gatsby

有没有一种方法可以根据网站是以开发模式还是生产模式运行来修改页面组件中的GraphQL查询?

question or discussion

最有用的评论

您不能直接操纵查询文本,但是可以根据开发环境或生产模式来传递不同的上下文。

如果这是指https://github.com/gatsbyjs/gatsby/issues/10844#issuecomment -471349943中描述的问题

以编程方式创建博客列表,因此您可以传递context进行查询

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

然后在查询中:

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

它应该显示dev中的所有节点,并过滤掉生产中draft设置为true所有节点。 可能存在一些缓存问题(如果在开发后运行构建,我们将不会重新运行查询atm)。 但是,如果您使用netlify (或其他种类的CI / CD)进行生产构建,则应该可以使用

>所有评论

您不能直接操纵查询文本,但是可以根据开发环境或生产模式来传递不同的上下文。

如果这是指https://github.com/gatsbyjs/gatsby/issues/10844#issuecomment -471349943中描述的问题

以编程方式创建博客列表,因此您可以传递context进行查询

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

然后在查询中:

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

它应该显示dev中的所有节点,并过滤掉生产中draft设置为true所有节点。 可能存在一些缓存问题(如果在开发后运行构建,我们将不会重新运行查询atm)。 但是,如果您使用netlify (或其他种类的CI / CD)进行生产构建,则应该可以使用

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

totsteps picture totsteps  ·  3评论

magicly picture magicly  ·  3评论

mikestopcontinues picture mikestopcontinues  ·  3评论

Oppenheimer1 picture Oppenheimer1  ·  3评论

ferMartz picture ferMartz  ·  3评论