在Gatsby-node.js中检索多种数据类型时,graphql复制文档错误

onT*_*net 5 kentico graphql gatsby kentico-kontent

我有一个简单的Gatsby原型,它利用Kentico Cloud作为数据源。对我来说幸运的是,他们已经构建了一个源插件,我正在利用该插件来获取一个名为“ BlogPost”的数据类型。这按预期工作。

gatsby-node.js源代码

const path = require(`path`);

exports.createPages = ({graphql, actions}) => {
    const { createPage } = actions;
    return new Promise((resolve, reject) => {
        graphql(`
        {
            allKenticoCloudItemBlogPost {
              edges {
                node {
                  elements {
                    url_slug{
                      value
                    }
                  }
                }
              }
            }
          }
        `).then(result => {        
            console.log(result);
            result.data.allKenticoCloudItemBlogPost.edges.map(({node}) => {
                createPage({
                    path: `${node.elements.url_slug.value}`,
                    component: path.resolve(`./src/templates/blog-post.js`),
                    context: {
                        slug: node.elements.url_slug.value,
                    },
                })
            })
            resolve();
        })
    });

}
Run Code Online (Sandbox Code Playgroud)

这很好用,但是我真的想添加第二种数据类型,称为“ Articles”

按照Gatsby Kentico入门模板示例,我修改了gatsby-node.js文件

const path = require(`path`);

exports.createPages = ({graphql, actions}) => {
    const { createPage } = actions;

    return new Promise((resolve, reject) => {
        graphql(`
        {
          allKenticoCloudItemBlogPost {
            edges {
              node {
                elements {
                  url_slug{
                    value
                  }
                }
              }
            }
          }
          allKenticoCloudItemArticle{
            edges{
              node{
                elements{
                  url_slug{
                    value
                  } 
                }
                internal{
                  type
                }
              }
            }
          }
        }
        `).then(result => {
            console.log('START HERE');        
            console.log(JSON.stringify(result));
            result.data.allKenticoCloudItemBlogPost.edges.map(({node}) => {
              createPage({
                  path: `${node.elements.url_slug.value}`,
                  component: path.resolve(`./src/templates/blog-post.js`),
                  context: {
                      slug: node.elements.url_slug.value,
                  },
              })
            });
            result.data.allKenticoCloudItemArticle.edges.map(({node}) => {
              createPage({
                path: `${node.elements.url_slug.value}`,
                component: path.resolve(`./src/templates/article.js`),
                context: {
                  slug: node.elements.url_slug.value,
                },
              })
            })
            resolve();
        })
    });
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我记录了结果,以便查看它们的外观。

console.log(JSON.stringify(result));
Run Code Online (Sandbox Code Playgroud)

产生

{
  "data": {
    "allKenticoCloudItemBlogPost": {
      "edges": [
        {
          "node": { "elements": { "url_slug": { "value": "my-first-post" } } }
        },
        {
          "node": {
            "elements": { "url_slug": { "value": "my-second-blog-post" } }
          }
        },
        { "node": { "elements": { "url_slug": { "value": "3rd-blog-post" } } } }
      ]
    },
    "allKenticoCloudItemArticle": {
      "edges": [
        {
          "node": {
            "elements": { "url_slug": { "value": "article-1-example" } },
            "internal": { "type": "KenticoCloudItemArticle" }
          }
        },
        {
          "node": {
            "elements": { "url_slug": { "value": "article-2" } },
            "internal": { "type": "KenticoCloudItemArticle" }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很好。我看到了我期望看到的。

当我运行时,gatsby develop它实际上可以成功编译,但是出现了graphQL错误

error GraphQL Error编译站点的GraphQL查询时出错。错误:RelayParser:遇到一个或多个文档的重复定义:每个文档必须具有唯一的名称。重复的文档:-templateBuilder

我试图通过在第一个BlogPost查询后添加逗号来解决此问题。

graphql(`
{
  allKenticoCloudItemBlogPost {
    edges {
      node {
        elements {
          url_slug{
            value
          }
        }
      }
    }
  },
  allKenticoCloudItemArticle{
    edges{
      node{
        elements{
          url_slug{
            value
          } 
        }
        internal{
          type
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我试图将新查询作为新的承诺,但从编辑器收到一条通知,通知它代码无法访问,因此我知道这行不通。

它必须很小,因为我是在使用与我相同的技术的Gatsby Kentico Source Plugin启动程序之后对代码进行建模的。我可以毫无问题地下载并运行该项目。所以我不确定我在做什么错。

编辑

我解决了 问题出在每种数据类型的模板中。我将两个查询都命名为templateBuilder。我将博客模板更改为blogBu​​ilder,并将文章模板更改为articleBuilder。现在就像魅力一样。

article.js

export const query = graphql`
  query articleBuilder($slug: String!) {
    kenticoCloudItemArticle(elements: { url_slug: { value: { eq: $slug } } }) {
      elements {
        article_title {
          value
        }
        article_content {
          value
        }
        article_date {
          value
        }
        url_slug {
          value
        }
      }
    }
  }
`;
Run Code Online (Sandbox Code Playgroud)

blog-post.js

export const query = graphql`
  query blogBuilder($slug: String!) {
    kenticoCloudItemBlogPost(elements: { url_slug: { value: { eq: $slug } } }) {
      elements {
        blog_title {
          value
        }
        blog_content {
          value
        }
        blog_date {
          value
        }
        url_slug {
          value
        }
      }
    }
  }
`;
Run Code Online (Sandbox Code Playgroud)

onT*_*net 5

问题出在每种数据类型的模板中。我将两个查询都命名为templateBuilder。我将博客模板更改为blogBu​​ilder,并将文章模板更改为articleBuilder。

有关更多详细信息和代码示例,请参见上面的编辑。