【发布时间】:2019-10-19 21:00:22
【问题描述】:
我有一个旧版 SQL 数据库,其中包含未针对 Relay Modern 优化的表,因为这些表都没有属性
身份证
我使用 Sequelize 作为 ORM,express-graphql 作为 GraphQl 层。
序列化层:
const STAFF = sequelize.define('staff', {
sid: {
// set the primary key for the table
primaryKey: true,
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
unique: true
},
firstName: {
type: Sequelize.STRING,
allowNull: false
}
}
GraphQl 层:
const StaffType = new GraphQLObjectType({
name: 'Staff',
description: 'This is the GraphQl schema for the staff table',
fields: () => ({
sid: { type: GraphQLInt },
firstName: { type: GraphQLString }
})
})
如您所见,此表有一个唯一键 sid。
我想知道是否有办法为服务器上的不同唯一键创建别名,以便我可以在客户端使用 Relay Modern 并可以编写如下查询:
{
node(id: "4") {
id
... on Staff {
firstName
}
}
}
这是 Relay 不得不说的字段:
Relay 对对象识别的支持依赖于 GraphQL 服务器以标准化的方式公开对象标识符。在查询中,模式应该提供一个标准的机制来通过 ID 请求一个对象。在响应中,架构提供了提供这些 ID 的标准方式。
https://facebook.github.io/relay/graphql/objectidentification.htm
【问题讨论】:
-
确定服务器的 GraphQL 架构的代码在哪里?在那里,您应该能够声明字段
id,它在内部解析为sid。 -
我添加了 Sequelize 层。
sid是实际唯一的主键,不能改成id。