【发布时间】:2017-08-12 03:55:10
【问题描述】:
我们在 GraphQL 中这样定义一个类型:
const GraphQLTodo = new GraphQLObjectType({
name: 'Todo',
fields: {
id: globalIdField('Todo'),
text: {
type: GraphQLString,
resolve: (obj) => obj.text,
},
complete: {
type: GraphQLBoolean,
resolve: (obj) => obj.complete,
},
},
interfaces: [nodeInterface], // what is this?
});
我读过 GraphQLInterfaceType - 当类型基本相同但某些字段不同时更合适(这类似于外键吗?)
在 Relay 中,我们通过 nodeDefinitions 获得 nodefield 和 nodeInterface:
const {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
const {type, id} = fromGlobalId(globalId);
if (type === 'Todo') {
return getTodo(id);
} else if (type === 'User') {
return getUser(id);
}
return null;
},
(obj) => {
if (obj instanceof Todo) {
return GraphQLTodo;
} else if (obj instanceof User) {
return GraphQLUser;
}
return null;
}
);
文档和示例只在接口上使用了一个:[] //它是一个数组。但是什么时候需要使用很多接口呢?我只是对它是什么感到困惑,我已经阅读了很多关于它的内容(不知道我的理解是否正确),只是似乎无法将它包裹在我的脑海中
【问题讨论】: