如何将Gatsby createResolver与嵌套数据一起使用
我正在使用盖茨比的如何将Gatsby createResolver与嵌套数据一起使用,gatsby,Gatsby,我正在使用盖茨比的createResolverAPI将标记转换为html。它在顶级数据上运行良好。但是,我无法让它在嵌套更深的数组上工作 以下是有效的方法: function markdownToHTMLResolver(nodeType, node, type) { return { [nodeType]: { [`${node}_html`]: { type: type, resolve: (source, args, context,
createResolver
API将标记转换为html。它在顶级数据上运行良好。但是,我无法让它在嵌套更深的数组上工作
以下是有效的方法:
function markdownToHTMLResolver(nodeType, node, type) {
return {
[nodeType]: {
[`${node}_html`]: {
type: type,
resolve: (source, args, context, info) => {
return remark().use(html).processSync(source[node]).contents;
},
},
},
};
}
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
...markdownToHTMLResolver(BLOG_NODE_TYPE, 'body', 'String'),
...markdownToHTMLResolver(FRONT_NODE_TYPE, 'body', 'String'),
...markdownToHTMLResolver(EVENT_NODE_TYPE, 'body', 'String'),
...markdownToHTMLPageResolver(PAGE_NODE_TYPE, 'body', 'String'),
};
createResolvers(resolvers);
};
这适用于我的REST API中的数据,其结构如下:
{
title: 'Title',
body: '## Heading 2 \n\nParagraph Text.'
}
{
title: 'Title',
body: '## Heading 2 \n\nParagraph Text.'
list: {
data: [
{ title: 'Nested Title 1', body: 'Nested body markdown text 1.'},
{ title: 'Nested Title 2', body: 'Nested body markdown text 2.'},
{ title: 'Nested Title 3', body: 'Nested body markdown text 3.'},
[
}
}
list_html: {
type: 'PageList',
resolve: (source, args, context, info) => {
const dataArray = source.list.data.map((item) => {
return {
title: item.title,
body: remark().use(html).processSync(item.body).contents,
};
});
return {
data: dataArray,
};
},
},
但是,我不太明白如何将其用于这样的嵌套数据:
{
title: 'Title',
body: '## Heading 2 \n\nParagraph Text.'
}
{
title: 'Title',
body: '## Heading 2 \n\nParagraph Text.'
list: {
data: [
{ title: 'Nested Title 1', body: 'Nested body markdown text 1.'},
{ title: 'Nested Title 2', body: 'Nested body markdown text 2.'},
{ title: 'Nested Title 3', body: 'Nested body markdown text 3.'},
[
}
}
list_html: {
type: 'PageList',
resolve: (source, args, context, info) => {
const dataArray = source.list.data.map((item) => {
return {
title: item.title,
body: remark().use(html).processSync(item.body).contents,
};
});
return {
data: dataArray,
};
},
},
我不确定这个嵌套数据的类型应该是什么。我尝试过类似的方法,但它显然有缺陷:
function markdownToHTMLPageResolver(nodeType, node, type) {
return {
[nodeType]: {
[`${node}_html`]: {
type: type,
resolve: (source, args, context, info) => {
return remark().use(html).processSync(source[node]).contents;
},
},
list_html: {
type: ['String'],
resolve: (source, args, context, info) => {
return source.list.data.forEach((item) => ({
title: item.title,
body: remark().use(html).processSync(source[node]).contents,
}));
},
},
},
};
}
任何指导或帮助都将不胜感激。所以我想我已经找到了答案。我能够使用
\uuuTypeName
在GraphiQL接口中发现推断的类型。然后,我能够迭代数据并按如下方式处理降价:
{
title: 'Title',
body: '## Heading 2 \n\nParagraph Text.'
}
{
title: 'Title',
body: '## Heading 2 \n\nParagraph Text.'
list: {
data: [
{ title: 'Nested Title 1', body: 'Nested body markdown text 1.'},
{ title: 'Nested Title 2', body: 'Nested body markdown text 2.'},
{ title: 'Nested Title 3', body: 'Nested body markdown text 3.'},
[
}
}
list_html: {
type: 'PageList',
resolve: (source, args, context, info) => {
const dataArray = source.list.data.map((item) => {
return {
title: item.title,
body: remark().use(html).processSync(item.body).contents,
};
});
return {
data: dataArray,
};
},
},
似乎保持对象结构与原始结构相同很重要