在服务器fromGlobalId(id)上用于从中继全局 Id 到对象上的实际 Id。
目前我在客户端上做同样的事情,即
javascript
import { fromGlobalId } from 'graphql-relay'
fromGlobalId(accounts[0].id)
它给出了正确的结果:
Object {type: "Account", id: "0"}
但是我应该graphql-relay在客户端上导入有没有一种方法可以做到这一点react-relay?
如果您需要对象类型和 id,也许最好在 GraphQL 响应中将其传回?
在服务器端,在您的 GraphQL 模式中定义:
var accountType = new GraphQLObjectType({
name: 'Account',
description: 'An account',
fields: () => ({
id: globalIdField('Account'),
objectType: {
type: GraphQLString,
description: 'Object type',
}
objectId: {
type: GraphQLString,
description: 'Object ID',
},
}),
interfaces: [nodeInterface],
});
在客户端,只需查询 objectType、objectId:
fragments: {
account: () => Relay.QL`
fragments on Account {
id,
objectType,
objectId,
}
`
}