【发布时间】:2020-05-14 00:52:37
【问题描述】:
使用 Gatsby.js,我想将一些静态文件转换为层次结构。这种层次结构的一个方面是一个“可执行文件”有许多由该可执行文件生成的文件。我的可执行文件的 GraphQL 架构是:
exports.createSchemaCustomization = ({ actions: {createTypes}, schema }) => {
createTypes([
schema.buildObjectType({
name: "CondaExecutable",
fields: {
wrappers: "[File]",
name: "String!",
path: "String!",
publicURL: "String!",
},
interfaces: ["Node"],
}),
])
}
然后,我想在我的新对象的wrapper 字段中添加多个文件,我尝试在createPages 中执行此操作,参考foreign key section in the Gatsby docs:
exports.createPages = async ({ graphql, actions, getNode, createContentDigest, createNodeId}) => {
const { createNode, createNodeField, createPage, createParentChildLink } = actions
const result = await graphql(`
{
allFile(filter: {sourceInstanceName: {in: ["Wrappers", "Definitions"]}}) {
edges {
node {
id
relativePath
extension
publicURL
}
}
}
}
`)
await Promise.all(result.data.allFile.edges.map(async ({ node }) => {
// Now create the node for the single file within that package
const exeId = createNodeId(...);
let exe = getNode(exeId);
if (!exe) {
exe = {
id: exeId,
name: stem,
path: node.relativePath.split('.')[0],
publicURL: exeUrl,
parent: versionId,
wrappers: [],
internal: {
type: "CondaExecutable",
contentDigest: node.relativePath
}
};
await createNode(exe);
}
// Link the executable to the wrapper
const wrappers = exe.wrappers || [];
wrappers.push(node.id)
createNodeField({node: exe, name: 'wrappers___NODE', value: wrappers});
}));
}
很遗憾,这段代码不起作用。我收到错误Cannot return null for non-nullable field File.id。无论如何,我并不奇怪这是错误的,因为我真的不知道我在这里做什么。
如何在我自己的自定义类型和许多 Files 之间建立关系?
【问题讨论】:
标签: javascript graphql gatsby