我正在使用apollo-codegen从我项目中tsx文件中嵌入的GraphQL查询生成TypeScript类型。生成类型的示例是:
export type MooQuery = {
wines: {
edges: Array< {
node: {
nodeId: string,
wineName: string | null,
wineryId: number | null,
vintageId: number | null,
uomId: number | null
},
} | null > | null,
} | null,
};
我会传递&#34;节点&#34;一个帮助方法,为每个节点呈现一个React组件。我想做类似的事情:
renderHelper(node: MooQuery.wines.edges.node) { // this won't work
}
有没有办法实现我想要的?我想要的甚至有意义吗?
答案 0 :(得分:1)
您可以使用括号表示法
renderHelper(node: MooQuery['wines']['edges'][0]['node']) { // this should work
// ^^^ Don't forget the 0 for the array
}
虽然在你的情况下我只是编辑类型(你只需要生成一次开始,我希望?)
export type MooQuery = {
wines: {
edges: Array<{node: MooNode} | null > | null,
} | null,
};
export type MooNode = {
nodeId: string,
wineName: string | null,
wineryId: number | null,
vintageId: number | null,
uomId: number | null
}
然后只是MooNode