我从relay-starter-kit
开始,也是通过Relay和GraphQL文档工作的。但是有不少地方是无法解释和神秘的。在Relay中,节点接口和全局标识规范起什么作用?
认真我读了很多单证的处处讲这些东西,但无法找到以下问题的任何令人满意的解释:
是什么呢?我把记录,但它从来没有得到调用:
var {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
var {type, id} = fromGlobalId(globalId);
if (type === 'User') {
return getUser(id);
} else if (type === 'Widget') {
return getWidget(id);
} else {
return null;
}
},
(obj) => {
if (obj instanceof User) {
return userType;
} else if (obj instanceof Widget) {
return widgetType;
} else {
return null;
}
}
);
什么是这样做的实际效果:
interfaces: [nodeInterface],
可能涉及到的是,是什么在这里做的node
领域做:
var queryType = new GraphQLObjectType({
name: 'Query',
fields:() => ({
node: nodeField,
// Add your own root fields here
viewer: {
type: userType,
resolve:() => getViewer(),
},
}),
});
围绕id
字段的魔术是什么?什么是globalIdField
?
我在我的数据库中id
,我想我可以用它在我GraphQL对象:
相反的:
id: globalIdField('User'),
我想用我的数据库ID:
id: {
type: GraphQLID,
description: 'The identifier'
},
但是,如果我这样做,我会在浏览器中发现错误RelayQueryWriter: Could not find a type name for record '1'
。
我可以通过将__typename
添加到我的组件容器中继查询中来摆脱该错误,但这似乎都是错误的。
如果你能在这里给出更深入的内涵和更好的解释并且增强官方文档,这将是非常棒的。
谢谢
希望这将使其进入教程很快。我也被这个困惑了很长一段时间,直到我终于完成了所有工作,并将这些部分拼凑在一起。 –
也许在'fromGlobalId'和'toGlobalId'这个答案中多加点儿?以及它们如何便利重新绘制任何对象类型?读取https://github.com/graphql/graphql-relay-js/blob/master/src/node/node.js和该回购协议中的其他文件对我有很大帮助,但将这些东西转化为文字可以为我节省大量的时间。 –
对于一个简洁的(你可以跳到结尾的总结)的代码说明解释什么神秘nodeInterface,nodeField,globalFieldId是,也许https://medium.com/p/relay-graphql-de-mystifying- node-id-38757121b9c – nethsix