NodeInterface和nodeField如何在relayjs中工作以及它们的含义

ter*_*poe 7 node.js graphql graphql-js 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;
});
Run Code Online (Sandbox Code Playgroud)

Fan*_*Jin 0

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

在 GraphQL 中,数据用树来表示,树的节点是不同类型的数据。 在此输入图像描述

nodeInterface、nodeField 用于序列化和反序列化节点。例如:如果您想将一本书添加到图书收藏中,则必须提供新书及其 ID。然后,Relay 会将 book 对象转换为新的 BookType 节点并将其添加到树中。

我创建了一个有关如何使用 React + Relay + GraphQL 上传图像的演示。

Github 仓库: https: //github.com/bfwg/relay-gallery

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