Gatsby.js条件查询

Gatsby.js条件查询,gatsby,Gatsby,我希望在我的组件中使用条件查询,如下所示: export const pageQuery = DATA_SOURCE === 'strapi' ? graphql` query AllNews { allStrapiApiupdates(sort: { order: DESC, fields: [date] }) { edges { node { id

我希望在我的组件中使用条件查询,如下所示:

export const pageQuery =
  DATA_SOURCE === 'strapi'
    ? graphql`
        query AllNews {
          allStrapiApiupdates(sort: { order: DESC, fields: [date] }) {
            edges {
              node {
                id
                title
                description
                date(formatString: "DD.MM", locale: "ru")
              }
            }
          }
        }
      `
    : graphql`
        query AllNews {
          allMarkdownRemark(
            sort: { order: DESC, fields: [frontmatter___date] }
            filter: { frontmatter: { type: { eq: "news" } } }
          ) {
            edges {
              node {
                id
                frontmatter {
                  title
                  date(formatString: "DD.MM", locale: "ru")
                  type
                }
                html
              }
            }
          }
        }
      `
但我有一个错误:

Uncaught (in promise) ReferenceError: graphql is not defined at Module.eval (D:/Projects/AHML-DWH/api-news-front/src/pages/index.tsx:68) at eval (D:/Projects/AHML-DWH/api-news-front/src/pages/index.tsx:146) at Module../src/pages/index.tsx (commons.js:9487) ... 未捕获(承诺中)引用错误:未定义graphql 在Module.eval(D:/Projects/AHML-DWH/api news front/src/pages/index.tsx:68) 评估时(D:/Projects/AHML-DWH/api news front/src/pages/index.tsx:146) 位于Module../src/pages/index.tsx(commons.js:9487) ...
我想这与运输有关。有什么方法可以做到这一点吗?

简单的答案是,不,这是不可能的


盖茨比从模板文件中提取查询,并专门处理这些查询。它无法使这些查询动态化。理论上,你可以用一个简单的方法来实现这一点。但我认为在盖茨比内部不可能像您发布的那样做到这一点。

从我现在尝试的内容来看,我会告诉您,您不能将指令
@include(if:$myBolean)
用于过滤器、排序等,但您也可以将它们用于盖茨比中的字段

然后,在您的例子中,您可以使用该指令执行整个查询,如

query AllNews($myBolean: Boolean!) { allMarkdownRemark( sort: { order: DESC, fields: [frontmatter___date] } filter: { frontmatter: { type: { eq: "news" } } } ) @include(if:$myBoolean) { edges { node { id frontmatter { title date(formatString: "DD.MM", locale: "ru") type } html } } } } 查询所有新闻($myBolean:Boolean!){ 所有的标记( 排序:{order:DESC,字段:[frontmatter\uuuuuu\u date]} 筛选器:{frontmatter:{type:{eq:“新闻”}} )@include(如果:$myBoolean){ 边缘{ 节点{ 身份证件 前沿物质{ 标题 日期(格式字符串:“DD.MM”,区域设置:“ru”) 类型 } html } } } } 然后可以在一个graphql调用中添加这两个查询,并根据布尔值只执行其中一个查询