NodeInterface和nodeField如何在relayjs中工作,它们是什么意思

nodeInterface如何重新提取并做对象识别,

这个type代表什么, obj是什么,这里的id是什么

什么是instanceof的意思

 const { nodeInterface, nodeField } = nodeDefinitions( (globalId) => { const { type, id } = fromGlobalId(globalId); console.log('NodeDefinitions (globalId), id:', id); console.log('NodeDefinitions (globalId), type:', type); if (type === 'teacher') { return teacher.findOne({ _id: id }).exec(); } else if (type === 'college') { return college.findOne({ _id: id }).exec(); } else if (type === 'student') { return student.findOne({ _id: id }).exec(); } return null; }, (obj) => { if( obj instanceof Teacher) { return teacherType } // other types too return null; }); 

nodeDefinitions返回对象可以实现的Node接口,并返回包含在查询types中的节点根字段。 为了实现这一点,它需要一个函数来parsing一个ID到一个对象,并确定一个给定的对象的types。

在GraphQL中,数据由树来表示,树的节点是不同types的数据。 在这里输入图像描述

nodeInterface,nodeField用于序列化和反序列化节点。 例如:如果你想添加一本书到书籍集合,必须提供新的书籍和它的ID。 然后,中继将书对象转换为一个新的BookType节点并将其添加到树中。

我已经创build了一个关于如何使用React + Relay + GraphQL上传图片的演示。

Github回购: https : //github.com/bfwg/relay-gallery

现场演示: http : //fanjin.computer

InstanceOf在这种情况下是检查具有给定的传递给globalID的对象的类types。 请参阅Mozilla上的instanceof 。

关于 nodeInterface简单的答案,以及最终让我明白的是,nodeInterface是应用程序如何与对象保持联系。记住您在创build模式时使用GraphQL进行的操作。 您正在创buildGraphQL类“types”。 所以nodeInterface,正在检查你的对象GraphQL Type。 如果你没有架构,那么你不能有types。 'id'属性就是你的types的实例,或者在这种情况下,是'teacher','college'或'student'GraphQLObjectType的实例。 因此,接力人员说,给我的对象types和ID,我会为你保持跟踪。 让我知道你是否需要进一步的解释。