【问题标题】:How does the NodeInterface and nodeField work in relayjs and what does they meannodeInterface 和 nodeField 在 relayjs 中是如何工作的,它们是什么意思
【发布时间】:2017-06-13 09:07:20
【问题描述】:

nodeInterface如何refetch和做对象识别,

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;
});

【问题讨论】:

    标签: node.js graphql relayjs graphql-js


    【解决方案1】:

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

    在 GraphQL 中,数据是用树来表示的,树的节点是不同类型的数据。

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

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

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

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

    【讨论】:

      【解决方案2】:

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

      关于 nodeInterface 简单的答案,最终让我明白的是,nodeInterface 是应用程序与对象保持联系的方式。记住你创建模式时正在使用 GraphQL。您正在创建 GraphQL 类“类型”。所以 nodeInterface 正在检查你的对象 GraphQL 类型。如果你没有模式,那么你就不能有类型。 'id' 属性是关于你的类型实例的想法,或者在这种情况下,是 'teacher'、'college' 或 'student' GraphQLObjectType 的实例。所以在一行中,relay 说,给我对象类型和 id,我会为你跟踪它。如果您需要进一步解释,请告诉我。

      【讨论】:

        猜你喜欢
        • 2017-07-23
        • 2011-06-29
        • 2011-06-07
        • 1970-01-01
        • 2022-11-27
        • 2022-12-10
        • 2012-08-21
        • 1970-01-01
        • 2018-04-24
        相关资源
        最近更新 更多