gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:48:01 25 4
gpt4 key购买 nike

我有一个简单的 Gatsby 原型(prototype),它使用 Kentico Cloud作为数据源。对我来说幸运的是,他们已经构建了一个 source plugin我正在利用它来获取名为“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();
})
});

}

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

遵循 Gatsby Kentico Starter Template Example ,我修改了我的 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();
})
});
}

如您所见,我记录了结果,以便可以看到它们的样子。

console.log(JSON.stringify(result));

产品

{
"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" }
}
}
]
}
}
}

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

当我运行 gatsbydevelop 时,它实际上编译成功,但出现 graphQL 错误

error GraphQL Error There was an error while compiling your site's GraphQL queries. Error: RelayParser: Encountered duplicate defintitions for one or more documents: each document must have a unique name. Duplicated documents: - templateBuilder

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

graphql(`
{
allKenticoCloudItemBlogPost {
edges {
node {
elements {
url_slug{
value
}
}
}
}
},
allKenticoCloudItemArticle{
edges{
node{
elements{
url_slug{
value
}
}
internal{
type
}
}
}
}
}

我尝试将一个新查询作为新的 promise 放入,但收到编辑器的通知,指出它是无法访问的代码,所以我知道这是行不通的。

它必须很小,因为我已经按照 Gatsby Kentico Source Plugin starter 建模了我的代码。它使用与我相同的技术。我可以毫无问题地下载并运行该项目。所以我不确定我做错了什么。

编辑

我解决了这个问题。问题出在每种数据类型的模板中。我将这两个查询命名为 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
}
}
}
}
`;

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
}
}
}
}
`;

最佳答案

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

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

关于在 Gatsby-node.js 中检索多种数据类型时,graphql 出现重复文档错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54870058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com