【问题标题】:Is it possible to use dynamic query alias names in GraphQL?是否可以在 GraphQL 中使用动态查询别名?
【发布时间】:2019-04-12 18:58:31
【问题描述】:

我目前正在开发 Gatsby 文档网站。一个特定页面根据其本地文件路径结构的正则表达式搜索检索 HTML/CSS 组件的各种 README 文件的内容,这些组件分为三个不同的类别。我目前正在使用 3 个单独的别名查询来检索非常相似的数据,我的 DRY 编码器认为这应该可以使用一个和一个 $group 类型变量(它将替换下面代码中的原子、分子和有机体)或类似的东西。由于我是 GraphQL 的真正新手,我不确定这是否可行,而且我似乎无法在网上找到任何人这样做。这是我目前所拥有的:

export const pageQuery = graphql`
  query($path: String!) {
    pageData: 
      markdownRemark(fields: { slug: { eq: $path } }) {
        html
        fields {
          slug
          title
        }
        fileAbsolutePath
      }


    atoms:
      allMarkdownRemark(sort: {order: ASC, fields: [fields___title]}, limit: 1000, filter: {fileAbsolutePath: {regex: "/dl-atoms/"}}) {
        edges {
          node {
            fields {
              slug
              title
            }
          }
        }
      }

    molecules:
      allMarkdownRemark(sort: {order: ASC, fields: [fields___title]}, limit: 1000, filter: {fileAbsolutePath: {regex: "/dl-molecules/"}}) {
        edges {
          node {
            fields {
              slug
              title
            }
          }
        }
      }
    organisms:
      allMarkdownRemark(sort: {order: ASC, fields: [fields___title]}, limit: 1000, filter: {fileAbsolutePath: {regex: "/dl-organisms/"}}) {
        edges {
          node {
            fields {
              slug
              title
            }
          }
        }
      }
  }
`;

【问题讨论】:

    标签: graphql gatsby


    【解决方案1】:

    您可以定义要在查询中使用的片段。这些允许您定义一次选择集,然后仅通过引用片段的名称来使用它。请注意,您必须知道要为其指定选择集的类型的名称。

    export const pageQuery = graphql`
      query($path: String!) {
        pageData: 
          markdownRemark(fields: { slug: { eq: $path } }) {
            html
            fields {
              slug
              title
            }
            fileAbsolutePath
          }
    
        atoms:
          allMarkdownRemark(sort: {order: ASC, fields: [fields___title]}, limit: 1000, filter: {fileAbsolutePath: {regex: "/dl-atoms/"}}) {
            ...MarkdownRemarkFields
          }
    
        molecules:
          allMarkdownRemark(sort: {order: ASC, fields: [fields___title]}, limit: 1000, filter: {fileAbsolutePath: {regex: "/dl-molecules/"}}) {
            ...MarkdownRemarkFields
          }
        organisms:
          allMarkdownRemark(sort: {order: ASC, fields: [fields___title]}, limit: 1000, filter: {fileAbsolutePath: {regex: "/dl-organisms/"}}) {
            ...MarkdownRemarkFields
          }
      }
    
      fragment MarkdownRemarkFields on MarkdownRemarkConnection {
        edges {
          node {
            fields {
              slug
              title
            }
          }
        }
      }
    `;
    

    Gatsby 文档 here 中提到了片段。

    【讨论】:

    • 该技术只是减少了查询所需数据的代码重复。是否不能从别名和/或正则表达式过滤器中命名替换原子、分子、有机体名称,以使用可以根据大多数编程语言在循环内更改值的变量?
    猜你喜欢
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    相关资源
    最近更新 更多